Loading...
Searching...
No Matches
Context.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood 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
38//#include <map>
39//#include "drain/util/Debug.h"
40//#include "drain/util/ReferenceMap.h"
41
42#include "drain/util/Cloner.h"
43#include "drain/util/StatusFlags.h"
44#include "drain/util/VariableMap.h" // statusMap
45
46
47#include "CommandSequence.h"
48#include "Loop.h"
49
50
51namespace drain {
52
53
54
55class Context {
56
57public:
58
59
60 Script routine;
61 // Experimental
62
63
64 // Experimental
65 Script addedCommands;
66
67 // Experimental
68 Loop::loopStack loops;
69
70 Context(const std::string & basename = __FUNCTION__);
71
72 Context(const Context & ctx);
73
74
75 virtual
76 ~Context(){
77 //if (logFileStream.is_open())
78 // logFileStream.close();
79 }
80
81
82 inline
83 long int getId() const {
84 return id;
85 }
86
87 inline
88 const std::string & getName() const {
89 return name;
90 }
91
92 Log log;
93
95
98 //std::string logFileSyntax;
99 //std::ofstream logFileStream;
100
103
105
108 virtual inline
110 return statusMap;
111 };
112
113 virtual inline
115 return statusMap;
116 };
117
118 virtual inline
119 drain::VariableMap & getUpdatedStatusMap(){
120 //Logger mout(this->log, __FILE__, __FUNCTION__);
121 //mout.attention("Base class getStatusMap");
122 updateStatus();
123 return statusMap;
124 };
125
126
127 inline
128 const Variable & getStatus(const std::string & key, bool update) const {
129 //updateStatus(update);
130 updateStatus();
131 return statusMap[key];
132 //return static_cast<T>(statusMap[key]);
133 };
134
135 // non-virtual
136
137 template <class T>
138 inline
139 void setStatus(const std::string & key, const T & value){
140 statusMap[key] = value;
141 };
142
143
145
148 virtual
149 inline
150 void report(StatusFlags & flags){
151 if (statusFlags.value > 0){
152 flags.set(statusFlags.value);
153 }
154 }
155
156 //bool SCRIPT_DEFINED; // To correctly handle sequential input commands (and other script-triggering commands)
157
158 inline
159 bool isScriptDefined(){
160 return !routine.empty();
161 }
162
163protected:
164
166 const std::string basename;
167
168 const long int id;
169
170 const std::string name;
171
172 void init();
173
174 mutable
175 drain::VariableMap statusMap;
176
177 virtual
178 void updateStatus() const;
179
180private:
181
182 static long int counter;
183
184 //drain::VariableMap statusMap;
185
186};
187
188
190/*
191// See Rack resources.sh
192*/
194
195public:
196
197 inline
198 ContextKit() : expandVariables(false) {
199 };
200
201 // virtual drain::FlexVariableMap & getStatus();
202
203 //
204 bool expandVariables;
205
207
211 std::string formatStr;
212
213 // mutable drain::StringMapper statusFormatter;
214
215};
216
217class SmartContext : public Context, public ContextKit {
218
219public:
220
221 SmartContext(const std::string & basename = __FUNCTION__) : Context(basename){
222 //linkStatusVariables();
223 };
224
225 SmartContext(const SmartContext & ctx) : Context(ctx), ContextKit(ctx){
226 //linkStatusVariables();
227 }
228
230
233 //virtual drain::FlexVariableMap & getStatus();
234
235private:
236
237 //void linkStatusVariables();
238
239};
240
241
243//class ContextClonerBase : public ClonerBase<Context>{
244//};
245
252/*
253template <class C, class BC=Context>
254class ContextCloner : public Cloner<BC,C> {
255};
256*/
257
259
260public:
261
263 inline
264 Contextual() : contextPtr(nullptr) {};
265
267 inline
268 Contextual(const Contextual & src) : contextPtr(src.contextPtr){ // or always null?
269 //setExternalContext(src.getBaseContext()); ? not logical, consider C1 and C2(C1);
270 };
271
273 inline
276 };
277
278
280 template <class C>
282 contextPtr = & getCloner<C>().get();
283 };
284
286 inline
288 contextPtr = &ctx;
289 };
290
292 inline
293 bool contextIsSet() const {
294 return (contextPtr != nullptr);
295 };
296
298
300
303 template <class T=Context> // ,class BC> //
304 T & getContext() const { // int logLevel = LOG_WARNING
305 if (contextIsSet()){
306 return (T &)*contextPtr;
307 }
308 else {
309 //Logger mout(__FILE__, __FUNCTION__);
310 //mout.log(logLevel) << "context not set" << mout.endl;
311 return getCloner<T>().getSourceOrig();
312 }
313 }
314
315 template <class T> // ,class BC>
316 static
317 Cloner<Context,T> & getCloner(){
318 static Cloner<Context,T> cloner;
319 return cloner;
320 }
321
322protected:
323
324 // Note: common base class. The actual object may be a derived class.
325 Context *contextPtr;
326
327};
328
330template <class C>
332
333public:
334
336
337 static inline
338 C & baseCtx() {
339 return getContextCloner().getSourceOrig();
340 }
341
342 // Static?
343 static inline
344 ctx_cloner_t & getContextCloner(){
345 return getCloner<C>();
346 }
347
348protected:
349
350
351};
352
353
354} /* namespace drain */
355
356#endif /* DRAIN_CONTEXT_H_ */
357
358// Rack
Utilities.
Definition Context.h:193
std::string formatStr
Definition Context.h:211
Definition Context.h:55
virtual const drain::VariableMap & getStatusMap() const
A long description of context variables and other resources.
Definition Context.h:109
StatusFlags statusFlags
Optional log filename (syntax): when defined, automatically opened by CommandBank::run()
Definition Context.h:102
virtual void report(StatusFlags &flags)
Report status. Typically, report final status of a thread to its base context.
Definition Context.h:150
const std::string basename
Used by copy const.
Definition Context.h:166
Definition Context.h:258
Contextual(Context &ctx)
Copies internal contextPtr.
Definition Context.h:274
bool contextIsSet() const
True, if contextPtr has been set.
Definition Context.h:293
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:268
T & getContext() const
If context has been set, returns it through a cast to base class Context.
Definition Context.h:304
void setContext()
Sets internal contextPtr to the static source.
Definition Context.h:281
Contextual()
Sets internal contextPtr to NULL.
Definition Context.h:264
void setExternalContext(Context &ctx)
Sets internal contextPtr to outside target.
Definition Context.h:287
Handler for notifications sent by a Logger.
Definition Log.h:147
void set(const V &... args)
Set bits, accepting numeric values or keys.
Definition Flags.h:284
Definition CommandSequence.h:85
Definition Context.h:217
Adds class-specific convenience functions.
Definition Context.h:331
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:118
virtual const S & getSourceOrig() const
Returns a const reference to a default instance, in actual class.
Definition Cloner.h:229