From 2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 12 Apr 2016 20:06:47 +0200 Subject: Soil Moisture: Adding support for controlling lights. Bluetooth: refectorying, trying to be more c++ idiomatic and modern. SM/Diller: adding bluetooth to Diller bridge. --- sensor/include/trygvis/sensor.h | 43 +++++++++++++++++++------------------- sensor/include/trygvis/sensor/io.h | 2 +- sensor/main/io.cpp | 30 +++++++++++++------------- sensor/main/sensor.cpp | 2 +- sensor/test/SampleTest.cpp | 16 +++++++------- 5 files changed, 47 insertions(+), 46 deletions(-) (limited to 'sensor') diff --git a/sensor/include/trygvis/sensor.h b/sensor/include/trygvis/sensor.h index c109a2a..09372b9 100644 --- a/sensor/include/trygvis/sensor.h +++ b/sensor/include/trygvis/sensor.h @@ -5,15 +5,16 @@ #include #include #include -#include +#include +#include namespace trygvis { namespace sensor { -using namespace std; +//using namespace std; -template -using o = boost::optional; +template +using o = std::experimental::optional; enum class sample_format_type { AUTO, @@ -24,7 +25,7 @@ enum class sample_format_type { RRD, }; -string to_string(const sample_format_type &arg); +std::string to_string(const sample_format_type &arg); std::ostream& operator<<(std::ostream& os, sample_format_type const& type); @@ -39,9 +40,9 @@ std::ostream& operator<<(std::ostream& os, time_resolution const& type); std::istream& operator>>(std::istream& is, time_resolution & type); -class sample_exception : public runtime_error { +class sample_exception : public std::runtime_error { public: - sample_exception(const string &what) : runtime_error(what) { + sample_exception(const std::string &what) : runtime_error(what) { } }; @@ -51,13 +52,13 @@ class SampleKey; class KeyDictionary; -using SampleKeyVector = vector; +using SampleKeyVector = std::vector; using SampleKeyIndex = SampleKeyVector::size_type; struct SampleKey { private: SampleKey(const SampleKey& that) = delete; - SampleKey(SampleKeyIndex index, const string &name) : index(index), name(name) { + SampleKey(SampleKeyIndex index, const std::string &name) : index(index), name(name) { if (name.length() == 0) { throw sample_exception("Bad sample key."); } @@ -72,7 +73,7 @@ public: } const SampleKeyIndex index; - const string name; + const std::string name; }; class KeyDictionary { @@ -88,7 +89,7 @@ public: KeyDictionary& operator=(const KeyDictionary&) = delete; - SampleKey *indexOf(const string &key) { + const SampleKey *indexOf(const std::string &key) { SampleKeyIndex i = 0; for (auto ptr = keys.cbegin(); ptr != keys.cend(); ptr++, i++) { if ((*ptr)->name == key) { @@ -111,8 +112,8 @@ public: return keys.at(i); } - vector findIndexes(SampleKeyVector &keys) { - vector indexes; + std::vector findIndexes(SampleKeyVector &keys) { + std::vector indexes; for (auto &key: keys) { auto index = indexOf(key->name); @@ -152,7 +153,7 @@ private: class SampleRecord { public: - typedef vector> vec; + typedef std::vector> vec; SampleRecord(KeyDictionary &dict) : dict(dict) { } @@ -179,19 +180,19 @@ public: return values.empty(); } - const o at(const SampleKey *key) const { + const o at(const SampleKey *key) const { SampleKeyIndex index = key->index; if (index >= values.size()) { - return o(); + return o(); } return values.at(index); } SampleRecord& set(const SampleKey *key, const std::string &value) { - values.resize(max(values.size(), key->index + 1)); + values.resize(std::max(values.size(), key->index + 1)); - values.at(key->index).reset(value); + values.at(key->index) = value; return *this; } @@ -199,9 +200,9 @@ public: template const o lexical_at(const SampleKey *key) const; - string to_string() const { + std::string to_string() const { SampleKeyIndex i = 0; - string s; + std::string s; for (auto ptr = values.begin(); ptr != values.end(); ptr++, i++) { auto o = *ptr; @@ -209,7 +210,7 @@ public: continue; } - auto value = o.get(); + auto value = *o; s += dict.at(i)->name + " = " + value + ", "; } diff --git a/sensor/include/trygvis/sensor/io.h b/sensor/include/trygvis/sensor/io.h index 71b0b84..304091b 100644 --- a/sensor/include/trygvis/sensor/io.h +++ b/sensor/include/trygvis/sensor/io.h @@ -187,7 +187,7 @@ public: void write(SampleRecord const &sample) override; private: - vector keys; + vector keys; shared_ptr stream; const SampleKey *timestamp_key; }; diff --git a/sensor/main/io.cpp b/sensor/main/io.cpp index 96a98f6..9e822c3 100644 --- a/sensor/main/io.cpp +++ b/sensor/main/io.cpp @@ -45,17 +45,17 @@ unique_ptr open_sample_output_stream( auto tsf = options.find_option(); - auto timestamp_key = dict.indexOf(tsf ? tsf.get()->name : "timestamp"); + auto timestamp_key = dict.indexOf(tsf ? tsf.value()->name : "timestamp"); return make_unique(output, dict, timestamp_key, of); } else if (type == sample_format_type::SQL) { auto tno = options.find_option(); - if (!tno.is_initialized()) { + if (!tno) { throw missing_required_option_error("table name"); } - return make_unique(move(output), dict, tno.get()->name); + return make_unique(move(output), dict, tno.value()->name); } else { throw sample_exception("No writer for format type: " + to_string(type)); } @@ -144,7 +144,7 @@ void CsvSampleOutputStream::write(SampleRecord const &sample) { auto o = sample.at(sampleKey); if (o) { - s << o.get(); + s << o.value(); } } @@ -152,7 +152,7 @@ void CsvSampleOutputStream::write(SampleRecord const &sample) { } void CsvSampleOutputStream::writeHeader() { - auto &s = *stream.get(); + auto &s = *stream; auto i = dict.begin(); while (i != dict.end()) { @@ -187,7 +187,7 @@ void JsonSampleOutputStream::write(SampleRecord const &sample) { auto value = sample.at(sampleKey); if (value) { - doc[key->name] = value.get(); + doc[key->name] = value.value(); } } } else { @@ -197,7 +197,7 @@ void JsonSampleOutputStream::write(SampleRecord const &sample) { if (o) { // Make sure that the key is registered in the dictionary dict.indexOf(sampleKey->name); - doc[sampleKey->name] = o.get(); + doc[sampleKey->name] = o.value(); } } } @@ -230,7 +230,7 @@ void KeyValueSampleOutputStream::write(SampleRecord const &sample) { } else { *s << ", "; } - *s << key->name << "=" << value.get(); + *s << key->name << "=" << value.value(); } } } else { @@ -245,7 +245,7 @@ void KeyValueSampleOutputStream::write(SampleRecord const &sample) { } // Make sure that the key is registered in the dictionary dict.indexOf(sampleKey->name); - *s << sampleKey->name << "=" << o.get(); + *s << sampleKey->name << "=" << o.value(); } } } @@ -260,7 +260,7 @@ RrdSampleOutputStream::RrdSampleOutputStream(shared_ptr stream, stream(move(stream)), timestamp_key(timestamp_key) { if (output_fields) { - for (auto field : output_fields.get()->fields) { + for (auto field : output_fields.value()->fields) { keys.emplace_back(dict.indexOf(field)); } } else { @@ -284,7 +284,7 @@ void RrdSampleOutputStream::write(SampleRecord const &sample) { return; } - auto timestamp = timestampO.get(); + auto timestamp = timestampO.value(); s << timestamp; @@ -303,7 +303,7 @@ void RrdSampleOutputStream::write(SampleRecord const &sample) { s << ":"; } - s << (value ? value.get() : "U"); + s << (value ? value.value() : "U"); } s << endl << flush; @@ -319,7 +319,7 @@ void SqlSampleOutputStream::write(SampleRecord const &sample) { fs.reserve(1024); vs.reserve(1024); - auto &s = *stream.get(); + auto &s = *stream; bool first = true; if (!dict.empty()) { @@ -336,7 +336,7 @@ void SqlSampleOutputStream::write(SampleRecord const &sample) { vs += ", "; } fs += "\"" + key->name + "\""; - vs += "'" + value.get() + "'"; + vs += "'" + value.value() + "'"; } } } else { @@ -354,7 +354,7 @@ void SqlSampleOutputStream::write(SampleRecord const &sample) { dict.indexOf(sample_key->name); fs += "\"" + sample_key->name + "\""; - vs += "'" + o.get() + "'"; + vs += "'" + o.value() + "'"; } } } diff --git a/sensor/main/sensor.cpp b/sensor/main/sensor.cpp index 1c0c666..0988e0d 100644 --- a/sensor/main/sensor.cpp +++ b/sensor/main/sensor.cpp @@ -80,7 +80,7 @@ const o SampleRecord::lexical_at(const SampleKey *key) const { return o(); } - return o(boost::lexical_cast(value.get())); + return o(boost::lexical_cast(*value)); } // //template diff --git a/sensor/test/SampleTest.cpp b/sensor/test/SampleTest.cpp index 8479e75..0fffc04 100644 --- a/sensor/test/SampleTest.cpp +++ b/sensor/test/SampleTest.cpp @@ -49,13 +49,13 @@ BOOST_AUTO_TEST_CASE(key_value_parser2) { SampleRecord& sample = buffer->samples[0]; auto it = dict.begin(); BOOST_CHECK_EQUAL((*it)->name, "now"); - BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true); - BOOST_CHECK_EQUAL(sample.at(*it).get(), "1"); + BOOST_CHECK_EQUAL(!!sample.at(*it), true); + BOOST_CHECK_EQUAL(sample.at(*it).value(), "1"); BOOST_CHECK_EQUAL((*it++)->index, 0); BOOST_CHECK_EQUAL((*it)->name, "sensor 1"); - BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true); - BOOST_CHECK_EQUAL(sample.at(*it).get(), "0.000999999833333"); + BOOST_CHECK_EQUAL(!!sample.at(*it), true); + BOOST_CHECK_EQUAL(sample.at(*it).value(), "0.000999999833333"); BOOST_CHECK_EQUAL((*it++)->index, 1); } @@ -112,12 +112,12 @@ BOOST_AUTO_TEST_CASE(key_value_parser_without_newline) { SampleRecord& sample = buffer->samples[0]; auto it = dict.begin(); BOOST_CHECK_EQUAL((*it)->name, "now"); - BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true); - BOOST_CHECK_EQUAL(sample.at(*it).get(), "1"); + BOOST_CHECK_EQUAL(!!sample.at(*it), true); + BOOST_CHECK_EQUAL(sample.at(*it).value(), "1"); BOOST_CHECK_EQUAL((*it++)->index, 0); BOOST_CHECK_EQUAL((*it)->name, "sensor"); - BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true); - BOOST_CHECK_EQUAL(sample.at(*it).get(), "123"); + BOOST_CHECK_EQUAL(!!sample.at(*it), true); + BOOST_CHECK_EQUAL(sample.at(*it).value(), "123"); BOOST_CHECK_EQUAL((*it++)->index, 1); } -- cgit v1.2.3