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
49
50namespace drain {
51
52
53
54class Context {
55
56public:
57
58 Context(const std::string & basename = __FUNCTION__);
59
60 Context(const Context & ctx);
61
62 // Experimental
63 Script addedCommands;
64
66 const std::string basename;
67
68
69
70 virtual
71 ~Context(){
72 //if (logFileStream.is_open())
73 // logFileStream.close();
74 }
75
76
77 inline
78 long int getId() const {
79 return id;
80 }
81
82 inline
83 const std::string & getName() const {
84 return name;
85 }
86
87 Log log;
88
90
93 //std::string logFileSyntax;
94 //std::ofstream logFileStream;
95
98
100
103 virtual inline
105 return statusMap;
106 };
107
108 virtual inline
110 return statusMap;
111 };
112
113 virtual inline
114 drain::VariableMap & getUpdatedStatusMap(){
115 //Logger mout(this->log, __FILE__, __FUNCTION__);
116 //mout.attention("Base class getStatusMap");
117 updateStatus();
118 return statusMap;
119 };
120
121
122 inline
123 const Variable & getStatus(const std::string & key, bool update) const {
124 //updateStatus(update);
125 updateStatus();
126 return statusMap[key];
127 //return static_cast<T>(statusMap[key]);
128 };
129
130 // non-virtual
131
132 template <class T>
133 inline
134 void setStatus(const std::string & key, const T & value){
135 statusMap[key] = value;
136 };
137
138
140
143 virtual
144 inline
145 void report(StatusFlags & flags){
146 if (statusFlags.value > 0){
147 flags.set(statusFlags.value);
148 }
149 }
150
151 bool SCRIPT_DEFINED; // To correctly handle sequential input commands (and other script-triggering commands)
152
153protected:
154
155 const long int id;
156
157 const std::string name;
158
159 void init();
160
161 mutable
162 drain::VariableMap statusMap;
163
164 virtual
165 void updateStatus() const;
166
167private:
168
169 static long int counter;
170
171 //drain::VariableMap statusMap;
172
173};
174
175
177/*
178// See Rack resources.sh
179*/
181
182public:
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
204class SmartContext : public Context, public ContextKit {
205
206public:
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
222private:
223
224 //void linkStatusVariables();
225
226};
227
228
230//class ContextClonerBase : public ClonerBase<Context>{
231//};
232
239/*
240template <class C, class BC=Context>
241class ContextCloner : public Cloner<BC,C> {
242};
243*/
244
246
247public:
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
263 };
264
265
267 template <class C>
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
309protected:
310
311 // Note: common base class. The actual object may be a derived class.
312 Context *contextPtr;
313
314};
315
317template <class C>
319
320public:
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
335protected:
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:54
virtual const drain::VariableMap & getStatusMap() const
A long description of context variables and other resources.
Definition Context.h:104
StatusFlags statusFlags
Optional log filename (syntax): when defined, automatically opened by CommandBank::run()
Definition Context.h:97
virtual void report(StatusFlags &flags)
Report status. Typically, report final status of a thread to its base context.
Definition Context.h:145
const std::string basename
Used by copy const.
Definition Context.h:66
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
void set(const V &... args)
Set bits, accepting numeric values or keys.
Definition Flags.h:284
Definition CommandSequence.h:85
Definition Context.h:204
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:118
virtual const S & getSourceOrig() const
Returns a const reference to a default instance, in actual class.
Definition Cloner.h:229