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#include "drain/util/StatusFlags.h"
47#include "drain/util/StringMapper.h"
48#include "drain/util/VariableMap.h" // statusMap
49
50
51#include "CommandSequence.h"
52
53
54namespace drain {
55
56
57
58class Context {
59
60public:
61
62 Context(const std::string & basename = __FUNCTION__);
63
64 Context(const Context & ctx);
65
66 // Experimental
67 Script addedCommands;
68
70 const std::string basename;
71
72
73
74 virtual
75 ~Context(){
76 //if (logFileStream.is_open())
77 // logFileStream.close();
78 }
79
80
81 inline
82 long int getId() const {
83 return id;
84 }
85
86 inline
87 const std::string & getName() const {
88 return name;
89 }
90
91 Log log;
92
94
97 //std::string logFileSyntax;
98 //std::ofstream logFileStream;
99
102
104
107 virtual inline
109 return statusMap;
110 };
111
112 virtual inline
114 return statusMap;
115 };
116
117 virtual inline
118 drain::VariableMap & getUpdatedStatusMap(){
119 //Logger mout(this->log, __FILE__, __FUNCTION__);
120 //mout.attention("Base class getStatusMap");
121 updateStatus();
122 return statusMap;
123 };
124
125
126 inline
127 const Variable & getStatus(const std::string & key, bool update) const {
128 //updateStatus(update);
129 updateStatus();
130 return statusMap[key];
131 //return static_cast<T>(statusMap[key]);
132 };
133
134 // non-virtual
135
136 template <class T>
137 inline
138 void setStatus(const std::string & key, const T & value){
139 statusMap[key] = value;
140 };
141
142
144
147 virtual
148 inline
149 void report(StatusFlags & flags){
150 if (statusFlags.value > 0){
151 flags.set(statusFlags.value);
152 }
153 }
154
155 bool SCRIPT_DEFINED; // To correctly handle sequential input commands (and other script-triggering commands)
156
157protected:
158
159 const long int id;
160
161 const std::string name;
162
163 void init();
164
165 mutable
166 drain::VariableMap statusMap;
167
168 virtual
169 void updateStatus() const;
170
171private:
172
173 static long int counter;
174
175 //drain::VariableMap statusMap;
176
177};
178
179
181/*
182// See Rack resources.sh
183*/
185
186public:
187
188 inline
189 ContextKit() : expandVariables(false) {
190 };
191
192 // virtual drain::FlexVariableMap & getStatus();
193
194 //
195 bool expandVariables;
196
198
202 std::string formatStr;
203
204 // mutable drain::StringMapper statusFormatter;
205
206};
207
208class SmartContext : public Context, public ContextKit {
209
210public:
211
212 SmartContext(const std::string & basename = __FUNCTION__) : Context(basename){
213 //linkStatusVariables();
214 };
215
216 SmartContext(const SmartContext & ctx) : Context(ctx), ContextKit(ctx){
217 //linkStatusVariables();
218 }
219
221
224 //virtual drain::FlexVariableMap & getStatus();
225
226private:
227
228 //void linkStatusVariables();
229
230};
231
232
234//class ContextClonerBase : public ClonerBase<Context>{
235//};
236
243/*
244template <class C, class BC=Context>
245class ContextCloner : public Cloner<BC,C> {
246};
247*/
248
250
251public:
252
254 inline
255 Contextual() : contextPtr(nullptr) {};
256
258 inline
259 Contextual(const Contextual & src) : contextPtr(src.contextPtr){ // or always null?
260 //setExternalContext(src.getBaseContext()); ? not logical, consider C1 and C2(C1);
261 };
262
264 inline
267 };
268
269
271 template <class C>
273 contextPtr = & getCloner<C>().get();
274 };
275
277 inline
279 contextPtr = &ctx;
280 };
281
283 inline
284 bool contextIsSet() const {
285 return (contextPtr != nullptr);
286 };
287
289
291
294 template <class T=Context> // ,class BC> //
295 T & getContext() const { // int logLevel = LOG_WARNING
296 if (contextIsSet()){
297 return (T &)*contextPtr;
298 }
299 else {
300 //Logger mout(__FILE__, __FUNCTION__);
301 //mout.log(logLevel) << "context not set" << mout.endl;
302 return getCloner<T>().getSourceOrig();
303 }
304 }
305
306 template <class T> // ,class BC>
307 static
308 Cloner<Context,T> & getCloner(){
309 static Cloner<Context,T> cloner;
310 return cloner;
311 }
312
313protected:
314
315 // Note: common base class. The actual object may be a derived class.
316 Context *contextPtr;
317
318};
319
321template <class C>
323
324public:
325
327
328 static inline
329 C & baseCtx() {
330 return getContextCloner().getSourceOrig();
331 }
332
333 // Static?
334 static inline
335 ctx_cloner_t & getContextCloner(){
336 return getCloner<C>();
337 }
338
339protected:
340
341
342};
343
344
345} /* namespace drain */
346
347#endif /* DRAIN_CONTEXT_H_ */
348
349// Rack
Utilities.
Definition Context.h:184
std::string formatStr
Definition Context.h:202
Definition Context.h:58
virtual const drain::VariableMap & getStatusMap() const
A long description of context variables and other resources.
Definition Context.h:108
StatusFlags statusFlags
Optional log filename (syntax): when defined, automatically opened by CommandBank::run()
Definition Context.h:101
virtual void report(StatusFlags &flags)
Report status. Typically, report final status of a thread to its base context.
Definition Context.h:149
const std::string basename
Used by copy const.
Definition Context.h:70
Definition Context.h:249
Contextual(Context &ctx)
Copies internal contextPtr.
Definition Context.h:265
bool contextIsSet() const
True, if contextPtr has been set.
Definition Context.h:284
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:259
T & getContext() const
If context has been set, returns it through a cast to base class Context.
Definition Context.h:295
void setContext()
Sets internal contextPtr to the static source.
Definition Context.h:272
Contextual()
Sets internal contextPtr to NULL.
Definition Context.h:255
void setExternalContext(Context &ctx)
Sets internal contextPtr to outside target.
Definition Context.h:278
void set(const V &... args)
Set bits, accepting numeric values or keys.
Definition Flags.h:291
Definition CommandSequence.h:85
Definition Context.h:208
Adds class-specific convenience functions.
Definition Context.h:322
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