RelationalWriter dependency injection v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 03 Feb 2019 12:35:17 +0100
branchv_0
changeset 7 61fc569b77e6
parent 6 4062b8436838
child 8 1e6206284c6c
RelationalWriter dependency injection
src/GuileHandler.h
src/relpipe-tr-guile.cpp
--- a/src/GuileHandler.h	Sun Feb 03 01:44:07 2019 +0100
+++ b/src/GuileHandler.h	Sun Feb 03 12:35:17 2019 +0100
@@ -54,7 +54,7 @@
 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings or use always UTF-8 between C++ and Guile
 
 	Configuration configuration;
-	shared_ptr<writer::RelationalWriter> relationalWriter;
+	writer::RelationalWriter* relationalWriter;
 
 	wregex relationNameRegEx;
 
@@ -146,8 +146,7 @@
 
 public:
 
-	GuileHandler(ostream& output, Configuration& configuration, const vector<string_t>& arguments) : configuration(configuration) {
-		relationalWriter.reset(writer::Factory::create(output));
+	GuileHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration, const vector<string_t>& arguments) : relationalWriter(relationalWriter), configuration(configuration) {
 
 		// FIXME: remove and work directly with configuration in startRelation() and attribute()
 		// i.e. support multiple relationConfigurations
@@ -182,6 +181,7 @@
 
 			currentAttributeIndex++;
 
+			// TODO: > 0 ?:
 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentReaderMetadata.size() == 0) {
 				includeCurrentRecord = scm_to_bool(evalGuileCode(guileCodeWhereCondition));
 				if (includeCurrentRecord) for (auto attribute : currentWriterMetadata) writeGuileValueToAttribute(attribute);
--- a/src/relpipe-tr-guile.cpp	Sun Feb 03 01:44:07 2019 +0100
+++ b/src/relpipe-tr-guile.cpp	Sun Feb 03 12:35:17 2019 +0100
@@ -36,7 +36,6 @@
 #include "CLIParser.h"
 
 using namespace relpipe::cli;
-using namespace relpipe::reader;
 using namespace relpipe::tr::guile;
 
 static void relpipeMain(void *closure, int argc, char **argv) {
@@ -49,8 +48,9 @@
 	try {
 		CLIParser cliParser;
 		Configuration configuration = cliParser.parse(cli.arguments());
-		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
-		GuileHandler handler(std::cout, configuration, cli.arguments());
+		std::shared_ptr<reader::RelationalReader> reader(reader::Factory::create(std::cin));
+		std::shared_ptr<writer::RelationalWriter> writer(writer::Factory::create(std::cout));
+		GuileHandler handler(writer.get(), configuration, cli.arguments());
 		reader->addHandler(&handler);
 		reader->process();