From 1f2c7aae9fdd39a478944ccda5c9e82d76ab5db6 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 1 Sep 2017 13:59:25 +0200 Subject: o Renaming SampleOutputStream to SampleConsumer. o Some c++ style fixes. --- apps/apps.h | 10 ++-------- apps/mqtt-publish.cpp | 10 +++++----- apps/sample-add-timestamp.cpp | 33 ++++++++++++++------------------- apps/sample-convert.cpp | 6 +++--- apps/sample-select.cpp | 6 +++--- apps/sm-get-value.cpp | 4 ++-- apps/sm-serial-read-all.cpp | 2 +- apps/sm-serial-read.cpp | 2 +- 8 files changed, 31 insertions(+), 42 deletions(-) (limited to 'apps') diff --git a/apps/apps.h b/apps/apps.h index 9173ae0..21a25a3 100644 --- a/apps/apps.h +++ b/apps/apps.h @@ -126,12 +126,6 @@ std::string get_hostname(); int real_main(app *app, int argc, const char *argv[]); -template -class noop_delete { -public: - void operator()(T *) const { } -}; - static inline void noop_deleter(void *) { } -} -} +} // namespace apps +} // namespace trygvis diff --git a/apps/mqtt-publish.cpp b/apps/mqtt-publish.cpp index 25e8ae9..1498b73 100644 --- a/apps/mqtt-publish.cpp +++ b/apps/mqtt-publish.cpp @@ -22,12 +22,12 @@ using namespace trygvis::sensor; using namespace trygvis::sensor::io; using namespace trygvis::mqtt_support; -class MqttSampleOutputStream : public SampleOutputStream { +class MqttSampleOutputStream : public SampleConsumer { public: MqttSampleOutputStream(const o &client_id, bool clean_session, string host, unsigned int port, string topic_name, unsigned int keep_alive) - : SampleOutputStream(), + : SampleConsumer(), client(host, port, keep_alive, client_id, clean_session), topic_name(topic_name) { client.connect(); @@ -37,15 +37,15 @@ public: client.disconnect(); }; - void write(SampleRecord const &sample) override { + void onSample(SampleRecord const &sample) override { if (sample.empty()) { return; } // make a string of the sample auto buf = make_shared(); - KeyValueSampleOutputStream out(buf, sample.dict); - out.write(sample); + KeyValueWriterSampleConsumer out(buf, sample.dict); + out.onSample(sample); string s = buf->str(); cout << "sample: " << s; diff --git a/apps/sample-add-timestamp.cpp b/apps/sample-add-timestamp.cpp index 8bc4bac..841782e 100644 --- a/apps/sample-add-timestamp.cpp +++ b/apps/sample-add-timestamp.cpp @@ -1,8 +1,6 @@ #include "trygvis/sensor.h" #include "trygvis/sensor/io.h" #include "apps.h" -#include -#include "apps.h" namespace trygvis { namespace apps { @@ -13,10 +11,10 @@ using namespace trygvis::sensor; using namespace trygvis::sensor::io; namespace po = boost::program_options; -class TimestampAddingSampleOutputStream : public SampleOutputStream { +class TimestampAddingSampleOutputStream : public SampleConsumer { public: - TimestampAddingSampleOutputStream(shared_ptr output, KeyDictionary &dict, string timestamp_name) - : timestamp_key(dict.indexOf(timestamp_name)) { + TimestampAddingSampleOutputStream(shared_ptr output, KeyDictionary &dict, string timestamp_name) + : output_(output), timestamp_key(dict.indexOf(timestamp_name)) { if (input_time_resolution_ == time_resolution::MILLISECONDS) { factor = 1000; } else { @@ -24,21 +22,21 @@ public: } } - virtual void write(SampleRecord const &sample) override { + virtual void onSample(const SampleRecord &sample) override { time_t now = time(NULL) * factor; SampleRecord updated_sample(sample); updated_sample.set(timestamp_key, std::to_string(now)); - output_->write(updated_sample); + output_->onSample(updated_sample); }; private: const SampleKey *timestamp_key; - time_resolution input_time_resolution_; + const time_resolution input_time_resolution_ = time_resolution::MILLISECONDS; int factor; - shared_ptr output_; + shared_ptr output_; }; class sample_add_timestamp : public app { @@ -62,25 +60,22 @@ public: const int buffer_size = 1024; int main(app_execution &execution) override { - shared_ptr input; - - input = shared_ptr(&cin, noop_deleter); + auto stdin = shared_ptr(&cin, noop_deleter); + auto stdout = shared_ptr(&cout, noop_deleter); KeyDictionary dict; sample_output_stream_options options = {}; - auto unique_output_stream = - open_sample_output_stream(shared_ptr(&cout, noop_deleter), dict, output_format, options); - shared_ptr output_stream{std::move(unique_output_stream)}; - auto p = make_shared(output_stream, dict, timestamp_name); + auto writer = open_sample_writer(stdout, dict, output_format, options); + auto p = make_shared(std::move(writer), dict, timestamp_name); auto parser = open_sample_stream_parser(p, dict); int recordCount = 0; - while (!input->eof()) { + while (!stdin->eof()) { char buffer[buffer_size]; - input->read(buffer, buffer_size); - size_t gcount = (size_t) input->gcount(); + stdin->read(buffer, buffer_size); + auto gcount = static_cast(stdin->gcount()); recordCount++; diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp index 81c08ca..2142462 100644 --- a/apps/sample-convert.cpp +++ b/apps/sample-convert.cpp @@ -93,13 +93,13 @@ public: options.push_back(&fs); } - unique_ptr o = open_sample_output_stream(outputStream, dict, output_format, options); + unique_ptr o = open_sample_writer(outputStream, dict, output_format, options); if (add_timestamp) { - o = make_unique(move(o), dict, timestamp_field); + o = make_unique(move(o), dict, timestamp_field); } - shared_ptr output(move(o)); + shared_ptr output(move(o)); auto input = make_shared(output, dict); diff --git a/apps/sample-select.cpp b/apps/sample-select.cpp index d067af4..1db1443 100644 --- a/apps/sample-select.cpp +++ b/apps/sample-select.cpp @@ -91,10 +91,10 @@ public: } vector options = {}; - unique_ptr unique_output_stream = open_sample_output_stream(shared_ptr(&cout, + unique_ptr unique_output_stream = open_sample_writer(shared_ptr(&cout, noop_deleter), dict, parser->type(), options); - shared_ptr output_stream{std::move(unique_output_stream)}; - shared_ptr p = make_shared(output_stream, dict, + shared_ptr output_stream{std::move(unique_output_stream)}; + shared_ptr p = make_shared(output_stream, dict, timestamp_name, relative_name, relative_resolution, start_time); parser = open_sample_stream_parser(p, dict, parser->type()); diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp index 42f5da3..483df70 100644 --- a/apps/sm-get-value.cpp +++ b/apps/sm-get-value.cpp @@ -109,7 +109,7 @@ public: auto epoch = system_clock::now().time_since_epoch(); auto timestamp = duration_cast(epoch).count(); auto unique_output_stream = open_sample_output_stream(shared_ptr(&cout, noop_deleter), dict, format); - shared_ptr output_stream{std::move(unique_output_stream)}; + shared_ptr output_stream{std::move(unique_output_stream)}; auto sample = SampleRecord(dict) .set(hostname_key, get_hostname()) @@ -138,7 +138,7 @@ public: i++; } - output_stream->write(sample); + output_stream->onSample(sample); } void withConnection(sample_format_type format, shared_ptr gatt) { diff --git a/apps/sm-serial-read-all.cpp b/apps/sm-serial-read-all.cpp index a81a016..3c2b617 100644 --- a/apps/sm-serial-read-all.cpp +++ b/apps/sm-serial-read-all.cpp @@ -115,7 +115,7 @@ public: auto output_stream = shared_ptr(&cout, noop_deleter); auto output = open_sample_output_stream(output_stream, outputDict, format); auto tso = thread_safe_sample_output_stream(move(output)); - shared_ptr thread_safe_output(move(tso)); + shared_ptr thread_safe_output(move(tso)); while (run) { // Port cleanup diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp index 980a3f6..69931a0 100644 --- a/apps/sm-serial-read.cpp +++ b/apps/sm-serial-read.cpp @@ -90,7 +90,7 @@ public: } shared_ptr outputStream = shared_ptr(&cout, noop_deleter); - shared_ptr output = open_sample_output_stream(outputStream, dict, format); + shared_ptr output = open_sample_output_stream(outputStream, dict, format); shared_ptr input = make_shared(output, dict); -- cgit v1.2.3