Context.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 
32 #ifndef DRAIN_CONTEXT_H_
33 #define DRAIN_CONTEXT_H_
34 
35 
36 #include <drain/Log.h>
37 #include <unistd.h> // process id, PID
38 
39 //#include <map>
40 #include <set>
41 #include <fstream>
42 //#include "drain/util/Debug.h"
43 //#include "drain/util/ReferenceMap.h"
44 
45 #include "drain/util/Cloner.h"
46 
47 #include "drain/util/StatusFlags.h"
48 #include "drain/util/StringMapper.h"
49 #include "drain/util/VariableMap.h" // statusMap
50 
51 
52 
53 namespace drain {
54 
55 
56 
57 class Context {
58 
59 public:
60 
61  Context(const std::string & basename = __FUNCTION__);
62 
63  Context(const Context & ctx);
64 
65 
67  const std::string basename;
68 
69 
70 
71  virtual
72  ~Context(){
73  //if (logFileStream.is_open())
74  // logFileStream.close();
75  }
76 
77 
78  inline
79  long int getId() const {
80  return id;
81  }
82 
83  inline
84  const std::string & getName() const {
85  return name;
86  }
87 
88  Log log;
89 
91 
94  //std::string logFileSyntax;
95  //std::ofstream logFileStream;
96 
99 
101 
104  virtual inline
105  drain::VariableMap & getStatusMap(bool update=true){
106  //Logger mout(this->log, __FILE__, __FUNCTION__);
107  //mout.attention("Base class getStatusMap");
108  if (update){
109  updateStatus();
110  }
111  return statusMap;
112  };
113 
114 
115  inline
116  const Variable & getStatus(const std::string & key, bool update) const {
117  //updateStatus(update);
118  updateStatus();
119  return statusMap[key];
120  //return static_cast<T>(statusMap[key]);
121  };
122 
123  // non-virtual
124 
125  template <class T>
126  inline
127  void setStatus(const std::string & key, const T & value){
128  statusMap[key] = value;
129  };
130 
131 
133 
136  virtual
137  inline
138  void report(StatusFlags & flags){
139  if (statusFlags.value > 0){
140  flags.set(statusFlags.value);
141  }
142  }
143 
144  bool SCRIPT_DEFINED; // To correctly handle sequential input commands (and other script-triggering commands)
145 
146 
147 protected:
148 
149  const long int id;
150 
151  const std::string name;
152 
153  void init();
154 
155  mutable
156  drain::VariableMap statusMap;
157 
158  virtual
159  void updateStatus() const {
160  //void updateStatus(bool update=true) const {
161  // if (update){
162  statusMap["statusFlags"] = statusFlags.value;
163  statusMap["statusKeys"] = statusFlags.getKeys();
164  //}
165  };
166 
167 private:
168 
169  static long int counter;
170 
171 
172  //drain::VariableMap statusMap;
173 };
174 
175 
177 /*
178 // See Rack resources.sh
179 */
180 class ContextKit {
181 
182 public:
183 
184  inline
185  ContextKit() : expandVariables(false) {
186  };
187 
188  // virtual drain::FlexVariableMap & getStatus();
189 
190  //
191  bool expandVariables;
192 
194 
198  std::string formatStr;
199 
200  // mutable drain::StringMapper statusFormatter;
201 
202 };
203 
204 class SmartContext : public Context, public ContextKit {
205 
206 public:
207 
208  SmartContext(const std::string & basename = __FUNCTION__) : Context(basename){
209  //linkStatusVariables();
210  };
211 
212  SmartContext(const SmartContext & ctx) : Context(ctx), ContextKit(ctx){
213  //linkStatusVariables();
214  }
215 
217 
220  //virtual drain::FlexVariableMap & getStatus();
221 
222 private:
223 
224  //void linkStatusVariables();
225 
226 };
227 
228 
230 //class ContextClonerBase : public ClonerBase<Context>{
231 //};
232 
239 /*
240 template <class C, class BC=Context>
241 class ContextCloner : public Cloner<BC,C> {
242 };
243 */
244 
245 class Contextual {
246 
247 public:
248 
250  inline
251  Contextual() : contextPtr(nullptr) {};
252 
254  inline
255  Contextual(const Contextual & src) : contextPtr(src.contextPtr){ // or always null?
256  //setExternalContext(src.getBaseContext()); ? not logical, consider C1 and C2(C1);
257  };
258 
260  inline
262  setExternalContext(ctx);
263  };
264 
265 
267  template <class C>
268  void setContext(){
269  contextPtr = & getCloner<C>().get();
270  };
271 
273  inline
275  contextPtr = &ctx;
276  };
277 
279  inline
280  bool contextIsSet() const {
281  return (contextPtr != nullptr);
282  };
283 
285 
287 
290  template <class T=Context> // ,class BC> //
291  T & getContext() const { // int logLevel = LOG_WARNING
292  if (contextIsSet()){
293  return (T &)*contextPtr;
294  }
295  else {
296  //Logger mout(__FILE__, __FUNCTION__);
297  //mout.log(logLevel) << "context not set" << mout.endl;
298  return getCloner<T>().getSourceOrig();
299  }
300  }
301 
302  template <class T> // ,class BC>
303  static
304  Cloner<Context,T> & getCloner(){
305  static Cloner<Context,T> cloner;
306  return cloner;
307  }
308 
309 protected:
310 
311  // Note: common base class. The actual object may be a derived class.
312  Context *contextPtr;
313 
314 };
315 
317 template <class C>
319 
320 public:
321 
323 
324  static inline
325  C & baseCtx() {
326  return getContextCloner().getSourceOrig();
327  }
328 
329  // Static?
330  static inline
331  ctx_cloner_t & getContextCloner(){
332  return getCloner<C>();
333  }
334 
335 protected:
336 
337 
338 };
339 
340 
341 } /* namespace drain */
342 
343 #endif /* DRAIN_CONTEXT_H_ */
344 
345 // Rack
Utilities.
Definition: Context.h:180
std::string formatStr
Definition: Context.h:198
Definition: Context.h:57
StatusFlags statusFlags
Optional log filename (syntax): when defined, automatically opened by CommandBank::run()
Definition: Context.h:98
virtual drain::VariableMap & getStatusMap(bool update=true)
A long description of context variables and other resources.
Definition: Context.h:105
virtual void report(StatusFlags &flags)
Report status. Typically, report final status of a thread to its base context.
Definition: Context.h:138
const std::string basename
Used by copy const.
Definition: Context.h:67
Definition: Context.h:245
Contextual(Context &ctx)
Copies internal contextPtr.
Definition: Context.h:261
bool contextIsSet() const
True, if contextPtr has been set.
Definition: Context.h:280
Contextual(const Contextual &src)
Copies base context (even null) of the source. Notice that the actual instance may be of derived clas...
Definition: Context.h:255
T & getContext() const
If context has been set, returns it through a cast to base class Context.
Definition: Context.h:291
void setContext()
Sets internal contextPtr to the static source.
Definition: Context.h:268
Contextual()
Sets internal contextPtr to NULL.
Definition: Context.h:251
void setExternalContext(Context &ctx)
Sets internal contextPtr to outside target.
Definition: Context.h:274
std::string getKeys(ivalue_t value, char separator=0) const
Given an integer, retrieves dictionary keys corresponding to each index of set bits.
Definition: FlagsOld.h:250
Flagger & set(const T &arg, const TT &... args)
Set desired flags. Does not reset any flag (zero value has no effect).
Definition: FlagsOld.h:101
Definition: Context.h:204
Definition: StatusFlags.h:45
Adds class-specific convenience functions.
Definition: Context.h:318
A map of Variables.
Definition: VariableMap.h:61
Definition: DataSelector.cpp:1277
DRAIN_VARIABLE Variable
Value container supporting dynamic type.
Definition: Variable.h:63
Definition: Cloner.h:46
Wrapper for derived class S, returning base class T.
Definition: Cloner.h:117
virtual const S & getSourceOrig() const
Returns a const reference to a default instance, in actual class.
Definition: Cloner.h:228