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