VariableMap.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 
32 #ifndef DRAIN_VARIABLEMAP_H
33 #define DRAIN_VARIABLEMAP_H
34 
35 //#include <string.h>
36 #include <string>
37 //#include <vector>
38 #include <map>
39 
40 //#include "Castable.h"
41 //#include "CastableIterator.h"
42 //#include "String.h"
43 
44 /*
45 #include "Variable.h" // <- FlexVariable
46 #include "FlexibleVariable.h"
47 */
48 
49 #include <drain/Variable.h> // <- FlexVariable
50 #include <drain/FlexibleVariable.h>
51 
52 #include "SmartMap.h"
53 
54 #ifndef DRAIN_SMAP_NAME
55 #define DRAIN_SMAP_NAME VariableMap
56 #endif
57 
58 namespace drain {
59 
61 class VariableMap : public SmartMap<Variable> {
62 
63 public:
64 
66  inline
68  };
69 
70 
72  inline
74  importMap(v);
75  };
76 
78 
83  inline
84  VariableMap(std::initializer_list<Variable::init_pair_t > inits) : SmartMap<Variable>(','){
85  //importMap(v);
86  for (const auto & entry: inits){
87  (*this)[entry.first] = entry.second;
88  }
89  };
90 
92 
95  inline
96  VariableMap(Variable::init_pair_t & entry) : SmartMap<Variable>(','){
97  (*this)[entry.first] = entry.second;
98  };
99 
100  /*
101  inline
102  VariableMap & operator=(const VariableMap & v){
103  importMap(v);
104  return *this;
105  }
106  */
107 
108  template <class T>
109  inline
110  VariableMap & operator=(const std::map<std::string,T> & m){
111  if (&m != this->getMap()){
112  importMap(m); // CastableMap?
113  }
114  return *this;
115  }
116 
117 
118 };
119 
120 
121 template <>
122 inline
123 std::ostream & Sprinter::toStream(std::ostream & ostr, const VariableMap & vmap, const SprinterLayout & layout){
124 
125  if (&layout == &UNSET_LAYOUT){
126  return Sprinter::sequenceToStream(ostr, vmap, Sprinter::jsonLayout.mapChars, jsonLayout);
127  }
128  else {
129  return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
130  }
131 
132  // return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
133  // return Sprinter::mapToStream(ostr, vmap.getMap(), layout, vmap.getKeys());
134 }
135 
137 
138 class FlexVariableMap : public SmartMap<FlexibleVariable> {
139 
140 public:
141 
142  inline
144  };
145 
147  //Does not try to create references (links), ie does not copy pointers even if input has referencing entries.
148  inline
150  importMap(m);
151  };
152 
153  inline
154  FlexVariableMap & operator=(const FlexVariableMap & v){
155  importMap(v);
156  return *this;
157  }
158 
159 
160  template <class T>
161  inline
162  FlexVariableMap & link(const std::string &key, T & target){
163  (*this)[key].link(target);
164  return *this;
165  };
166 
167  inline
168  void clearVariables(){
169  iterator it = this->begin();
170  while (true){
171  if (!it->second.isLinking()){
172  iterator dit = it;
173  ++it;
174  this->erase(dit);
175  }
176  else {
177  ++it;
178  }
179 
180  if (it == this->end()){
181  break;
182  }
183 
184  };
185  }
186 
187 };
188 
189 
190 template <>
191 inline
192 std::ostream & Sprinter::toStream(std::ostream & ostr, const FlexVariableMap & vmap, const SprinterLayout & layout){
193 
194  if (&layout == &UNSET_LAYOUT){
195  ostr << "UNSET_LAYOUT:";
197  }
198  else {
199  return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
200  }
201 
202  // return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
203  // return Sprinter::mapToStream(ostr, vmap.getMap(), layout, vmap.getKeys());
204 }
205 
206 
207 // std::ostream &operator<<(std::ostream &ostr, const VariableMap &m);
208 
209 } // drain
210 
211 #endif /* VARIABLE_H_ */
212 
213 
A map of FlexVariable:s.
Definition: VariableMap.h:138
FlexVariableMap(const FlexVariableMap &m)
Copies a map like VariableMap does - creates an own entry for every input entry.
Definition: VariableMap.h:149
A base class for smart maps providing methods for importing and exporting values, among others.
Definition: SmartMap.h:66
map_t::iterator iterator
Needed?
Definition: SmartMap.h:80
char separator
Default character used for splitting input and output. See setValues.
Definition: SmartMap.h:85
const map_t & getMap() const
Definition: SmartMap.h:206
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition: SmartMap.h:252
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const SprinterLayout &layout)
Convenience: if sequence type (array, list, set, map) not given, assume array.
Definition: Sprinter.h:321
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition: Sprinter.h:420
static const SprinterLayout UNSET_LAYOUT
Marker for unset layout.
Definition: Sprinter.h:200
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition: Sprinter.h:221
A map of Variables.
Definition: VariableMap.h:61
VariableMap(char separator=',')
Default constructor.
Definition: VariableMap.h:67
VariableMap(std::initializer_list< Variable::init_pair_t > inits)
Constructor for initializer lists.
Definition: VariableMap.h:84
VariableMap(const VariableMap &v)
Copy constructor.
Definition: VariableMap.h:73
VariableMap(Variable::init_pair_t &entry)
Experimental constructor.
Definition: VariableMap.h:96
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition: VariableT.h:87
Definition: DataSelector.cpp:1277
DRAIN_VARIABLE Variable
Value container supporting dynamic type.
Definition: Variable.h:63