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
62 // Experimental
63 Script addedCommands;
64
65 // Experimental
66 // Program addedProgram;
67
68
69 // Experimental
70 Loop::loopStack loops;
71
72 Context(const std::string & basename = __FUNCTION__);
73
74 Context(const Context & ctx);
75
76
77 virtual
78 ~Context(){
79 //if (logFileStream.is_open())
80 // logFileStream.close();
81 }
82
83
84 inline
85 long int getId() const {
86 return id;
87 }
88
89 inline
90 const std::string & getName() const {
91 return name;
92 }
93
94 Log log;
95
97
100 //std::string logFileSyntax;
101 //std::ofstream logFileStream;
102
105
107
110 virtual inline
112 return statusMap;
113 };
114
115 virtual inline
117 return statusMap;
118 };
119
120 virtual inline
121 drain::VariableMap & getUpdatedStatusMap(){
122 //Logger mout(this->log, __FILE__, __FUNCTION__);
123 //mout.attention("Base class getStatusMap");
124 updateStatus();
125 return statusMap;
126 };
127
128
129 inline
130 const Variable & getStatus(const std::string & key, bool update=true) const {
131 if (update){
132 updateStatus();
133 }
134 return statusMap[key];
135 //return static_cast<T>(statusMap[key]);
136 };
137
138 // non-virtual
139
140 template <class T>
141 inline
142 void setStatus(const std::string & key, const T & value){
143 statusMap[key] = value;
144 }
145
146
148
151 virtual
152 inline
153 void report(StatusFlags & flags){
154 if (statusFlags.value > 0){
155 flags.set(statusFlags.value);
156 }
157 }
158
159 //bool SCRIPT_DEFINED; // To correctly handle sequential input commands (and other script-triggering commands)
160
161 inline
162 bool isScriptDefined(){
163 return !routine.empty();
164 }
165
166protected:
167
169 const std::string basename;
170
171 const long int id;
172
173 const std::string name;
174
175 void init();
176
177 mutable
178 drain::VariableMap statusMap;
179
180 virtual
181 void updateStatus() const;
182
183private:
184
185 static long int counter;
186
187 //drain::VariableMap statusMap;
188
189};
190
191
193/*
194// See Rack resources.sh
195*/
197
198public:
199
200 inline
201 ContextKit() : expandVariables(false) {
202 };
203
204 // virtual drain::FlexVariableMap & getStatus();
205
206 //
207 bool expandVariables;
208
210
214 std::string formatStr;
215
216 // mutable drain::StringMapper statusFormatter;
217
218};
219
220class SmartContext : public Context, public ContextKit {
221
222public:
223
224 SmartContext(const std::string & basename = __FUNCTION__) : Context(basename){
225 //linkStatusVariables();
226 };
227
228 SmartContext(const SmartContext & ctx) : Context(ctx), ContextKit(ctx){
229 //linkStatusVariables();
230 }
231
233
236 //virtual drain::FlexVariableMap & getStatus();
237
238private:
239
240 //void linkStatusVariables();
241
242};
243
244
246//class ContextClonerBase : public ClonerBase<Context>{
247//};
248
255/*
256template <class C, class BC=Context>
257class ContextCloner : public Cloner<BC,C> {
258};
259*/
260
262
263public:
264
266 inline
267 Contextual() : contextPtr(nullptr) {};
268
270 inline
271 Contextual(const Contextual & src) : contextPtr(src.contextPtr){ // or always null?
272 //setExternalContext(src.getBaseContext()); ? not logical, consider C1 and C2(C1);
273 };
274
276 inline
279 };
280
281
283 template <class C>
285 contextPtr = & getCloner<C>().get();
286 }
287
289 inline
291 contextPtr = &ctx;
292 };
293
295 inline
296 bool contextIsSet() const {
297 return (contextPtr != nullptr);
298 };
299
301
303
306 template <class T=Context> // ,class BC> //
307 T & getContext() const { // int logLevel = LOG_WARNING
308 if (contextIsSet()){
309 return (T &)*contextPtr;
310 }
311 else {
312 //Logger mout(__FILE__, __FUNCTION__);
313 //mout.log(logLevel) << "context not set" << mout.endl;
314 return getCloner<T>().getSourceOrig();
315 }
316 }
317
319 template <class T> // ,class BC>
320 static
322 static Cloner<Context,T> cloner;
323 return cloner;
324 }
325
326protected:
327
328 // Note: common base class. The actual object may be a derived class.
330
331};
332
334template <class C>
336
337public:
338
340
341 static inline
342 C & baseCtx() {
343 return getContextCloner().getSourceOrig();
344 }
345
346 // Static?
347 static inline
348 ctx_cloner_t & getContextCloner(){
349 return getCloner<C>();
350 }
351
352protected:
353
354
355};
356
357
358} /* namespace drain */
359
360#endif /* DRAIN_CONTEXT_H_ */
361
362// Rack
Utilities.
Definition Context.h:196
std::string formatStr
Definition Context.h:214
Definition Context.h:55
virtual const drain::VariableMap & getStatusMap() const
A long description of context variables and other resources.
Definition Context.h:111
StatusFlags statusFlags
Optional log filename (syntax): when defined, automatically opened by CommandBank::run()
Definition Context.h:104
virtual void report(StatusFlags &flags)
Report status. Typically, report final status of a thread to its base context.
Definition Context.h:153
const std::string basename
Used by copy const.
Definition Context.h:169
Definition Context.h:261
Contextual(Context &ctx)
Copies internal contextPtr.
Definition Context.h:277
bool contextIsSet() const
True, if contextPtr has been set.
Definition Context.h:296
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:271
T & getContext() const
If context has been set, returns it through a cast to base class Context.
Definition Context.h:307
Context * contextPtr
Pointer to the current context; nullptr if not set.
Definition Context.h:329
static Cloner< Context, T > & getCloner()
Returns the object cloner.
Definition Context.h:321
void setContext()
Sets internal contextPtr to the static source.
Definition Context.h:284
Contextual()
Sets internal contextPtr to NULL.
Definition Context.h:267
void setExternalContext(Context &ctx)
Sets internal contextPtr to outside target.
Definition Context.h:290
Handler for notifications sent by a Logger.
Definition Log.h:159
void set(const V &... args)
Set bits, accepting numeric values or keys.
Definition Flags.h:293
Definition CommandSequence.h:85
Definition Context.h:220
Adds class-specific convenience functions.
Definition Context.h:335
A map of Variables.
Definition VariableMap.h:61
Definition Data.h:1473
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