CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
18 namespace jsoncollector {
19 
22 
24 
25 public:
26 
28 
29  virtual ~JsonMonitorable() {}
30 
31  virtual std::string toString() const = 0;
32 
33  virtual void resetValue() = 0;
34 
35  unsigned int getUpdates() {return updates_;}
36 
37  bool getNotSame() {return notSame_;}
38 
39  virtual void setName(std::string name) {
40  name_=name;
41  }
42 
43  virtual std::string & getName() {
44  return name_;
45  }
46 
47 
48 
49 protected:
51  unsigned int updates_;
52  bool notSame_;
53 };
54 
55 class JsonMonPtr {
56 public:
59  void operator=(JsonMonitorable* ptr ){ptr_=ptr;}
60  ~JsonMonPtr() {if (ptr_) delete ptr_;ptr_=nullptr;}
62  JsonMonitorable* get() {return ptr_;}
63  //JsonMonPtr& operator=(JsonMonPtr& ) = delete;
64  //JsonMonPtr& operator=(JsonMonPtr&& other){ptr_=other.ptr_;return *this;}
65 private:
67 };
68 
69 
70 
71 
72 class IntJ: public JsonMonitorable {
73 
74 public:
76  IntJ(long val) : JsonMonitorable(), theVar_(val) {}
77 
78  virtual ~IntJ() {}
79 
80  virtual std::string toString() const {
81  std::stringstream ss;
82  ss << theVar_;
83  return ss.str();
84  }
85  virtual void resetValue() {
86  theVar_=0;
87  updates_=0;
88  notSame_=0;
89  }
90  void operator=(long sth) {
91  theVar_ = sth;
92  updates_=1;
93  notSame_=0;
94  }
95  long & value() {
96  return theVar_;
97  }
98 
99  void update(long sth) {
100  theVar_=sth;
101  if (updates_ && theVar_!=sth) notSame_=true;
102  updates_++;
103  }
104 
105  void add(long sth) {
106  theVar_+=sth;
107  updates_++;
108  }
109 
110 private:
111  long theVar_;
112 };
113 
114 
115 class DoubleJ: public JsonMonitorable {
116 
117 public:
119  DoubleJ(double val) : JsonMonitorable(), theVar_(val) {}
120 
121  virtual ~DoubleJ() {}
122 
123  virtual std::string toString() const {
124  std::stringstream ss;
125  ss << theVar_;
126  return ss.str();
127  }
128  virtual void resetValue() {
129  theVar_=0;
130  updates_=0;
131  notSame_=0;
132  }
133  void operator=(double sth) {
134  theVar_ = sth;
135  updates_=1;
136  notSame_=0;
137  }
138  double & value() {
139  return theVar_;
140  }
141  void update(double sth) {
142  theVar_=sth;
143  if (updates_ && theVar_!=sth) notSame_=true;
144  updates_++;
145  }
146 
147 private:
148  double theVar_;
149 };
150 
151 
152 class StringJ: public JsonMonitorable {
153 
154 public:
156 
157  virtual ~StringJ() {}
158 
159  virtual std::string toString() const {
160  return theVar_;
161  }
162  virtual void resetValue() {
164  updates_ = 0;
165  notSame_=0;
166  }
167  void operator=(std::string sth) {
168  theVar_ = sth;
169  updates_=1;
170  notSame_=0;
171  }
173  return theVar_;
174  }
175  void concatenate(std::string const& added) {
176  if (!updates_)
177  theVar_=added;
178  else
179  theVar_+=","+added;
180  updates_++;
181  }
182  void update(std::string const& newStr) {
183  theVar_=newStr;
184  updates_=1;
185  }
186 
187 private:
189 };
190 
191 //histograms filled at time intervals (later converted to full histograms)
192 template<class T> class HistoJ: public JsonMonitorable {
193 
194 public:
195  HistoJ( int expectedUpdates = 1 , unsigned int maxUpdates = 0 ){
196  expectedSize_=expectedUpdates;
197  updates_ = 0;
198  maxUpdates_ = maxUpdates;
200  histo_.reserve(expectedSize_);
201  }
202  virtual ~HistoJ() {}
203 
204  std::string toCSV() const {
205  std::stringstream ss;
206  for (unsigned int i=0;i<updates_;i++) {
207  ss << histo_[i];
208  if (i!=histo_.size()-1) ss<<",";
209  }
210  return ss.str();
211  }
212 
213  virtual std::string toString() const {
214  std::stringstream ss;
215  ss << "[";
216  if (histo_.size())
217  for (unsigned int i = 0; i < histo_.size(); i++) {
218  ss << histo_[i];
219  if (i<histo_.size()-1) ss << ",";
220  }
221  ss << "]";
222  return ss.str();
223  }
224  virtual void resetValue() {
225  histo_.clear();
226  histo_.reserve(expectedSize_);
227  updates_=0;
228  }
229  void operator=(std::vector<T> & sth) {
230  histo_ = sth;
231  }
232 
233  std::vector<T> & value() {
234  return histo_;
235  }
236 
237  unsigned int getExpectedSize() {
238  return expectedSize_;
239  }
240 
241  unsigned int getMaxUpdates() {
242  return maxUpdates_;
243  }
244 
245  void setMaxUpdates(unsigned int maxUpdates) {
246  maxUpdates_=maxUpdates;
247  if (!maxUpdates_) return;
249  //truncate what is over the limit
250  if (maxUpdates_ && histo_.size()>maxUpdates_) {
251  histo_.resize(maxUpdates_);
252  }
253  else histo_.reserve(expectedSize_);
254  }
255 
256  unsigned int getSize() {
257  return histo_.size();
258  }
259 
260  void update(T val) {
261  if (maxUpdates_ && updates_>=maxUpdates_) return;
262  histo_.push_back(val);
263  updates_++;
264  }
265 
266 private:
267  std::vector<T> histo_;
268  unsigned int expectedSize_;
269  unsigned int maxUpdates_;
270 };
271 
272 }
273 
274 #endif
void setMaxUpdates(unsigned int maxUpdates)
std::string toCSV() const
int i
Definition: DBlmapReader.cc:9
void operator=(std::string sth)
virtual void resetValue()
virtual std::string & getName()
JsonMonitorable * ptr_
void operator=(long sth)
#define nullptr
void update(std::string const &newStr)
virtual std::string toString() const
unsigned int getExpectedSize()
void operator=(std::vector< T > &sth)
std::vector< T > & value()
void concatenate(std::string const &added)
void add(long sth)
virtual std::string toString() const
virtual void setName(std::string name)
unsigned int expectedSize_
unsigned int getSize()
virtual std::string toString() const
JsonMonPtr(JsonMonitorable *ptr)
void operator=(double sth)
virtual void resetValue()
virtual void resetValue()
virtual std::string toString() const =0
virtual std::string toString() const
HistoJ(int expectedUpdates=1, unsigned int maxUpdates=0)
void update(long sth)
std::vector< T > histo_
JsonMonitorable * operator->()
volatile std::atomic< bool > shutdown_flag false
long double T
virtual void resetValue()
unsigned int getMaxUpdates()
void update(double sth)
void operator=(JsonMonitorable *ptr)