From 7edc4328bfd5eee557108ebdb4243ca06914c41e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 17 Mar 2015 22:56:47 +0100 Subject: o cout and unique_ptr became complicated. o Updating lots of code to the latest api. --- apps/SoilMoistureIo.cpp | 14 +++++++------- apps/SoilMoistureIo.h | 21 +++++++++++---------- apps/apps.h | 7 +++++++ apps/sample-convert.cpp | 14 ++++++++------ apps/sample-timestamp.cpp | 11 ++++++----- apps/sm-serial-read.cpp | 8 ++++---- 6 files changed, 43 insertions(+), 32 deletions(-) (limited to 'apps') 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 stream, KeyDictionary &dict) +CsvSampleOutputStream::CsvSampleOutputStream(shared_ptr stream, KeyDictionary &dict) : stream(move(stream)), headerWritten(false), dict(dict) { } @@ -84,7 +84,7 @@ void CsvSampleOutputStream::writeHeader() { s << endl; } -JsonSampleOutputStream::JsonSampleOutputStream(unique_ptr stream, KeyDictionary &dict) : +JsonSampleOutputStream::JsonSampleOutputStream(shared_ptr 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 stream, KeyDictionary &dict, string table_name) : +SqlSampleOutputStream::SqlSampleOutputStream(shared_ptr 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> packet) { auto value = static_cast(what[2]); start = what[0].second; - map 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 open_sample_input_stream( - KeyDictionary &dict, shared_ptr output, + KeyDictionary &dict, sample_format_type type) { if (type == sample_format_type::CSV) { return make_unique(output, dict); @@ -272,7 +272,7 @@ unique_ptr open_sample_input_stream( } unique_ptr open_sample_output_stream( - unique_ptr output, + shared_ptr 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 open_sample_input_stream( - KeyDictionary &dict, shared_ptr output, + KeyDictionary &dict, sample_format_type type = sample_format_type::AUTO); unique_ptr open_sample_output_stream( - unique_ptr output, + shared_ptr 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(value)); + values.at(key->index) = o(value); } template @@ -242,7 +242,7 @@ public: class CsvSampleOutputStream : public SampleOutputStream { public: - CsvSampleOutputStream(unique_ptr stream, KeyDictionary &dict); + CsvSampleOutputStream(shared_ptr stream, KeyDictionary &dict); void write(SampleRecord sample); @@ -254,30 +254,30 @@ private: void writeHeader(); KeyDictionary &dict; - unique_ptr stream; + shared_ptr stream; bool headerWritten; }; class JsonSampleOutputStream : public SampleOutputStream { public: - JsonSampleOutputStream(unique_ptr stream, KeyDictionary &dict); + JsonSampleOutputStream(shared_ptr stream, KeyDictionary &dict); void write(SampleRecord sample) override; private: KeyDictionary &dict; - unique_ptr stream; + shared_ptr stream; }; class SqlSampleOutputStream : public SampleOutputStream { public: - SqlSampleOutputStream(unique_ptr stream, KeyDictionary &dict, string table_name); + SqlSampleOutputStream(shared_ptr stream, KeyDictionary &dict, string table_name); void write(SampleRecord sample) override; private: KeyDictionary &dict; - unique_ptr stream; + shared_ptr 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 +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 outputStream; + shared_ptr outputStream; if (output_file == "-") { - outputStream = unique_ptr(&cout); + outputStream = shared_ptr(&cout, noop_deleter); } else { - outputStream = make_unique(output_file); + outputStream = make_shared(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(move(outputStream), dict); + output = make_shared(outputStream, dict); } else if (output_format == "json") { - output = make_shared(move(outputStream), dict); + output = make_shared(outputStream, dict); } else if (output_format == "sql") { if (table_name.size() == 0) { cerr << "Missing option: table-name" << endl; return EXIT_FAILURE; } - output = make_shared(move(outputStream), dict, table_name); + output = make_shared(outputStream, dict, table_name); + } else if (output_format == "csv") { + output = make_shared(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(); - unique_ptr 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(&cout), dict, parser->type()); - auto p = make_shared(move(output_stream), dict, "timestamp", now_name, start_time); - parser = open_sample_input_stream(dict, p, parser->type()); + unique_ptr unique_output_stream = open_sample_output_stream(shared_ptr(&cout, noop_deleter), dict, parser->type()); + shared_ptr output_stream{std::move(unique_output_stream)}; + auto p = make_shared(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 output; - unique_ptr outputStream = unique_ptr(&cout); + shared_ptr outputStream = shared_ptr(&cout, noop_deleter); if (format == Format::JSON) { - output = make_shared(std::move(outputStream), dict); + output = make_shared(outputStream, dict); } else if (format == Format::SQL) { - output = make_shared(std::move(outputStream), dict, "raw"); + output = make_shared(outputStream, dict, "raw"); } else if (format == Format::PLAIN) { - output = make_shared(std::move(outputStream), dict); + output = make_shared(outputStream, dict); } else { cerr << "Unsupported format: " << boost::lexical_cast(format) << endl; return EXIT_FAILURE; -- cgit v1.2.3