diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-17 22:56:47 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-17 22:56:47 +0100 |
commit | 7edc4328bfd5eee557108ebdb4243ca06914c41e (patch) | |
tree | fd01761ed57c6f5d31409c7777b4c0c127a1e482 | |
parent | 5d6e4c971857b5e05d7c277e7eafcb9d64dbc6a7 (diff) | |
download | ble-toys-7edc4328bfd5eee557108ebdb4243ca06914c41e.tar.gz ble-toys-7edc4328bfd5eee557108ebdb4243ca06914c41e.tar.bz2 ble-toys-7edc4328bfd5eee557108ebdb4243ca06914c41e.tar.xz ble-toys-7edc4328bfd5eee557108ebdb4243ca06914c41e.zip |
o cout and unique_ptr became complicated.
o Updating lots of code to the latest api.
-rw-r--r-- | apps/SoilMoistureIo.cpp | 14 | ||||
-rw-r--r-- | apps/SoilMoistureIo.h | 21 | ||||
-rw-r--r-- | apps/apps.h | 7 | ||||
-rw-r--r-- | apps/sample-convert.cpp | 14 | ||||
-rw-r--r-- | apps/sample-timestamp.cpp | 11 | ||||
-rw-r--r-- | apps/sm-serial-read.cpp | 8 |
6 files changed, 43 insertions, 32 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp index ad8a3bb..c9cfa1e 100644 --- a/apps/SoilMoistureIo.cpp +++ b/apps/SoilMoistureIo.cpp @@ -15,7 +15,7 @@ void VectorSampleOutputStream::write(SampleRecord sample) { samples.emplace_back(sample); } -CsvSampleOutputStream::CsvSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict) +CsvSampleOutputStream::CsvSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict) : stream(move(stream)), headerWritten(false), dict(dict) { } @@ -84,7 +84,7 @@ void CsvSampleOutputStream::writeHeader() { s << endl; } -JsonSampleOutputStream::JsonSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict) : +JsonSampleOutputStream::JsonSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict) : dict(dict), stream(move(stream)) { } @@ -121,7 +121,7 @@ void JsonSampleOutputStream::write(SampleRecord sample) { *stream.get() << doc << endl; } -SqlSampleOutputStream::SqlSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict, string table_name) : +SqlSampleOutputStream::SqlSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict, string table_name) : dict(dict), stream(move(stream)), table_name(table_name) { } @@ -217,8 +217,8 @@ void CsvSampleParser::process_line(shared_ptr<vector<uint8_t>> packet) { auto value = static_cast<string>(what[2]); start = what[0].second; - map<string, string> values; - values[name] = value; + + auto key = dict.indexOf(name); sample.set(key, value); @@ -259,8 +259,8 @@ string to_string(const sample_format_type &arg) { } unique_ptr<SampleStreamParser> open_sample_input_stream( - KeyDictionary &dict, shared_ptr<SampleOutputStream> output, + KeyDictionary &dict, sample_format_type type) { if (type == sample_format_type::CSV) { return make_unique<CsvSampleParser>(output, dict); @@ -272,7 +272,7 @@ unique_ptr<SampleStreamParser> open_sample_input_stream( } unique_ptr<SampleOutputStream> open_sample_output_stream( - unique_ptr<ostream> output, + shared_ptr<ostream> output, KeyDictionary &dict, sample_format_type type) { diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h index 473c098..0d93d2a 100644 --- a/apps/SoilMoistureIo.h +++ b/apps/SoilMoistureIo.h @@ -39,12 +39,12 @@ class KeyDictionary; class SampleKey; unique_ptr<SampleStreamParser> open_sample_input_stream( - KeyDictionary &dict, shared_ptr<SampleOutputStream> output, + KeyDictionary &dict, sample_format_type type = sample_format_type::AUTO); unique_ptr<SampleOutputStream> open_sample_output_stream( - unique_ptr<ostream> output, + shared_ptr<ostream> output, KeyDictionary &dict, sample_format_type type); @@ -121,7 +121,7 @@ public: indexes.push_back(index); } - return move(indexes); + return indexes; } inline @@ -190,7 +190,7 @@ public: void set(const SampleKey *key, const std::string &value) { values.resize(max(values.size(), key->index + 1)); - values.assign(key->index, o<string>(value)); + values.at(key->index) = o<string>(value); } template<class A> @@ -242,7 +242,7 @@ public: class CsvSampleOutputStream : public SampleOutputStream { public: - CsvSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict); + CsvSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict); void write(SampleRecord sample); @@ -254,30 +254,30 @@ private: void writeHeader(); KeyDictionary &dict; - unique_ptr<ostream> stream; + shared_ptr<ostream> stream; bool headerWritten; }; class JsonSampleOutputStream : public SampleOutputStream { public: - JsonSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict); + JsonSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict); void write(SampleRecord sample) override; private: KeyDictionary &dict; - unique_ptr<ostream> stream; + shared_ptr<ostream> stream; }; class SqlSampleOutputStream : public SampleOutputStream { public: - SqlSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict, string table_name); + SqlSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict, string table_name); void write(SampleRecord sample) override; private: KeyDictionary &dict; - unique_ptr<ostream> stream; + shared_ptr<ostream> stream; const string table_name; }; @@ -296,6 +296,7 @@ protected: } }; +// TODO: rename to "KeyValueParser", this is not CSV. class CsvSampleParser : public SampleStreamParser { public: diff --git a/apps/apps.h b/apps/apps.h index 1995e43..03ec68f 100644 --- a/apps/apps.h +++ b/apps/apps.h @@ -35,6 +35,13 @@ int launch_app(int argc, char *argv[], app &app); std::string get_hostname(); +template<typename T> +class noop_delete { +public: + void operator()(T *) const { + } +}; + static inline void noop_deleter(void *) { } diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp index 5e87a15..c5e9c4f 100644 --- a/apps/sample-convert.cpp +++ b/apps/sample-convert.cpp @@ -49,11 +49,11 @@ public: } } - unique_ptr<ostream> outputStream; + shared_ptr<ostream> outputStream; if (output_file == "-") { - outputStream = unique_ptr<ostream>(&cout); + outputStream = shared_ptr<ostream>(&cout, noop_deleter); } else { - outputStream = make_unique<ofstream>(output_file); + outputStream = make_shared<ofstream>(output_file); if (outputStream->fail()) { cerr << "Unable to open output file " << output_file << endl; return EXIT_FAILURE; @@ -61,16 +61,18 @@ public: } if (output_format == "plain") { - output = make_shared<CsvSampleOutputStream>(move(outputStream), dict); + output = make_shared<CsvSampleOutputStream>(outputStream, dict); } else if (output_format == "json") { - output = make_shared<JsonSampleOutputStream>(move(outputStream), dict); + output = make_shared<JsonSampleOutputStream>(outputStream, dict); } else if (output_format == "sql") { if (table_name.size() == 0) { cerr << "Missing option: table-name" << endl; return EXIT_FAILURE; } - output = make_shared<SqlSampleOutputStream>(move(outputStream), dict, table_name); + output = make_shared<SqlSampleOutputStream>(outputStream, dict, table_name); + } else if (output_format == "csv") { + output = make_shared<CsvSampleOutputStream>(outputStream, dict); } else { cerr << "Unsupported output format: " << output_format << endl; return EXIT_FAILURE; diff --git a/apps/sample-timestamp.cpp b/apps/sample-timestamp.cpp index dd9ab3c..4855506 100644 --- a/apps/sample-timestamp.cpp +++ b/apps/sample-timestamp.cpp @@ -81,7 +81,7 @@ public: now_key = dict.indexOf(now_name); auto sample_buffer = make_shared<VectorSampleOutputStream>(); - unique_ptr<SampleStreamParser> parser = open_sample_input_stream(dict, sample_buffer); + auto parser = open_sample_input_stream(sample_buffer, dict); while (!input.fail()) { char buffer[buffer_size]; input.read(buffer, buffer_size); @@ -129,9 +129,10 @@ public: return EXIT_FAILURE; } - auto output_stream = open_sample_output_stream(unique_ptr<ostream>(&cout), dict, parser->type()); - auto p = make_shared<TimestampFixingSampleOutputStream>(move(output_stream), dict, "timestamp", now_name, start_time); - parser = open_sample_input_stream(dict, p, parser->type()); + unique_ptr<SampleOutputStream> unique_output_stream = open_sample_output_stream(shared_ptr<ostream>(&cout, noop_deleter), dict, parser->type()); + shared_ptr<SampleOutputStream> output_stream{std::move(unique_output_stream)}; + auto p = make_shared<TimestampFixingSampleOutputStream>(output_stream, dict, timestamp_name, now_name, start_time); + parser = open_sample_input_stream(p, dict, parser->type()); int recordCount = 0; @@ -147,7 +148,7 @@ public: } return EXIT_SUCCESS; - } + }; }; } diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp index c52e7f9..0d0bfe6 100644 --- a/apps/sm-serial-read.cpp +++ b/apps/sm-serial-read.cpp @@ -123,14 +123,14 @@ public: } shared_ptr<SampleOutputStream> output; - unique_ptr<ostream> outputStream = unique_ptr<ostream>(&cout); + shared_ptr<ostream> outputStream = shared_ptr<ostream>(&cout, noop_deleter); if (format == Format::JSON) { - output = make_shared<JsonSampleOutputStream>(std::move(outputStream), dict); + output = make_shared<JsonSampleOutputStream>(outputStream, dict); } else if (format == Format::SQL) { - output = make_shared<SqlSampleOutputStream>(std::move(outputStream), dict, "raw"); + output = make_shared<SqlSampleOutputStream>(outputStream, dict, "raw"); } else if (format == Format::PLAIN) { - output = make_shared<CsvSampleOutputStream>(std::move(outputStream), dict); + output = make_shared<CsvSampleOutputStream>(outputStream, dict); } else { cerr << "Unsupported format: " << boost::lexical_cast<string>(format) << endl; return EXIT_FAILURE; |