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 "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
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 /*
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
121template <>
122inline
123std::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
138class FlexVariableMap : public SmartMap<FlexibleVariable> {
139
140public:
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
154
159 inline
160 FlexVariableMap(std::initializer_list<Variable::init_pair_t > inits) : SmartMap<FlexibleVariable>(','){
161 for (const auto & entry: inits){
162 (*this)[entry.first] = entry.second;
163 }
164 };
165
166
167 inline
168 FlexVariableMap & operator=(const FlexVariableMap & v){
169 importMap(v);
170 return *this;
171 }
172
173
174 template <class T>
175 inline
176 FlexVariableMap & link(const std::string &key, T & target){
177 (*this)[key].link(target);
178 return *this;
179 };
180
181 inline
182 void clearVariables(){
183 iterator it = this->begin();
184 while (true){
185 if (!it->second.isLinking()){
186 iterator dit = it;
187 ++it;
188 this->erase(dit);
189 }
190 else {
191 ++it;
192 }
193
194 if (it == this->end()){
195 break;
196 }
197
198 };
199 }
200
201};
202
203
204template <>
205inline
206std::ostream & Sprinter::toStream(std::ostream & ostr, const FlexVariableMap & vmap, const SprinterLayout & layout){
207
208 if (&layout == &UNSET_LAYOUT){
209 ostr << "UNSET_LAYOUT:";
211 }
212 else {
213 return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
214 }
215
216 // return Sprinter::sequenceToStream(ostr, vmap.getMap(), layout.mapChars, layout);
217 // return Sprinter::mapToStream(ostr, vmap.getMap(), layout, vmap.getKeys());
218}
219
220
221// std::ostream &operator<<(std::ostream &ostr, const VariableMap &m);
222inline
223std::ostream & operator<<(std::ostream &ostr, const FlexVariableMap & vmap){
224 return Sprinter::sequenceToStream(ostr, vmap.getMap(), Sprinter::jsonLayout.mapChars, Sprinter::jsonLayout);
225}
226
227} // drain
228
229#endif /* VARIABLE_H_ */
230
231
A map of FlexVariable:s.
Definition VariableMap.h:138
FlexVariableMap(std::initializer_list< Variable::init_pair_t > inits)
Constructor for initializer lists.
Definition VariableMap.h:160
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 & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:420
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 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