pass RelationalWriter instead of std::ostream to the JackCommand v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 18 May 2020 22:00:20 +0200
branchv_0
changeset 2 e5f0d3f92eb4
parent 1 001b956610ca
child 3 0222c20f590f
pass RelationalWriter instead of std::ostream to the JackCommand
src/JackCommand.h
src/relpipe-in-jack.cpp
--- a/src/JackCommand.h	Mon May 18 18:04:12 2020 +0200
+++ b/src/JackCommand.h	Mon May 18 22:00:20 2020 +0200
@@ -23,6 +23,7 @@
 #include <pthread.h>
 #include <sys/mman.h>
 #include <atomic>
+#include <functional>
 
 #include <jack/jack.h>
 #include <jack/midiport.h>
@@ -61,7 +62,7 @@
 		uint32_t size;
 		uint32_t time;
 	};
-
+	
 public:
 
 	int enqueueMessage(jack_nframes_t frames) {
@@ -137,9 +138,8 @@
 		continueProcessing = false;
 	}
 
-	void processJackStream(ostream &output) {
+	void processJackStream(std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
 		// Relation headers:
-		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
 		vector<AttributeMetadata> metadata;
 		metadata.push_back({L"event", TypeId::STRING});
 		metadata.push_back({L"channel", TypeId::INTEGER});
@@ -149,7 +149,7 @@
 		metadata.push_back({L"controller_id", TypeId::INTEGER});
 		metadata.push_back({L"controller_value", TypeId::INTEGER});
 		writer->startRelation(L"midi", metadata, true);
-		output.flush();
+		relationalWriterFlush();
 
 		// Initialize JACK connection:
 		std::string clientName = "relpipe-in-jack";
@@ -177,7 +177,7 @@
 				MidiMessage m;
 				jack_ringbuffer_read(ringBuffer, (char*) &m, sizeof (MidiMessage));
 				processMessage(writer, &m);
-				output.flush();
+				relationalWriterFlush();
 			}
 			pthread_cond_wait(&dataReady, &messageThreadLock);
 		}
--- a/src/relpipe-in-jack.cpp	Mon May 18 18:04:12 2020 +0200
+++ b/src/relpipe-in-jack.cpp	Mon May 18 22:00:20 2020 +0200
@@ -45,7 +45,8 @@
 		signal(SIGHUP, finish);
 		signal(SIGINT, finish);
 		jackCommand.reset(new JackCommand());
-		jackCommand->processJackStream(cout);
+		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
+		jackCommand->processJackStream(writer, std::bind(fflush, stdout)); // std::bind(fflush, XXX) Factory::create(XXX) must be the same stream XXX
 		resultCode = CLI::EXIT_CODE_SUCCESS;
 	} catch (JackException e) {
 		fwprintf(stderr, L"Caught JACK exception: %ls\n", e.getMessge().c_str());