Commit 7556fa84 authored by Alessio Netti's avatar Alessio Netti
Browse files

Operator field now uses String instead of Vector in SensorMetadata class

- Complies with the rest of the interface in libdcdb
parent ea1ab2b2
......@@ -83,11 +83,11 @@ DCDB::PublicSensor Configuration::metadataToPublicSensor(const SensorMetadata& s
ps.scaling_factor = sm.scale;
ps.ttl = sm.ttl;
ps.interval = sm.interval;
ps.operations = sm.operations;
uint64_t sensorMask = 0;
if(sm.integrable)
sensorMask = sensorMask | INTEGRABLE;
if(sm.monotonic)
sensorMask = sensorMask | MONOTONIC;
ps.operations = sm.getOperationsString();
return ps;
}
......@@ -53,7 +53,8 @@ public:
unit(""),
scale(1.0),
ttl(0),
interval(0) {}
interval(0),
operations("") {}
SensorMetadata(const SensorMetadata& other) {
this->isVirtual = other.isVirtual;
......@@ -120,7 +121,7 @@ public:
} else if (boost::iequals(val.first, "ttl")) {
this->ttl = stoull(val.second.data()) * 1000000;
} else if (boost::iequals(val.first, "operations")) {
_parseVector(val.second.data(), this->operations);
this->operations = _sanitizeOperations(val.second.data());
}
}
}
......@@ -149,17 +150,6 @@ public:
return config;
}
/**
* @brief Returns a string representation of the internal vector of sensor operations.
*
* @return A comma-separated string of sensor operations
*/
string getOperationsString() const {
string out="";
_dumpVector(out, this->operations);
return out;
}
// Public class members
bool isVirtual;
bool integrable;
......@@ -169,13 +159,16 @@ public:
double scale;
uint64_t ttl;
uint64_t interval;
vector<string> operations;
string operations;
protected:
// Parses a string and splits it according to a separator
void _parseVector(const string& str, vector<string>& v, const char sep=',') {
v.clear();
// Parses a operations string and sanitizes it from excess whitespace
string _sanitizeOperations(const string& str, const char sep=',') {
vector<string> v;
string out="";
// We split the string into the comma-separated tokens
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, sep)) {
......@@ -184,12 +177,19 @@ protected:
v.push_back(token);
}
}
// We re-write the vector into a string, this time properly formatted
string sepStr = string(1,sep);
for(const auto& el : v)
out += el + sepStr;
if(!out.empty() && out.back() == sep)
out.erase(out.size()-1, 1);
return out;
}
// Dumps the contents of "s" in "config"
void _dumpPTREE(boost::property_tree::ptree& config) const {
string ops="";
_dumpVector(ops, this->operations);
config.clear();
config.push_back(boost::property_tree::ptree::value_type("isVirtual", boost::property_tree::ptree(this->isVirtual ? "true" : "false")));
config.push_back(boost::property_tree::ptree::value_type("monotonic", boost::property_tree::ptree(this->monotonic ? "true" : "false")));
......@@ -199,17 +199,7 @@ protected:
config.push_back(boost::property_tree::ptree::value_type("scale", boost::property_tree::ptree(to_string(this->scale))));
config.push_back(boost::property_tree::ptree::value_type("interval", boost::property_tree::ptree(to_string(this->interval / 1000000))));
config.push_back(boost::property_tree::ptree::value_type("ttl", boost::property_tree::ptree(to_string(this->ttl / 1000000))));
config.push_back(boost::property_tree::ptree::value_type("operations", boost::property_tree::ptree(ops)));
}
// Dumps the content of a vector into a string with an arbitrary separator
void _dumpVector(string& str, const vector<string>& v, const char sep=',') const {
str = "";
string sepStr = string(1,sep);
for(const auto& el : v)
str += el + sepStr;
if(!str.empty() && str.back() == sep)
str.erase(str.size()-1, 1);
config.push_back(boost::property_tree::ptree::value_type("operations", boost::property_tree::ptree(this->operations)));
}
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment