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
106  return statusMap;
107  };
108 
109  virtual inline
111  return statusMap;
112  };
113 
114  virtual inline
115  drain::VariableMap & getUpdatedStatusMap(){
116  //Logger mout(this->log, __FILE__, __FUNCTION__);
117  //mout.attention("Base class getStatusMap");
118  updateStatus();
119  return statusMap;
120  };
121 
122 
123  inline
124  const Variable & getStatus(const std::string & key, bool update) const {
125  //updateStatus(update);
126  updateStatus();
127  return statusMap[key];
128  //return static_cast<T>(statusMap[key]);
129  };
130 
131  // non-virtual
132 
133  template <class T>
134  inline
135  void setStatus(const std::string & key, const T & value){
136  statusMap[key] = value;
137  };
138 
139 
141 
144  virtual
145  inline
146  void report(StatusFlags & flags){
147  if (statusFlags.value > 0){
148  flags.set(statusFlags.value);
149  }
150  }
151 
152  bool SCRIPT_DEFINED; // To correctly handle sequential input commands (and other script-triggering commands)
153 
154 protected:
155 
156  const long int id;
157 
158  const std::string name;
159 
160  void init();
161 
162  mutable
163  drain::VariableMap statusMap;
164 
165  virtual
166  void updateStatus() const {
167  //void updateStatus(bool update=true) const {
168  // if (update){
169  statusMap["statusFlags"] = statusFlags.value;
170  statusMap["statusKeys"] = statusFlags.str(); //getKeys()
171  //}
172  };
173 
174 private:
175 
176  static long int counter;
177 
178 
179  //drain::VariableMap statusMap;
180 };
181 
182 
184 /*
185 // See Rack resources.sh
186 */
187 class ContextKit {
188 
189 public:
190 
191  inline
192  ContextKit() : expandVariables(false) {
193  };
194 
195  // virtual drain::FlexVariableMap & getStatus();
196 
197  //
198  bool expandVariables;
199 
201 
205  std::string formatStr;
206 
207  // mutable drain::StringMapper statusFormatter;
208 
209 };
210 
211 class SmartContext : public Context, public ContextKit {
212 
213 public:
214 
215  SmartContext(const std::string & basename = __FUNCTION__) : Context(basename){
216  //linkStatusVariables();
217  };
218 
219  SmartContext(const SmartContext & ctx) : Context(ctx), ContextKit(ctx){
220  //linkStatusVariables();
221  }
222 
224 
227  //virtual drain::FlexVariableMap & getStatus();
228 
229 private:
230 
231  //void linkStatusVariables();
232 
233 };
234 
235 
237 //class ContextClonerBase : public ClonerBase<Context>{
238 //};
239 
246 /*
247 template <class C, class BC=Context>
248 class ContextCloner : public Cloner<BC,C> {
249 };
250 */
251 
252 class Contextual {
253 
254 public:
255 
257  inline
258  Contextual() : contextPtr(nullptr) {};
259 
261  inline
262  Contextual(const Contextual & src) : contextPtr(src.contextPtr){ // or always null?
263  //setExternalContext(src.getBaseContext()); ? not logical, consider C1 and C2(C1);
264  };
265 
267  inline
269  setExternalContext(ctx);
270  };
271 
272 
274  template <class C>
275  void setContext(){
276  contextPtr = & getCloner<C>().get();
277  };
278 
280  inline
282  contextPtr = &ctx;
283  };
284 
286  inline
287  bool contextIsSet() const {
288  return (contextPtr != nullptr);
289  };
290 
292 
294 
297  template <class T=Context> // ,class BC> //
298  T & getContext() const { // int logLevel = LOG_WARNING
299  if (contextIsSet()){
300  return (T &)*contextPtr;
301  }
302  else {
303  //Logger mout(__FILE__, __FUNCTION__);
304  //mout.log(logLevel) << "context not set" << mout.endl;
305  return getCloner<T>().getSourceOrig();
306  }
307  }
308 
309  template <class T> // ,class BC>
310  static
311  Cloner<Context,T> & getCloner(){
312  static Cloner<Context,T> cloner;
313  return cloner;
314  }
315 
316 protected:
317 
318  // Note: common base class. The actual object may be a derived class.
319  Context *contextPtr;
320 
321 };
322 
324 template <class C>
326 
327 public:
328 
330 
331  static inline
332  C & baseCtx() {
333  return getContextCloner().getSourceOrig();
334  }
335 
336  // Static?
337  static inline
338  ctx_cloner_t & getContextCloner(){
339  return getCloner<C>();
340  }
341 
342 protected:
343 
344 
345 };
346 
347 
348 } /* namespace drain */
349 
350 #endif /* DRAIN_CONTEXT_H_ */
351 
352 // Rack
Utilities.
Definition: Context.h:187
std::string formatStr
Definition: Context.h:205
Definition: Context.h:57
virtual const drain::VariableMap & getStatusMap() const
A long description of context variables and other resources.
Definition: Context.h:105
StatusFlags statusFlags
Optional log filename (syntax): when defined, automatically opened by CommandBank::run()
Definition: Context.h:98
virtual void report(StatusFlags &flags)
Report status. Typically, report final status of a thread to its base context.
Definition: Context.h:146
const std::string basename
Used by copy const.
Definition: Context.h:67
Definition: Context.h:252
Contextual(Context &ctx)
Copies internal contextPtr.
Definition: Context.h:268
bool contextIsSet() const
True, if contextPtr has been set.
Definition: Context.h:287
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:262
T & getContext() const
If context has been set, returns it through a cast to base class Context.
Definition: Context.h:298
void setContext()
Sets internal contextPtr to the static source.
Definition: Context.h:275
Contextual()
Sets internal contextPtr to NULL.
Definition: Context.h:258
void setExternalContext(Context &ctx)
Sets internal contextPtr to outside target.
Definition: Context.h:281
void set(const V &... args)
Set bits, accepting numeric values or keys.
Definition: Flags.h:277
virtual const key_t & str() const override
For exporting values.
Definition: Flags.h:294
Definition: Context.h:211
Adds class-specific convenience functions.
Definition: Context.h:325
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