From 2a7ffd694cfa3493ef1b83a69878322b8ca97670 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 14 Mar 2015 23:10:13 +0100 Subject: o Updating to new API. --- apps/SoilMoistureIo.h | 197 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 147 insertions(+), 50 deletions(-) (limited to 'apps/SoilMoistureIo.h') diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h index b8f08e9..b8f0b52 100644 --- a/apps/SoilMoistureIo.h +++ b/apps/SoilMoistureIo.h @@ -33,121 +33,216 @@ class SampleStreamParser; class SampleOutputStream; -unique_ptr open_sample_input_stream(shared_ptr output, sample_format_type type = sample_format_type::AUTO); +class KeyDictionary; -unique_ptr open_sample_output_stream(sample_format_type type, unique_ptr output, - o> fields = o>()); +class SampleKey; -class Sample { +unique_ptr open_sample_input_stream( + KeyDictionary &dict, + shared_ptr output, + sample_format_type type = sample_format_type::AUTO); + +unique_ptr open_sample_output_stream( + KeyDictionary &dict, + sample_format_type type, + unique_ptr output, + o> fields = o>()); + +class sample_exception : public runtime_error { public: - Sample() : entries() { + sample_exception(const string &what) : runtime_error(what) { } +}; - Sample(map entries) : entries(entries) { +struct SampleKey { + // TODO: only the dictionary should be able to create keys + SampleKey(string &name) : name(name) { + if (name.length() == 0) { + throw sample_exception("Bad sample key."); + } } - map::iterator find(string &s) { - return entries.find(s); + inline + bool operator==(const SampleKey &that) const { + return name == that.name; } - map::iterator begin() { - return entries.begin(); - } + string name; +}; + +class KeyDictionary { +public: + typedef vector v; + typedef v::size_type index_t; - map::iterator end() { - return entries.end(); + KeyDictionary() { } - /** - * @throws std::out_of_range - */ - inline const string &operator[](string key) { - return at(key); + index_t indexOf(const SampleKey key) { + index_t i = 0; + for (auto ptr = keys.begin(); ptr != keys.end(); ptr++, i++) { + if (*ptr == key) { + return i; + } + } + + keys.push_back(key); + + return keys.size() - 1; } - /** - * @throws std::out_of_range - */ - const string &at(string key) { - return entries.at(key); + vector findIndexes(v keys); + + inline + v::const_iterator begin() { + return keys.begin(); } - template - const A lexical_at(string key) { - return boost::lexical_cast(entries.at(key)); + inline + v::const_iterator end() { + return keys.end(); } - void set(const std::string &key, const std::string &value) { - entries[key] = value; + string nameOf(index_t index) { + return keys.at(index).name; } private: - map entries; + v keys; }; -class sample_exception : public runtime_error { +class SampleRecord { public: - sample_exception(const string &what) : runtime_error(what) { + typedef vector> vec; + + SampleRecord(KeyDictionary &dict) : dict(dict) { + } + + SampleRecord(KeyDictionary &dict, vec values) + : dict(dict), values(values) { + } + + inline + vec::const_iterator begin() { + return values.begin(); + } + + inline + vec::const_iterator end() { + return values.end(); } + + inline + bool empty() { + return values.empty(); + } + + o at(size_t index) { + if (index >= values.size()) { + return o(); + } + + return values.at(index); + } + + void set(const KeyDictionary::index_t index, const std::string &value) { + values.resize(max(values.size(), index + 1)); + + values[index] = o(value); + } + + template + const o lexical_at(KeyDictionary::index_t index) { + auto value = at(index); + + if (!value) { + return o(); + } + + return o(boost::lexical_cast(value.get())); + } + + string to_string() { + KeyDictionary::index_t i = 0; + string s; + for (auto ptr = values.begin(); ptr != values.end(); ptr++, i++) { + auto o = *ptr; + + if (!o) { + continue; + } + + auto value = o.get(); + + s += dict.nameOf(i) + " = " + value + ", "; + } + return s; + } + +private: + KeyDictionary &dict; + vec values; }; class SampleOutputStream { public: - virtual void write(Sample sample) = 0; + virtual void write(SampleRecord sample) = 0; }; class VectorSampleOutputStream : public SampleOutputStream { public: - virtual void write(Sample sample) override; + virtual void write(SampleRecord sample) override; public: - vector samples; + vector samples; }; class CsvSampleOutputStream : public SampleOutputStream { public: - CsvSampleOutputStream(unique_ptr stream); + CsvSampleOutputStream(KeyDictionary &dict, unique_ptr stream); - CsvSampleOutputStream(unique_ptr stream, vector fields); + CsvSampleOutputStream(KeyDictionary &dict, unique_ptr stream, vector fields); - void write(Sample values); + void write(SampleRecord values); private: void writeHeader(); + KeyDictionary &dict; unique_ptr stream; bool headerWritten; - bool filterFields; - vector fields; + vector fields; }; class JsonSampleOutputStream : public SampleOutputStream { public: - JsonSampleOutputStream(unique_ptr stream); + JsonSampleOutputStream(KeyDictionary &dict, unique_ptr stream); - JsonSampleOutputStream(unique_ptr stream, vector fields); + JsonSampleOutputStream(KeyDictionary &dict, unique_ptr stream, vector fields); - void write(Sample values); + void write(SampleRecord values); private: + KeyDictionary &dict; unique_ptr stream; bool filterFields; - vector fields; + vector fields; }; class SqlSampleOutputStream : public SampleOutputStream { public: - SqlSampleOutputStream(unique_ptr stream, string table_name); + SqlSampleOutputStream(KeyDictionary &dict, unique_ptr stream, string table_name); - SqlSampleOutputStream(unique_ptr stream, string table_name, vector fields); + SqlSampleOutputStream(KeyDictionary &dict, unique_ptr stream, string table_name, vector fields); - void write(Sample values); + void write(SampleRecord values); private: + KeyDictionary &dict; unique_ptr stream; bool filter_fields; - vector fields; + vector fields; const string table_name; }; @@ -169,8 +264,9 @@ protected: class CsvSampleParser : public SampleStreamParser { public: - CsvSampleParser(shared_ptr output) : SampleStreamParser(sample_format_type::CSV), - output(output), line(make_shared>()) { + CsvSampleParser(KeyDictionary &dict, shared_ptr output) : + SampleStreamParser(sample_format_type::CSV), dict(dict), output(output), + line(make_shared>()) { } void process(mutable_buffers_1 buffer) override; @@ -179,13 +275,14 @@ private: void process_line(shared_ptr> packet); static const uint8_t packet_delimiter = '\n'; + KeyDictionary &dict; shared_ptr output; shared_ptr> line; }; class AutoSampleParser : public SampleStreamParser { public: - AutoSampleParser(shared_ptr output); + AutoSampleParser(KeyDictionary &dict, shared_ptr output); private: unique_ptr parser; -- cgit v1.2.3