#include "PublisherData.h" #include "instreamOperator.h" #include PublisherData::PublisherData(const int _publisherID, PublisherType pubType, float _value) : publisherID(_publisherID), publisherType(pubType),time_generated(system_clock::now()), values(std::vector()) { values.push_back(_value); } PublisherData::PublisherData(const int _publisherID, PublisherType pubType, std::vector _values) : publisherID(_publisherID), publisherType(pubType), time_generated(system_clock::now()), values(_values) { } PublisherData::PublisherData() : publisherID(0), publisherType(PublisherType::NA), time_generated(system_clock::now()), values({ 0.0 }) { } std::string PublisherData::toString() const{ //auto timeNow = system_clock::to_time_t(time_generated); //char timestring[26]; //ctime_s(timestring, sizeof timestring, &timeNow); //std::stringstream ss; //ss << "PublisherData from ID: " << publisherID << " at " << timestring; //unsigned int i = 0; //for (auto v : values) { // ss << " Value " << ++i << " " << ": " << v << std::endl; //} //return ss.str(); } std::istream& operator>>(std::istream& is, char const* pat) { char ch; while (isspace(static_cast(is.peek()))) is.get(ch); while (*pat && is && *pat == is.peek() && is.get(ch)) { ++pat; } // if we didn't reach the end of the pattern, matching failed (mismatch, premature EOF, etc.) if (*pat) { is.setstate(std::ios::failbit); } return is; } std::ostream& operator<<(std::ostream& os, const PublisherData& data){ //auto timeNow = system_clock::to_time_t(data.time_generated); //char timestring[26]; //ctime_s(timestring, sizeof timestring, &timeNow); os << duration_cast(data.time_generated.time_since_epoch()).count() << "_"; for (unsigned int i = 0; i < data.values.size()-1; i++) { os << data.values[i] << ","; } os << data.values[data.values.size()-1] << ";"; return os; } std::istream& operator>>(std::istream& is, PublisherData& data) { //size_t time; float value; is >> value; data.values.clear(); data.values.push_back(value); //data.time_generated = std::chrono::system_clock::from_time_t(time); while (is.peek() == ',') { is >> "," >> value; data.values.push_back(value); } if (is.peek() == ';') { is.get(); return is; } else throw std::invalid_argument("Stream object has invalid pattern (should be %d:%f,...,%f;)"); }