12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #include "PublisherData.h"
- #include "instreamOperator.h"
- #include <sstream>
- PublisherData::PublisherData(const int _publisherID, PublisherType pubType, float _value) :
- publisherID(_publisherID), publisherType(pubType),time_generated(system_clock::now()), values(std::vector<float>())
- {
- values.push_back(_value);
- }
-
- PublisherData::PublisherData(const int _publisherID, PublisherType pubType, std::vector<float> _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 << " <type: " << typeid(v).name() << "> " << ": " << v << std::endl;
- //}
- //return ss.str();
- }
-
- std::istream& operator>>(std::istream& is, char const* pat) {
-
- char ch;
- while (isspace(static_cast<unsigned char>(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<seconds>(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;)");
- }
|