From 8d9760f9bfc8be9b1abad9a8212b14ffd4552fd7 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 28 Mar 2015 21:52:46 +0100 Subject: o Adding timestamp by default when converting samples. --- sensor/include/trygvis/sensor.h | 2 +- sensor/include/trygvis/sensor/io.h | 55 +++++++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 10 deletions(-) (limited to 'sensor/include/trygvis') diff --git a/sensor/include/trygvis/sensor.h b/sensor/include/trygvis/sensor.h index 42362b2..4662bab 100644 --- a/sensor/include/trygvis/sensor.h +++ b/sensor/include/trygvis/sensor.h @@ -75,7 +75,7 @@ public: } KeyDictionary(KeyDictionary& that) = delete; - SampleKey *indexOf(const string key) { + SampleKey *indexOf(const string &key) { SampleKeyIndex i = 0; for (auto ptr = keys.cbegin(); ptr != keys.cend(); ptr++, i++) { if ((*ptr)->name == key) { diff --git a/sensor/include/trygvis/sensor/io.h b/sensor/include/trygvis/sensor/io.h index bbeb80e..f92b800 100644 --- a/sensor/include/trygvis/sensor/io.h +++ b/sensor/include/trygvis/sensor/io.h @@ -37,6 +37,25 @@ public: string name; }; +class sample_output_stream_options : public vector { +public: + ~sample_output_stream_options() { + } + + template + o find_option() const { + for (auto it = begin(); it != end(); ++it) { + T *x = dynamic_cast(*it); + + if (x != nullptr) { + return o(x); + } + } + + return o(); + } +}; + class SampleOutputStream { public: virtual void write(SampleRecord const &sample) = 0; @@ -65,6 +84,20 @@ private: std::mutex mutex; }; +class AddTimestampSampleOutputStream : public SampleOutputStream { +public: + AddTimestampSampleOutputStream(unique_ptr underlying, KeyDictionary &dict, const string ×tamp_name); + + ~AddTimestampSampleOutputStream() { + } + + void write(SampleRecord const &sample) override; + +private: + unique_ptr underlying_; + const SampleKey* timestamp_key; +}; + class CsvSampleOutputStream : public SampleOutputStream { public: CsvSampleOutputStream(shared_ptr stream, KeyDictionary &dict); @@ -132,16 +165,21 @@ private: class SampleStreamParser { public: // TODO: return number of samples found for progress indication? - virtual void process(mutable_buffers_1 buffer) = 0; + virtual void process(mutable_buffers_1 &buffer) = 0; virtual sample_format_type type() { return type_; } + virtual KeyDictionary &dict() { + return dict_; + } + protected: sample_format_type type_; + KeyDictionary &dict_; - SampleStreamParser(const sample_format_type type) : type_(type) { + SampleStreamParser(const sample_format_type type, KeyDictionary &dict) : type_(type), dict_(dict) { } }; @@ -149,17 +187,16 @@ class KeyValueSampleStreamParser : public SampleStreamParser { public: KeyValueSampleStreamParser(shared_ptr output, KeyDictionary &dict) : - SampleStreamParser(sample_format_type::CSV), output(output), dict(dict), + SampleStreamParser(sample_format_type::CSV, dict), output(output), line(make_shared>()) { } - void process(mutable_buffers_1 buffer) override; + void process(mutable_buffers_1 &buffer) override; private: void process_line(shared_ptr> packet); static const uint8_t packet_delimiter = '\n'; - KeyDictionary &dict; shared_ptr output; shared_ptr> line; }; @@ -168,11 +205,11 @@ class AutoSampleParser : public SampleStreamParser { public: AutoSampleParser(shared_ptr output, KeyDictionary &dict); + virtual void process(mutable_buffers_1 &buffer) override; + private: unique_ptr parser; unique_ptr keyValueParser; -public: - virtual void process(mutable_buffers_1 buffer); }; unique_ptr open_sample_stream_parser( @@ -184,14 +221,14 @@ unique_ptr open_sample_output_stream( shared_ptr output, KeyDictionary &dict, sample_format_type type, - vector options); + sample_output_stream_options options); static inline unique_ptr open_sample_output_stream( shared_ptr output, KeyDictionary &dict, sample_format_type type) { - vector options; + sample_output_stream_options options; return open_sample_output_stream(output, dict, type, options); } -- cgit v1.2.3