BeanLike.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
48 
49 namespace drain
50 {
51 
52 
54 
60 class BeanLike {
61 
62 public:
63 
64  virtual inline
65  ~BeanLike(){};
66 
68  virtual inline
69  const std::string & getName() const { return name; };
70 
71 
73  virtual inline
74  const std::string & getDescription() const {
75  return description;
76  };
77 
79  inline
80  bool hasParameters() const {
81  return !parameters.empty();
82  };
83 
85  template <class F>
86  F getParameter(const std::string & p) const {
87  if (parameters.hasKey(p))
88  return parameters[p];
89  else {
90  throw std::runtime_error(p + ": no such parameter (BeanLike::getParameter)");
91  }
92  }
93 
94  inline
95  const ReferenceMap & getParameters() const { return parameters; };
96 
97  inline
98  ReferenceMap & getParameters() { return parameters; };
99 
100 
101  template <class F>
102  inline
103  void setParametersFromEntries(const F & args){
104 
105  ReferenceMap & parameters = getParameters();
106  std::stringstream sstr;
107  char separator = 0;
108  for (const auto & entry: args){
109  parameters[entry.first] = entry.second;
110  // setParameter(entry.first, entry.second);
111  if (separator)
112  sstr << separator;
113  else
114  separator = ',';
115  sstr << entry.first << '=' << entry.second;
116  }
117  storeLastArguments(sstr.str());
118 
119  updateBean();
120 
121  }
122 
124  /*
125  inline
126  void shareParameters(ReferenceMap & rmap) {
127  return rmap.append(parameters);
128  };
129  */
130  inline
131  void setParameters(std::initializer_list<Variable::init_pair_t > args){
132 
133  setParametersFromEntries(args);
134 
135  /*
136  ReferenceMap & parameters = getParameters();
137  std::stringstream sstr;
138  char separator = 0;
139  for (const auto & entry: args){
140  parameters[entry.first] = entry.second;
141  // setParameter(entry.first, entry.second);
142  if (separator)
143  sstr << separator;
144  else
145  separator = ',';
146  sstr << entry.first << '=' << entry.second;
147  }
148  storeLastArguments(sstr.str());
149  */
150 
151  /*
152  for (const auto & entry: args){
153  setParameter(entry.first, entry.second);
154  //parameters[entry.first] = entry.second;
155  }
156  */
157  }
158 
160 
163  virtual inline
164  void setParameters(const std::string &p, char assignmentSymbol='=', char separatorSymbol=0){
165  parameters.setValues(p, assignmentSymbol, separatorSymbol);
166  updateBean();
167  storeLastArguments(p); // experimental
168  };
169 
171 
173  template <class T>
174  inline
175  void setParameters(const std::map<std::string,T> & args){
176  setParametersFromEntries(args);
177  /*
178  parameters.importMap(p);
179  updateBean();
180  */
181  }
182 
184 
186  template <class T>
187  inline
188  void setParameters(const SmartMap<T> & args){ // NEEDED?
189  setParametersFromEntries(args);
190  /*
191  parameters.importCastableMap(p);
192  updateBean();
193  */
194  }
195 
197  inline
198  void setParameter(const std::string &p, const Castable & value){
199  parameters[p].assignCastable(value);
200  updateBean();
201  storeLastArguments(StringBuilder<'='>(p, value)); // experimental
202  }
203 
206  template <class T>
207  inline
208  void setParameter(const std::string &p, const VariableT<T> & value){
209  parameters[p].assignCastable(value);
210  updateBean();
211  storeLastArguments(StringBuilder<'='>(p, value)); // experimental
212  }
213 
215  /*
216  inline
217  void setParameter(const std::string &p, const Reference & value){
218  parameters[p].assignCastable(value);
219  updateBean();
220  }
221  */
222 
224  template <class F>
225  inline
226  void setParameter(const std::string &p, const F &value){
227  parameters[p] = value;
228  updateBean();
229  // storeLastArguments(p); // experimental
231  }
232 
234  template <class F>
235  inline
236  void setParameter(const std::string &p, std::initializer_list<F> value){
237  parameters[p] = value;
238  updateBean();
239  // develop storeLastParameters(StringBuilder<'='>(p, value)); // experimental
240  }
241 
242 
243 
244  inline
245  BeanLike & operator=(const BeanLike & b){ //
246  parameters.importMap(b.getParameters()); // 2021
247  updateBean(); // ADDED 2021/10
248  return *this;
249  }
250 
251 
252  virtual
253  std::ostream & toStream(std::ostream & ostr, bool compact = true) const;
254 
255  /*
256  virtual inline
257  std::ostream & toStream(std::ostream & ostr) const {
258  //ostr << name << ':' << parameters;
259  ostr << name << ": " << description << '\n';
260  ostr << '\t' << parameters << '\n';
261  return ostr;
262  }
263  */
264 
265 // 2024 public:
266  BeanLike(const BeanLike & b) : name(b.name), description(b.description){
267  // copy(b);
268  parameters.copyStruct(b.getParameters(), b, *this, ReferenceMap::RESERVE);
269  }
270 
271 
272  BeanLike(const std::string & name, const std::string & description="") : name(name), description(description) {
273  }
274 
275 protected:
276 
277 
278 
279  const std::string name; // todo separate (Beanlet)
280 
281  const std::string description; // todo separate (Beanlet)
282 
283  ReferenceMap parameters; // todo separate (Beanlet)
284 
286  virtual inline
287  void storeLastArguments(const std::string & p){};
288 
290  virtual inline
291  void updateBean() const {};
292 
293 
294 
295 };
296 
297 
298 inline
299 std::ostream & operator<<(std::ostream &ostr, const BeanLike & bean){
300  bean.toStream(ostr);
301  return ostr;
302 }
303 
304 }
305 
306 
307 #endif
308 
309 // Drain
Something which has a name, a description and possibly some parameters of varying type.
Definition: BeanLike.h:60
F getParameter(const std::string &p) const
Gets a single parameter.
Definition: BeanLike.h:86
void setParameter(const std::string &p, const VariableT< T > &value)
Definition: BeanLike.h:208
void setParameters(const std::map< std::string, T > &args)
Set parameters.
Definition: BeanLike.h:175
void setParameter(const std::string &p, std::initializer_list< F > value)
Sets a single parameter.
Definition: BeanLike.h:236
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:164
void setParameters(std::initializer_list< Variable::init_pair_t > args)
Grants access to (if above hidden)
Definition: BeanLike.h:131
void setParameters(const SmartMap< T > &args)
Set parameters.
Definition: BeanLike.h:188
void setParameter(const std::string &p, const F &value)
Sets a single parameter.
Definition: BeanLike.h:226
void setParameter(const std::string &p, const Castable &value)
Sets a single parameter.
Definition: BeanLike.h:198
virtual void storeLastArguments(const std::string &p)
Called after setParameters()
Definition: BeanLike.h:287
virtual const std::string & getName() const
Return the name of an instance.
Definition: BeanLike.h:69
virtual const std::string & getDescription() const
Return a brief description.
Definition: BeanLike.h:74
virtual void updateBean() const
Called after setParameters()
Definition: BeanLike.h:291
Definition: Castable.h:76
Definition: ReferenceMap.h:207
@ 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