Loading...
Searching...
No Matches
VariableMap.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
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 "StringTools.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
58namespace drain {
59
61class VariableMap : public SmartMap<Variable> {
62
63public:
64
66 inline
69
70
72 inline
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 // TODO: CHECK: -Wdeprecated-copy fires because the copy constructor above is user-declared
101 // but operator=(const VariableMap&) is not (it was tried and disabled below); the implicit
102 // copy-assignment operator does a plain memberwise/base copy (via std::map's operator=,
103 // copying items), whereas SmartMap's own copy constructor deliberately "does not copy items"
104 // (see SmartMap.h) and this class's copy constructor instead calls importMap(v) explicitly.
105 // Confirm whether copy-assignment should match the copy constructor's behavior.
106 /*
107 inline
108 VariableMap & operator=(const VariableMap & v){
109 importMap(v);
110 return *this;
111 }
112 */
113
114 template <class T>
115 inline
116 VariableMap & operator=(const std::map<std::string,T> & m){
117 if (&m != this->getMap()){
118 importMap(m); // CastableMap?
119 }
120 return *this;
121 }
122
123
124};
125
126
127template <>
128inline
129std::ostream & Sprinter::toStream(std::ostream & ostr, const VariableMap & vmap, const SprinterLayout & layout){
130
131 if (&layout == &UNSET_LAYOUT){
132 return Sprinter::sequenceToStream(ostr, vmap, Sprinter::jsonLayout.mapChars, jsonLayout);
133 }
134 else {
135 return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
136 }
137
138 // return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
139 // return Sprinter::mapToStream(ostr, vmap.getMap(), layout, vmap.getKeys());
140}
141
143
144class FlexVariableMap : public SmartMap<FlexibleVariable> {
145
146public:
147
148 inline
150 };
151
153 //Does not try to create references (links), ie does not copy pointers even if input has referencing entries.
154 inline
156 importMap(m);
157 };
158
160
165 inline
166 FlexVariableMap(std::initializer_list<Variable::init_pair_t > inits) : SmartMap<FlexibleVariable>(','){
167 for (const auto & entry: inits){
168 (*this)[entry.first] = entry.second;
169 }
170 };
171
172
173 inline
174 FlexVariableMap & operator=(const FlexVariableMap & v){
175 importMap(v);
176 return *this;
177 }
178
179
180 template <class T>
181 inline
182 FlexVariableMap & link(const std::string &key, T & target){
183 (*this)[key].link(target);
184 return *this;
185 }
186
187 inline
188 void clearVariables(){
189 iterator it = this->begin();
190 while (true){
191 if (!it->second.isLinking()){
192 iterator dit = it;
193 ++it;
194 this->erase(dit);
195 }
196 else {
197 ++it;
198 }
199
200 if (it == this->end()){
201 break;
202 }
203
204 };
205 }
206
207};
208
209
210template <>
211inline
212std::ostream & Sprinter::toStream(std::ostream & ostr, const FlexVariableMap & vmap, const SprinterLayout & layout){
213
214 if (&layout == &UNSET_LAYOUT){
215 ostr << "UNSET_LAYOUT:";
217 }
218 else {
219 return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
220 }
221
222 // return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
223 // return Sprinter::mapToStream(ostr, vmap.getMap(), layout, vmap.getKeys());
224}
225
226
227// std::ostream &operator<<(std::ostream &ostr, const VariableMap &m);
228inline
229std::ostream & operator<<(std::ostream &ostr, const FlexVariableMap & vmap){
230 return Sprinter::sequenceToStream(ostr, vmap.getMap(), Sprinter::jsonLayout.mapChars, Sprinter::jsonLayout);
231}
232
233} // drain
234
235#endif /* VARIABLE_H_ */
236
237
A map of FlexVariable:s.
Definition VariableMap.h:144
FlexVariableMap(std::initializer_list< Variable::init_pair_t > inits)
Constructor for initializer lists.
Definition VariableMap.h:166
FlexVariableMap(const FlexVariableMap &m)
Copies a map like VariableMap does - creates an own entry for every input entry.
Definition VariableMap.h:155
A base class for smart maps providing methods for importing and exporting values, among others.
Definition SmartMap.h:62
map_t::iterator iterator
Needed?
Definition SmartMap.h:76
char separator
Default character used for splitting input and output. See setValues.
Definition SmartMap.h:81
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:253
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:423
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:324
static const SprinterLayout UNSET_LAYOUT
Marker for unset layout.
Definition Sprinter.h:203
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition Sprinter.h:224
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 Data.h:1473
DRAIN_VARIABLE Variable
Value container supporting dynamic type.
Definition Variable.h:63
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631