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 +- 2 files changed, 23 insertions(+), 22 deletions(-) (limited to 'sensor/include') 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; }; -- cgit v1.2.3