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#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
53namespace drain {
54
55
56
57class Context {
58
59public:
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
154protected:
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
168private:
169
170 static long int counter;
171
172 //drain::VariableMap statusMap;
173
174};
175
176
178/*
179// See Rack resources.sh
180*/
182
183public:
184
185 inline
186 ContextKit() : expandVariables(false) {
187 };
188
189 // virtual drain::FlexVariableMap & getStatus();
190
191 //
192 bool expandVariables;
193
195
199 std::string formatStr;
200
201 // mutable drain::StringMapper statusFormatter;
202
203};
204
205class SmartContext : public Context, public ContextKit {
206
207public:
208
209 SmartContext(const std::string & basename = __FUNCTION__) : Context(basename){
210 //linkStatusVariables();
211 };
212
213 SmartContext(const SmartContext & ctx) : Context(ctx), ContextKit(ctx){
214 //linkStatusVariables();
215 }
216
218
221 //virtual drain::FlexVariableMap & getStatus();
222
223private:
224
225 //void linkStatusVariables();
226
227};
228
229
231//class ContextClonerBase : public ClonerBase<Context>{
232//};
233
240/*
241template <class C, class BC=Context>
242class ContextCloner : public Cloner<BC,C> {
243};
244*/
245
247
248public:
249
251 inline
252 Contextual() : contextPtr(nullptr) {};
253
255 inline
256 Contextual(const Contextual & src) : contextPtr(src.contextPtr){ // or always null?
257 //setExternalContext(src.getBaseContext()); ? not logical, consider C1 and C2(C1);
258 };
259
261 inline
264 };
265
266
268 template <class C>
270 contextPtr = & getCloner<C>().get();
271 };
272
274 inline
276 contextPtr = &ctx;
277 };
278
280 inline
281 bool contextIsSet() const {
282 return (contextPtr != nullptr);
283 };
284
286
288
291 template <class T=Context> // ,class BC> //
292 T & getContext() const { // int logLevel = LOG_WARNING
293 if (contextIsSet()){
294 return (T &)*contextPtr;
295 }
296 else {
297 //Logger mout(__FILE__, __FUNCTION__);
298 //mout.log(logLevel) << "context not set" << mout.endl;
299 return getCloner<T>().getSourceOrig();
300 }
301 }
302
303 template <class T> // ,class BC>
304 static
305 Cloner<Context,T> & getCloner(){
306 static Cloner<Context,T> cloner;
307 return cloner;
308 }
309
310protected:
311
312 // Note: common base class. The actual object may be a derived class.
313 Context *contextPtr;
314
315};
316
318template <class C>
320
321public:
322
324
325 static inline
326 C & baseCtx() {
327 return getContextCloner().getSourceOrig();
328 }
329
330 // Static?
331 static inline
332 ctx_cloner_t & getContextCloner(){
333 return getCloner<C>();
334 }
335
336protected:
337
338
339};
340
341
342} /* namespace drain */
343
344#endif /* DRAIN_CONTEXT_H_ */
345
346// Rack
Utilities.
Definition Context.h:181
std::string formatStr
Definition Context.h:199
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:246
Contextual(Context &ctx)
Copies internal contextPtr.
Definition Context.h:262
bool contextIsSet() const
True, if contextPtr has been set.
Definition Context.h:281
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:256
T & getContext() const
If context has been set, returns it through a cast to base class Context.
Definition Context.h:292
void setContext()
Sets internal contextPtr to the static source.
Definition Context.h:269
Contextual()
Sets internal contextPtr to NULL.
Definition Context.h:252
void setExternalContext(Context &ctx)
Sets internal contextPtr to outside target.
Definition Context.h:275
void set(const V &... args)
Set bits, accepting numeric values or keys.
Definition Flags.h:287
Definition Context.h:205
Adds class-specific convenience functions.
Definition Context.h:319
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