CMS 3D CMS Logo

JsonMonitorable.h
Go to the documentation of this file.
1 /*
2  * JsonMonitorable.h
3  *
4  * Created on: Oct 29, 2012
5  * Author: aspataru
6  */
7 
8 #ifndef JSON_MONITORABLE_H
9 #define JSON_MONITORABLE_H
10 
11 #include <string>
12 #include <sstream>
13 #include <vector>
14 #include <memory>
15 //#include "EventFilter/Utilities/interface/Utils.h"
16 #include <iostream>
17 #include "json.h"
18 
19 namespace jsoncollector {
20 
23 
25  public:
27 
28  virtual ~JsonMonitorable() {}
29 
30  virtual std::string toString() const = 0;
31 
32  virtual void resetValue() = 0;
33 
34  unsigned int getUpdates() { return updates_; }
35 
36  bool getNotSame() const { return notSame_; }
37 
38  virtual void setName(std::string name) { name_ = name; }
39 
40  virtual std::string const& getName() const { return name_; }
41 
42  protected:
44  unsigned int updates_;
45  bool notSame_;
46  };
47 
48  class JsonMonPtr {
49  public:
50  JsonMonPtr() : ptr_(nullptr) {}
52  void operator=(JsonMonitorable* ptr) { ptr_ = ptr; }
54  if (ptr_)
55  delete ptr_;
56  ptr_ = nullptr;
57  }
59  JsonMonitorable* get() { return ptr_; }
60  //JsonMonPtr& operator=(JsonMonPtr& ) = delete;
61  //JsonMonPtr& operator=(JsonMonPtr&& other){ptr_=other.ptr_;return *this;}
62  private:
64  };
65 
66  class IntJ : public JsonMonitorable {
67  public:
70 
71  ~IntJ() override {}
72 
73  std::string toString() const override {
74  std::stringstream ss;
75  ss << theVar_;
76  return ss.str();
77  }
78  void resetValue() override {
79  theVar_ = 0;
80  updates_ = 0;
81  notSame_ = false;
82  }
83  void operator=(long sth) {
84  theVar_ = sth;
85  updates_ = 1;
86  notSame_ = false;
87  }
88  long& value() { return theVar_; }
89  long value() const { return theVar_; }
90 
91  void update(long sth) {
92  theVar_ = sth;
93  if (updates_ && theVar_ != sth)
94  notSame_ = true;
95  updates_++;
96  }
97 
98  void add(long sth) {
99  theVar_ += sth;
100  updates_++;
101  }
102 
103  private:
104  long theVar_;
105  };
106 
107  class DoubleJ : public JsonMonitorable {
108  public:
111 
112  ~DoubleJ() override {}
113 
114  std::string toString() const override {
115  std::stringstream ss;
116  ss << theVar_;
117  return ss.str();
118  }
119  void resetValue() override {
120  theVar_ = 0;
121  updates_ = 0;
122  notSame_ = false;
123  }
124  void operator=(double sth) {
125  theVar_ = sth;
126  updates_ = 1;
127  notSame_ = false;
128  }
129  double& value() { return theVar_; }
130  double value() const { return theVar_; }
131  void update(double sth) {
132  theVar_ = sth;
133  if (updates_ && theVar_ != sth)
134  notSame_ = true;
135  updates_++;
136  }
137 
138  private:
139  double theVar_;
140  };
141 
142  class StringJ : public JsonMonitorable {
143  public:
145  StringJ(StringJ const& sJ) : JsonMonitorable() { theVar_ = sJ.value(); }
146 
147  ~StringJ() override {}
148 
149  std::string toString() const override { return theVar_; }
150  void resetValue() override {
151  theVar_ = std::string();
152  updates_ = 0;
153  notSame_ = false;
154  }
155  void operator=(std::string sth) {
156  theVar_ = sth;
157  updates_ = 1;
158  notSame_ = false;
159  }
160  std::string& value() { return theVar_; }
161  std::string const& value() const { return theVar_; }
162  void concatenate(std::string const& added) {
163  if (!updates_)
164  theVar_ = added;
165  else
166  theVar_ += "," + added;
167  updates_++;
168  }
169  void update(std::string const& newStr) {
170  theVar_ = newStr;
171  updates_ = 1;
172  }
173 
174  private:
176  };
177 
178  //histograms filled at time intervals (later converted to full histograms)
179  template <class T>
180  class HistoJ : public JsonMonitorable {
181  public:
182  HistoJ(int expectedUpdates = 1, unsigned int maxUpdates = 0) {
183  expectedSize_ = expectedUpdates;
184  updates_ = 0;
185  maxUpdates_ = maxUpdates;
188  histo_.reserve(expectedSize_);
189  }
190  ~HistoJ() override {}
191 
192  std::string toCSV() const {
193  std::stringstream ss;
194  for (unsigned int i = 0; i < updates_; i++) {
195  ss << histo_[i];
196  if (i != histo_.size() - 1)
197  ss << ",";
198  }
199  return ss.str();
200  }
201 
202  std::string toString() const override {
203  std::stringstream ss;
204  ss << "[";
205  if (!histo_.empty())
206  for (unsigned int i = 0; i < histo_.size(); i++) {
207  ss << histo_[i];
208  if (i < histo_.size() - 1)
209  ss << ",";
210  }
211  ss << "]";
212  return ss.str();
213  }
214  virtual Json::Value toJsonValue() const { //TODO
215  Json::Value jsonValue(Json::arrayValue);
216  for (unsigned int i = 0; i < histo_.size(); i++) {
217  jsonValue.append(histo_[i]);
218  }
219  return jsonValue;
220  }
221  void resetValue() override {
222  histo_.clear();
223  histo_.reserve(expectedSize_);
224  updates_ = 0;
225  }
226  void operator=(std::vector<T> const& sth) { histo_ = sth; }
227 
228  std::vector<T>& value() { return histo_; }
229  std::vector<T> const& value() const { return histo_; }
230 
231  unsigned int getExpectedSize() const { return expectedSize_; }
232 
233  unsigned int getMaxUpdates() const { return maxUpdates_; }
234 
235  void setMaxUpdates(unsigned int maxUpdates) {
236  maxUpdates_ = maxUpdates;
237  if (!maxUpdates_)
238  return;
241  //truncate what is over the limit
242  if (maxUpdates_ && histo_.size() > maxUpdates_) {
243  histo_.resize(maxUpdates_);
244  } else
245  histo_.reserve(expectedSize_);
246  }
247 
248  unsigned int getSize() const { return histo_.size(); }
249 
250  void update(T val) {
251  if (maxUpdates_ && updates_ >= maxUpdates_)
252  return;
253  histo_.push_back(val);
254  updates_++;
255  }
256 
257  private:
258  std::vector<T> histo_;
259  unsigned int expectedSize_;
260  unsigned int maxUpdates_;
261  };
262 
263 } // namespace jsoncollector
264 
265 #endif
void setMaxUpdates(unsigned int maxUpdates)
std::string toString() const override
void operator=(std::string sth)
std::string toString() const override
void resetValue() override
StringJ(StringJ const &sJ)
std::string toCSV() const
std::string toString() const override
JsonMonitorable * ptr_
unsigned int getExpectedSize() const
void operator=(long sth)
void update(std::string const &newStr)
void operator=(std::vector< T > const &sth)
std::vector< T > & value()
std::string toString() const override
void concatenate(std::string const &added)
void add(long sth)
virtual void setName(std::string name)
unsigned int expectedSize_
unsigned int getMaxUpdates() const
std::vector< T > const & value() const
array value (ordered list)
Definition: value.h:32
void resetValue() override
void resetValue() override
JsonMonPtr(JsonMonitorable *ptr)
void operator=(double sth)
virtual std::string const & getName() const
virtual Json::Value toJsonValue() const
virtual std::string toString() const =0
Value & append(const Value &value)
Append value to array at the end.
HistoJ(int expectedUpdates=1, unsigned int maxUpdates=0)
std::string const & value() const
void resetValue() override
void update(long sth)
std::vector< T > histo_
unsigned int getSize() const
JsonMonitorable * operator->()
long double T
Represents a JSON value.
Definition: value.h:101
JSON (JavaScript Object Notation).
Definition: DataPoint.h:26
void update(double sth)
void operator=(JsonMonitorable *ptr)