aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-03-19 21:55:23 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-03-19 21:55:23 +0100
commit97ad73ac5c0e8955fcc0478d12075423c8023e84 (patch)
tree6f903dcf51ddca76962d895b68028d86f3c9bd5a /apps
parent17de795175d0573d8ac6c2517d0eb35e8a4f8f69 (diff)
downloadble-toys-97ad73ac5c0e8955fcc0478d12075423c8023e84.tar.gz
ble-toys-97ad73ac5c0e8955fcc0478d12075423c8023e84.tar.bz2
ble-toys-97ad73ac5c0e8955fcc0478d12075423c8023e84.tar.xz
ble-toys-97ad73ac5c0e8955fcc0478d12075423c8023e84.zip
o Fixing key-value output.
Diffstat (limited to 'apps')
-rw-r--r--apps/SoilMoistureIo.cpp20
-rw-r--r--apps/SoilMoistureIo.h28
-rw-r--r--apps/sample-timestamp.cpp9
3 files changed, 30 insertions, 27 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp
index c7f3504..a2d50e4 100644
--- a/apps/SoilMoistureIo.cpp
+++ b/apps/SoilMoistureIo.cpp
@@ -11,7 +11,7 @@ namespace soil_moisture {
using namespace std;
using json = nlohmann::json;
-void VectorSampleOutputStream::write(SampleRecord sample) {
+void VectorSampleOutputStream::write(SampleRecord const &sample) {
samples.emplace_back(sample);
}
@@ -19,7 +19,7 @@ CsvSampleOutputStream::CsvSampleOutputStream(shared_ptr<ostream> stream, KeyDict
: stream(move(stream)), headerWritten(false), dict(dict) {
}
-void CsvSampleOutputStream::write(SampleRecord sample) {
+void CsvSampleOutputStream::write(SampleRecord const &sample) {
// Skip empty records
if (sample.empty()) {
return;
@@ -28,8 +28,8 @@ void CsvSampleOutputStream::write(SampleRecord sample) {
// Build the dict with the keys from the first sample.
if (dict.empty()) {
SampleKeyIndex index = 0;
- auto ptr = sample.begin();
- while (ptr != sample.end()) {
+ auto ptr = sample.cbegin();
+ while (ptr != sample.cend()) {
auto o = *ptr;
if (o) {
@@ -88,7 +88,7 @@ JsonSampleOutputStream::JsonSampleOutputStream(shared_ptr<ostream> stream, KeyDi
dict(dict), stream(move(stream)) {
}
-void JsonSampleOutputStream::write(SampleRecord sample) {
+void JsonSampleOutputStream::write(SampleRecord const &sample) {
// Skip empty records
if (sample.empty()) {
return;
@@ -125,7 +125,7 @@ KeyValueSampleOutputStream::KeyValueSampleOutputStream(shared_ptr<ostream> strea
dict(dict), stream(move(stream)) {
}
-void KeyValueSampleOutputStream::write(SampleRecord sample) {
+void KeyValueSampleOutputStream::write(SampleRecord const &sample) {
// Skip empty records
if (sample.empty()) {
return;
@@ -146,7 +146,7 @@ void KeyValueSampleOutputStream::write(SampleRecord sample) {
} else {
s << ", ";
}
- s << key->name << "=" << value;
+ s << key->name << "=" << value.get();
}
}
} else {
@@ -173,7 +173,7 @@ SqlSampleOutputStream::SqlSampleOutputStream(shared_ptr<ostream> stream, KeyDict
dict(dict), stream(move(stream)), table_name(table_name) {
}
-void SqlSampleOutputStream::write(SampleRecord values) {
+void SqlSampleOutputStream::write(SampleRecord const &values) {
throw sample_exception("deimplemented");
// string fs, vs;
@@ -305,11 +305,11 @@ string to_string(const sample_format_type &arg) {
throw std::runtime_error("Unknown format value: " + to_string(arg));
}
-std::ostream& operator<<(std::ostream& os, sample_format_type const& type){
+std::ostream& operator<<(std::ostream& os, sample_format_type const& type) {
return os << to_string(type);
}
-std::istream& operator>>(std::istream& is, sample_format_type& type){
+std::istream& operator>>(std::istream& is, sample_format_type& type) {
string s;
is >> s;
diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h
index 3e78f46..fac518c 100644
--- a/apps/SoilMoistureIo.h
+++ b/apps/SoilMoistureIo.h
@@ -169,21 +169,21 @@ public:
}
inline
- vec::const_iterator begin() {
- return values.begin();
+ vec::const_iterator cbegin() const {
+ return values.cbegin();
}
inline
- vec::const_iterator end() {
- return values.end();
+ vec::const_iterator cend() const {
+ return values.cend();
}
inline
- bool empty() {
+ bool empty() const {
return values.empty();
}
- o<string> at(const SampleKey *key) const {
+ const o<string> at(const SampleKey *key) const {
SampleKeyIndex index = key->index;
if (index >= values.size()) {
return o<string>();
@@ -195,11 +195,11 @@ public:
void set(const SampleKey *key, const std::string &value) {
values.resize(max(values.size(), key->index + 1));
- values.at(key->index) = o<string>(value);
+ values.at(key->index).reset(value);
}
template<class A>
- const o<A> lexical_at(const SampleKey *key) {
+ const o<A> lexical_at(const SampleKey *key) const {
auto value = at(key);
if (!value) {
@@ -233,13 +233,13 @@ private:
class SampleOutputStream {
public:
- virtual void write(SampleRecord sample) = 0;
+ virtual void write(SampleRecord const &sample) = 0;
};
class VectorSampleOutputStream : public SampleOutputStream {
public:
- virtual void write(SampleRecord sample) override;
+ virtual void write(SampleRecord const &sample) override;
public:
vector<SampleRecord> samples;
@@ -249,7 +249,7 @@ class CsvSampleOutputStream : public SampleOutputStream {
public:
CsvSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict);
- void write(SampleRecord sample);
+ void write(SampleRecord const &sample);
const KeyDictionary &getDict() {
return dict;
@@ -267,7 +267,7 @@ class JsonSampleOutputStream : public SampleOutputStream {
public:
JsonSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict);
- void write(SampleRecord sample) override;
+ void write(SampleRecord const &sample) override;
private:
KeyDictionary &dict;
@@ -278,7 +278,7 @@ class KeyValueSampleOutputStream : public SampleOutputStream {
public:
KeyValueSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict);
- void write(SampleRecord sample) override;
+ void write(SampleRecord const &sample) override;
private:
KeyDictionary &dict;
@@ -289,7 +289,7 @@ class SqlSampleOutputStream : public SampleOutputStream {
public:
SqlSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict, string table_name);
- void write(SampleRecord sample) override;
+ void write(SampleRecord const &sample) override;
private:
KeyDictionary &dict;
diff --git a/apps/sample-timestamp.cpp b/apps/sample-timestamp.cpp
index 4855506..df4a89e 100644
--- a/apps/sample-timestamp.cpp
+++ b/apps/sample-timestamp.cpp
@@ -18,7 +18,7 @@ public:
timestamp_key(dict.indexOf(timestamp_name)), now_key(dict.indexOf(now_name)), start_time_(start_time), output_(output) {
}
- virtual void write(SampleRecord sample) override {
+ virtual void write(SampleRecord const &sample) override {
o<long> relative_time_o = sample.lexical_at<long>(now_key);
if (!relative_time_o) {
@@ -28,9 +28,12 @@ public:
long relative_time = relative_time_o.get();
string new_value = std::to_string(start_time_ + relative_time);
- sample.set(timestamp_key, new_value);
- output_->write(sample);
+ SampleRecord updatedSample(sample);
+
+ updatedSample.set(timestamp_key, new_value);
+
+ output_->write(updatedSample);
};
private: