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 <vector>
38#include <iostream>
39#include <stdexcept>
40#include <map>
41
42#include <drain/Log.h>
43#include <drain/StringBuilder.h>
44//#include <drain/VariableAssign.h>
45// #include "Variable.h"
46#include "ReferenceMap.h"
47//#include "VariableMap.h"
48
49namespace drain
50{
51
52
54
60class BeanLike {
61
62public:
63
64 // typedef FlexVariableMap map_t;
65 typedef ReferenceMap map_t;
66
67 // 2024 public:
68 BeanLike(const BeanLike & b) : name(b.name), description(b.description){
69 // copy(b);
70 parameters.copyStruct(b.getParameters(), b, *this, ReferenceMap::RESERVE);
71 }
72
73
74 BeanLike(const std::string & name, const std::string & description="") : name(name), description(description) {
75 }
76
77 virtual inline
78 ~BeanLike(){};
79
81 virtual inline
82 const std::string & getName() const { return name; };
83
84
86 virtual inline
87 const std::string & getDescription() const {
88 return description;
89 };
90
92 inline
93 bool hasParameters() const {
94 return !parameters.empty();
95 };
96
98 template <class F>
99 F getParameter(const std::string & p) const {
100 if (parameters.hasKey(p))
101 return parameters[p];
102 else {
103 throw std::runtime_error(p + ": no such parameter (BeanLike::getParameter)");
104 }
105 }
106
107 inline
108 const map_t & getParameters() const { return parameters; };
109
110 inline
111 map_t & getParameters() { return parameters; };
112
113
114 template <class F>
115 inline
116 void setParametersFromEntries(const F & args){
117
118 map_t & parameters = getParameters();
119 std::stringstream sstr;
120 char separator = 0;
121 for (const auto & entry: args){
122 parameters[entry.first] = entry.second;
123 // setParameter(entry.first, entry.second);
124 if (separator)
125 sstr << separator;
126 else
127 separator = ',';
128 sstr << entry.first << '=' << entry.second;
129 }
130 storeLastArguments(sstr.str());
131
132 updateBean();
133
134 }
135
137 /*
138 inline
139 void shareParameters(ReferenceMap & rmap) {
140 return rmap.append(parameters);
141 };
142 */
143 inline
144 void setParameters(std::initializer_list<Variable::init_pair_t > args){
145
146 setParametersFromEntries(args);
147
148 /*
149 ReferenceMap & parameters = getParameters();
150 std::stringstream sstr;
151 char separator = 0;
152 for (const auto & entry: args){
153 parameters[entry.first] = entry.second;
154 // setParameter(entry.first, entry.second);
155 if (separator)
156 sstr << separator;
157 else
158 separator = ',';
159 sstr << entry.first << '=' << entry.second;
160 }
161 storeLastArguments(sstr.str());
162 */
163
164 /*
165 for (const auto & entry: args){
166 setParameter(entry.first, entry.second);
167 //parameters[entry.first] = entry.second;
168 }
169 */
170 }
171
173
176 virtual inline
177 void setParameters(const std::string &p, char assignmentSymbol='=', char separatorSymbol=0){
178 parameters.setValues(p, assignmentSymbol, separatorSymbol);
179 updateBean();
180 storeLastArguments(p); // experimental
181 };
182
184
186 template <class T>
187 inline
188 void setParameters(const std::map<std::string,T> & args){
189 setParametersFromEntries(args);
190 /*
191 parameters.importMap(p);
192 updateBean();
193 */
194 }
195
197
199 template <class T>
200 inline
201 void setParameters(const SmartMap<T> & args){ // NEEDED?
202 setParametersFromEntries(args);
203 /*
204 parameters.importCastableMap(p);
205 updateBean();
206 */
207 }
208
210 inline
211 void setParameter(const std::string &p, const Castable & value){
212 parameters[p].assignCastable(value);
213 updateBean();
214 storeLastArguments(StringBuilder<'='>(p, value)); // experimental
215 }
216
219 template <class T>
220 inline
221 void setParameter(const std::string &p, const VariableT<T> & value){
222 parameters[p].assignCastable(value);
223 updateBean();
224 storeLastArguments(StringBuilder<'='>(p, value)); // experimental
225 }
226
228 /*
229 inline
230 void setParameter(const std::string &p, const Reference & value){
231 parameters[p].assignCastable(value);
232 updateBean();
233 }
234 */
235
237 template <class F>
238 inline
239 void setParameter(const std::string &p, const F &value){
240 parameters[p] = value;
241 updateBean();
242 // storeLastArguments(p); // experimental
243 storeLastArguments(StringBuilder<'='>(p, value));
244 }
245
247 template <class F>
248 inline
249 void setParameter(const std::string &p, std::initializer_list<F> value){
250 parameters[p] = value;
251 updateBean();
252 // develop storeLastParameters(StringBuilder<'='>(p, value)); // experimental
253 }
254
255
256
257 inline
258 BeanLike & operator=(const BeanLike & b){ //
259 parameters.importMap(b.getParameters()); // 2021
260 updateBean(); // ADDED 2021/10
261 return *this;
262 }
263
264
265 virtual
266 std::ostream & toStream(std::ostream & ostr, bool compact = true) const;
267
268 /*
269 virtual inline
270 std::ostream & toStream(std::ostream & ostr) const {
271 //ostr << name << ':' << parameters;
272 ostr << name << ": " << description << '\n';
273 ostr << '\t' << parameters << '\n';
274 return ostr;
275 }
276 */
277
278
279protected:
280
281
282
283 const std::string name; // todo separate (Beanlet)
284
285 const std::string description; // todo separate (Beanlet)
286
287 //ReferenceMap parameters; // todo separate (Beanlet)
288 map_t parameters; // todo separate (Beanlet)?
289
291 virtual inline
292 void storeLastArguments(const std::string & p){};
293
295 virtual inline
296 void updateBean() const {};
297
298
299
300};
301
302
303inline
304std::ostream & operator<<(std::ostream &ostr, const BeanLike & bean){
305 bean.toStream(ostr);
306 return ostr;
307}
308
309}
310
311
312#endif
313
314// Drain
Something which has a name, a description and possibly some parameters of varying type.
Definition BeanLike.h:60
virtual const std::string & getName() const
Return the name of an instance.
Definition BeanLike.h:82
virtual const std::string & getDescription() const
Return a brief description.
Definition BeanLike.h:87
F getParameter(const std::string &p) const
Gets a single parameter.
Definition BeanLike.h:99
void setParameter(const std::string &p, const VariableT< T > &value)
Definition BeanLike.h:221
void setParameters(const std::map< std::string, T > &args)
Set parameters.
Definition BeanLike.h:188
void setParameter(const std::string &p, std::initializer_list< F > value)
Sets a single parameter.
Definition BeanLike.h:249
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:177
void setParameters(std::initializer_list< Variable::init_pair_t > args)
Grants access to (if above hidden)
Definition BeanLike.h:144
void setParameters(const SmartMap< T > &args)
Set parameters.
Definition BeanLike.h:201
void setParameter(const std::string &p, const F &value)
Sets a single parameter.
Definition BeanLike.h:239
void setParameter(const std::string &p, const Castable &value)
Sets a single parameter.
Definition BeanLike.h:211
virtual void storeLastArguments(const std::string &p)
Called after setParameters()
Definition BeanLike.h:292
virtual void updateBean() const
Called after setParameters()
Definition BeanLike.h:296
Definition Castable.h:76
Definition ReferenceMap.h:207
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:399
@ RESERVE
Definition ReferenceMap.h:385
A base class for smart maps providing methods for importing and exporting values, among others.
Definition SmartMap.h:66
Definition StringBuilder.h:58
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Definition DataSelector.cpp:1277