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. --- sensor/include/trygvis/sensor/io.h | 125 +++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 68 deletions(-) (limited to 'sensor/include') diff --git a/sensor/include/trygvis/sensor/io.h b/sensor/include/trygvis/sensor/io.h index 304091b..403af9b 100644 --- a/sensor/include/trygvis/sensor/io.h +++ b/sensor/include/trygvis/sensor/io.h @@ -4,6 +4,7 @@ #include #include +#include namespace trygvis { namespace sensor { @@ -13,47 +14,40 @@ using namespace std; using namespace boost::asio; struct sample_output_stream_option { - virtual ~sample_output_stream_option() { - }; + virtual ~sample_output_stream_option() = default;; }; struct output_fields_option : sample_output_stream_option { - ~output_fields_option() { - } + ~output_fields_option() override = default; vector fields; }; struct timestamp_field_option : sample_output_stream_option { - timestamp_field_option(const string name) : name(name) { - } + explicit timestamp_field_option(string name) : name(std::move(name)) {} - ~timestamp_field_option() { - } + ~timestamp_field_option() override = default; const string name; }; class table_name_option : public sample_output_stream_option { public: - table_name_option(const string name) : name(name) { - } + explicit table_name_option(string name) : name(std::move(name)) {} - ~table_name_option() { - } + ~table_name_option() override = default; const string name; }; class sample_output_stream_options : public vector { public: - ~sample_output_stream_options() { - } + ~sample_output_stream_options() = default; template o find_option() const { - for (auto it = begin(); it != end(); ++it) { - T *x = dynamic_cast(*it); + for (auto it : *this) { + auto *x = dynamic_cast(it); if (x != nullptr) { return o(x); @@ -65,90 +59,85 @@ public: }; struct missing_required_option_error : runtime_error { - missing_required_option_error(string what) : runtime_error(what) { - } + explicit missing_required_option_error(const string &what) : runtime_error(what) {} - ~missing_required_option_error() { - } + ~missing_required_option_error() override = default; }; class SampleStreamParser; -class SampleOutputStream; +class SampleConsumer; /** * Throws missing_required_option_error */ unique_ptr open_sample_stream_parser( - shared_ptr output, + shared_ptr output, KeyDictionary &dict, sample_format_type type = sample_format_type::AUTO); /** * Throws missing_required_option_error */ -unique_ptr open_sample_output_stream( +unique_ptr open_sample_writer( shared_ptr output, KeyDictionary &dict, sample_format_type type, sample_output_stream_options options); static inline -unique_ptr open_sample_output_stream( +unique_ptr open_sample_output_stream( shared_ptr output, KeyDictionary &dict, sample_format_type type) { sample_output_stream_options options; - return open_sample_output_stream(output, dict, type, options); + return open_sample_writer(std::move(output), dict, type, options); } -class SampleOutputStream { +class SampleConsumer { public: - virtual void write(SampleRecord const &sample) = 0; + virtual void onSample(SampleRecord const &sample) = 0; }; -class VectorSampleOutputStream : public SampleOutputStream { +class VectorSampleOutputStream : public SampleConsumer { public: - virtual void write(SampleRecord const &sample) override; + void onSample(SampleRecord const &sample) override; -public: vector samples; }; -class ThreadSafeSampleOutputStream : public SampleOutputStream { +class ThreadSafeSampleConsumer : public SampleConsumer { public: - ThreadSafeSampleOutputStream(unique_ptr underlying); + explicit ThreadSafeSampleConsumer(unique_ptr underlying); - ~ThreadSafeSampleOutputStream() { - } + ~ThreadSafeSampleConsumer() = default; - void write(SampleRecord const &sample) override; + void onSample(SampleRecord const &sample) override; private: - unique_ptr underlying; + unique_ptr underlying; std::mutex mutex; }; -class AddTimestampSampleOutputStream : public SampleOutputStream { +class AddTimestampSampleConsumer : public SampleConsumer { public: - AddTimestampSampleOutputStream(unique_ptr underlying, KeyDictionary &dict, const string ×tamp_name); + AddTimestampSampleConsumer(unique_ptr underlying, KeyDictionary &dict, const string ×tamp_name); - ~AddTimestampSampleOutputStream() { - } + ~AddTimestampSampleConsumer() = default; - void write(SampleRecord const &sample) override; + void onSample(SampleRecord const &sample) override; private: - unique_ptr underlying_; + unique_ptr underlying_; const SampleKey* timestamp_key; }; -class CsvSampleOutputStream : public SampleOutputStream { +class CsvWriterSampleConsumer : public SampleConsumer { public: - CsvSampleOutputStream(shared_ptr stream, KeyDictionary &dict); + CsvWriterSampleConsumer(shared_ptr stream, KeyDictionary &dict); - void write(SampleRecord const &sample); + void onSample(SampleRecord const &sample) override; private: void writeHeader(); @@ -158,33 +147,33 @@ private: bool headerWritten; }; -class JsonSampleOutputStream : public SampleOutputStream { +class JsonWriterSampleConsumer : public SampleConsumer { public: - JsonSampleOutputStream(shared_ptr stream, KeyDictionary &dict); + JsonWriterSampleConsumer(shared_ptr stream, KeyDictionary &dict); - void write(SampleRecord const &sample) override; + void onSample(SampleRecord const &sample) override; private: KeyDictionary &dict; shared_ptr stream; }; -class KeyValueSampleOutputStream : public SampleOutputStream { +class KeyValueWriterSampleConsumer : public SampleConsumer { public: - KeyValueSampleOutputStream(shared_ptr stream, KeyDictionary &dict); + KeyValueWriterSampleConsumer(shared_ptr stream, KeyDictionary &dict); - void write(SampleRecord const &sample) override; + void onSample(SampleRecord const &sample) override; private: KeyDictionary &dict; shared_ptr stream; }; -class RrdSampleOutputStream : public SampleOutputStream { +class RrdWriterSampleConsumer : public SampleConsumer { public: - RrdSampleOutputStream(shared_ptr stream, KeyDictionary &dict, const SampleKey *timestamp_key, o output_fields); + RrdWriterSampleConsumer(shared_ptr stream, KeyDictionary &dict, const SampleKey *timestamp_key, o output_fields); - void write(SampleRecord const &sample) override; + void onSample(SampleRecord const &sample) override; private: vector keys; @@ -192,11 +181,11 @@ private: const SampleKey *timestamp_key; }; -class SqlSampleOutputStream : public SampleOutputStream { +class SqlWriterSampleConsumer : public SampleConsumer { public: - SqlSampleOutputStream(shared_ptr stream, KeyDictionary &dict, string table_name); + SqlWriterSampleConsumer(shared_ptr stream, KeyDictionary &dict, string table_name); - void write(SampleRecord const &sample) override; + void onSample(SampleRecord const &sample) override; private: KeyDictionary &dict; @@ -235,8 +224,8 @@ protected: class KeyValueSampleStreamParser : public SampleStreamParser { public: - KeyValueSampleStreamParser(shared_ptr output, KeyDictionary &dict) : - SampleStreamParser(sample_format_type::CSV, dict), output(output), + KeyValueSampleStreamParser(shared_ptr output, KeyDictionary &dict) : + SampleStreamParser(sample_format_type::CSV, dict), output(std::move(output)), line(make_shared>()) { } @@ -248,17 +237,17 @@ private: void process_line(shared_ptr> &packet); static const uint8_t packet_delimiter = '\n'; - shared_ptr output; + shared_ptr output; shared_ptr> line; }; class AutoSampleParser : public SampleStreamParser { public: - AutoSampleParser(shared_ptr output, KeyDictionary &dict); + AutoSampleParser(shared_ptr output, KeyDictionary &dict); - virtual int process(mutable_buffers_1 &buffer) override; + int process(mutable_buffers_1 &buffer) override; - virtual int finish() override; + int finish() override; private: unique_ptr parser; @@ -266,10 +255,10 @@ private: }; static inline -unique_ptr thread_safe_sample_output_stream(unique_ptr underlying) { - return make_unique(move(underlying)); +unique_ptr thread_safe_sample_output_stream(unique_ptr underlying) { + return make_unique(move(underlying)); }; -} -} -} +} // namespace io +} // namespace sensor +} // namespace trygvis -- cgit v1.2.3