Loading...
Searching...
No Matches
BeanLike.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#ifndef DRAIN_BEANLIKE_H_
32#define DRAIN_BEANLIKE_H_
33
34//
35#include <cmath>
36
37#include <iostream>
38#include <stdexcept>
39#include <map>
40
41#include <drain/StringBuilder.h>
42//#include <drain/VariableAssign.h>
43// #include "Variable.h"
44#include "ReferenceMap.h"
45//#include "VariableMap.h"
46
47namespace drain
48{
49
50
52
58class BeanLike {
59
60public:
61
62 // typedef FlexVariableMap map_t;
65
67
70 // 2024 public:
72 // copy(b);
74 }
75
77
81 BeanLike(const std::string & name, const std::string & description="") : name(name), description(description) {
82 }
83
84 virtual inline
85 ~BeanLike(){};
86
88 virtual inline
89 const std::string & getName() const { return name; };
90
91
93 virtual inline
94 const std::string & getDescription() const {
95 return description;
96 };
97
99 inline
100 bool hasParameters() const {
101 return !parameters.empty();
102 };
103
105 template <class F>
106 F getParameter(const std::string & p) const {
107 if (parameters.hasKey(p))
108 return parameters[p];
109 else {
110 throw std::runtime_error(p + ": no such parameter (BeanLike::getParameter)");
111 }
112 }
113
115 inline
116 const map_t & getParameters() const { return parameters; };
117
119 inline
121
123
127 template <class F>
128 inline
129 void setParametersFromEntries(const F & args){
130
132 std::stringstream sstr;
133 char separator = 0;
134 for (const auto & entry: args){
135 parameters[entry.first] = entry.second;
136 // setParameter(entry.first, entry.second);
137 if (separator)
138 sstr << separator;
139 else
140 separator = ',';
141 sstr << entry.first << '=' << entry.second;
142 }
143 storeLastArguments(sstr.str());
144
145 updateBean();
146
147 }
148
150 /*
151 inline
152 void shareParameters(ReferenceMap & rmap) {
153 return rmap.append(parameters);
154 };
155 */
156 inline
157 void setParameters(std::initializer_list<Variable::init_pair_t > args){
158
160
161 /*
162 ReferenceMap & parameters = getParameters();
163 std::stringstream sstr;
164 char separator = 0;
165 for (const auto & entry: args){
166 parameters[entry.first] = entry.second;
167 // setParameter(entry.first, entry.second);
168 if (separator)
169 sstr << separator;
170 else
171 separator = ',';
172 sstr << entry.first << '=' << entry.second;
173 }
174 storeLastArguments(sstr.str());
175 */
176
177 /*
178 for (const auto & entry: args){
179 setParameter(entry.first, entry.second);
180 //parameters[entry.first] = entry.second;
181 }
182 */
183 }
184
186
189 virtual inline
190 void setParameters(const std::string &p, char assignmentSymbol='=', char separatorSymbol=0){
191 parameters.setValues(p, assignmentSymbol, separatorSymbol);
192 updateBean();
193 storeLastArguments(p); // experimental
194 };
195
197
199 template <class T>
200 inline
201 void setParameters(const std::map<std::string,T> & args){
203 /*
204 parameters.importMap(p);
205 updateBean();
206 */
207 }
208
210
212 template <class T>
213 inline
214 void setParameters(const SmartMap<T> & args){ // NEEDED?
216 /*
217 parameters.importCastableMap(p);
218 updateBean();
219 */
220 }
221
223 inline
224 void setParameter(const std::string &p, const Castable & value){
225 parameters[p].assignCastable(value);
226 updateBean();
227 storeLastArguments(StringBuilder<'='>(p, value)); // experimental
228 }
229
232 template <class T>
233 inline
234 void setParameter(const std::string &p, const VariableT<T> & value){
235 parameters[p].assignCastable(value);
236 updateBean();
237 storeLastArguments(StringBuilder<'='>(p, value)); // experimental
238 }
239
241 /*
242 inline
243 void setParameter(const std::string &p, const Reference & value){
244 parameters[p].assignCastable(value);
245 updateBean();
246 }
247 */
248
250 template <class F>
251 inline
252 void setParameter(const std::string &p, const F &value){
253 parameters[p] = value;
254 updateBean();
255 // storeLastArguments(p); // experimental
256 storeLastArguments(StringBuilder<'='>(p, value));
257 }
258
260 template <class F>
261 inline
262 void setParameter(const std::string &p, std::initializer_list<F> value){
263 parameters[p] = value;
264 updateBean();
265 // develop storeLastParameters(StringBuilder<'='>(p, value)); // experimental
266 }
267
268
269
271
274 inline
275 BeanLike & operator=(const BeanLike & b){ //
277 updateBean(); // ADDED 2021/10
278 return *this;
279 }
280
281
283
287 virtual
288 std::ostream & toStream(std::ostream & ostr, bool compact = true) const;
289
290 /*
291 virtual inline
292 std::ostream & toStream(std::ostream & ostr) const {
293 //ostr << name << ':' << parameters;
294 ostr << name << ": " << description << '\n';
295 ostr << '\t' << parameters << '\n';
296 return ostr;
297 }
298 */
299
300
301protected:
302
303
304
306 const std::string name; // todo separate (Beanlet)
307
309 const std::string description; // todo separate (Beanlet)
310
312 //ReferenceMap parameters; // todo separate (Beanlet)
313 map_t parameters; // todo separate (Beanlet)?
314
316 virtual inline
317 void storeLastArguments(const std::string & p){};
318
320 virtual inline
321 void updateBean() const {};
322
323
324
325};
326
327
328inline
329std::ostream & operator<<(std::ostream &ostr, const BeanLike & bean){
330 bean.toStream(ostr);
331 return ostr;
332}
333
334}
335
336
337#endif
338
339// Drain
Something which has a name, a description and possibly some parameters of varying type.
Definition BeanLike.h:58
const std::string description
Brief, human-readable description of this bean.
Definition BeanLike.h:309
map_t parameters
Parameters (name -> linked variable), typically linked to member variables of the derived class.
Definition BeanLike.h:313
ReferenceMap map_t
Reference map type used to store this bean's parameters (name -> linked variable).
Definition BeanLike.h:64
BeanLike(const std::string &name, const std::string &description="")
Constructor.
Definition BeanLike.h:81
virtual const std::string & getName() const
Return the name of an instance.
Definition BeanLike.h:89
virtual const std::string & getDescription() const
Return a brief description.
Definition BeanLike.h:94
F getParameter(const std::string &p) const
Gets a single parameter.
Definition BeanLike.h:106
bool hasParameters() const
Tells if any parameters have been linked (registered) for this bean.
Definition BeanLike.h:100
BeanLike(const BeanLike &b)
Copy constructor. Re-links parameters to the member variables of this (new) instance.
Definition BeanLike.h:71
BeanLike & operator=(const BeanLike &b)
Assigns the parameter values of another bean (by key) to this one's own parameters.
Definition BeanLike.h:275
void setParameter(const std::string &p, const VariableT< T > &value)
Definition BeanLike.h:234
void setParameters(const std::map< std::string, T > &args)
Set parameters.
Definition BeanLike.h:201
virtual std::ostream & toStream(std::ostream &ostr, bool compact=true) const
Prints the name, description and current parameter values.
Definition BeanLike.cpp:37
void setParameter(const std::string &p, std::initializer_list< F > value)
Sets a single parameter.
Definition BeanLike.h:262
virtual void setParameters(const std::string &p, char assignmentSymbol='=', char separatorSymbol=0)
Sets comma-separated parameters in a predetermined order "a,b,c" or by specifing them "b=2".
Definition BeanLike.h:190
void setParameters(std::initializer_list< Variable::init_pair_t > args)
Grants access to (if above hidden)
Definition BeanLike.h:157
map_t & getParameters()
Returns the parameter map.
Definition BeanLike.h:120
void setParameters(const SmartMap< T > &args)
Set parameters.
Definition BeanLike.h:214
void setParameter(const std::string &p, const F &value)
Sets a single parameter.
Definition BeanLike.h:252
void setParametersFromEntries(const F &args)
Sets several parameters at once from a sequence of key-value pairs.
Definition BeanLike.h:129
const map_t & getParameters() const
Returns the parameter map (const access).
Definition BeanLike.h:116
void setParameter(const std::string &p, const Castable &value)
Sets a single parameter.
Definition BeanLike.h:224
const std::string name
Name of this bean, typically also the name of the (derived) class.
Definition BeanLike.h:306
virtual void storeLastArguments(const std::string &p)
Called after setParameters()
Definition BeanLike.h:317
virtual void updateBean() const
Called after setParameters()
Definition BeanLike.h:321
Definition Castable.h:76
Definition ReferenceMap.h:215
void copyStruct(const ReferenceMap &m, const T &src, T &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition ReferenceMap.h:412
@ RESERVE
Definition ReferenceMap.h:398
A base class for smart maps providing methods for importing and exporting values, among others.
Definition SmartMap.h:62
void setValues(const std::string &entries, char assignmentChar='=', char separatorChar=0)
Sets values. If strictness==STRICTLY_CLOSED, throws exception if tries to assign a non-existing entry...
Definition SmartMap.h:299
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:253
Definition StringBuilder.h:58
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Definition Data.h:1473