Loading...
Searching...
No Matches
ReferenceMap.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//#include <typeinfo>
32#include <drain/Log.h>
33#include <stdexcept>
34//#include <iostream>
35//#include <vector>
36#include <string>
37
38#include <map>
39#include <list>
40
41
42#include <drain/Reference.h>
43#include <drain/StringTools.h>
44
45#include "Range.h"
46// #include "FlexibleVariable.h"
47//#include "ReferenceVariable.h"
48
49#include "SmartMap.h"
50
51
52#ifndef REFERENCE_MAP
53#define REFERENCE_MAP
54
55
56namespace drain {
57
59
68template <class T=Reference>
69class ReferenceMap2 : public SmartMap<T> {
70
71public:
72
73 typedef T ref_t;
74 typedef SmartMap<T> map_t;
75
76
78
82 template <class F>
83 inline
84 ref_t & link(const std::string & key, F &x){
85
86 ref_t & r = (*this)[key];
87 r.link(x); // .setSeparator(this->arraySeparator);
88 return r;
89
90 }
91
93 inline
94 ref_t & link(const std::string & key, void *ptr, const std::type_info &type, size_t count=1){
95
96 ref_t & r = (*this)[key];
97 r.link(ptr, type, count);
98 return r;
99
100 /*
101 if (this->find(key) == this->end()) // not already referenced
102 this->keyList.push_back(key);
103
104 Reference & r = map_t::operator[](key);
105 r.setSeparator(this->arraySeparator); // applicable, if array type element
106 r.link(ptr, type, count);
107 // unitMap[key] = unit;
108 return r;
109 */
110 }
111
112 inline
113 void unlink(const std::string & key){
114 map_t::erase(key);
115 for (std::list<std::string>::iterator it = this->keyList.begin(); it != this->keyList.end(); ++it){
116 if (*it == key){
117 this->keyList.erase(it);
118 break;
119 }
120 }
121 //unitMap.erase(key);
122 }
123
124
134
136
143 template <class T2>
144 //static // Start with T. Todo: consider other
145 void copyStruct(const ReferenceMap2<T> & m, const T2 & src, T2 & dst, extLinkPolicy policy=RESERVE) {
146 //, bool copyValues = true, bool linkExternal = false){
147
148 Logger mout(__FILE__, __FUNCTION__);
149 long s = sizeof(T2); // yes signed
150 //mout.warn("experimental, obj.size=" , s );
151
152 // Clearing is bad, it resets work of base class constructors
153 // if (policy==LINK)
154 // clear();
155
156 typedef unsigned long addr_t;
157 typedef long addr_diff_t;
158
159 const addr_t srcAddr = (addr_t)(&src);
160 const addr_t dstAddr = (addr_t)(&dst);
161 //for (std::list<std::string>::const_iterator it = m.getKeyList().begin(); it != m.getKeyList().end(); ++it){
162 for (const std::string & key: m.getKeyList()){
163 // const std::string & key = *it;
164 const ref_t & srcRef = m[key];
165 addr_t srcVarAddr = (addr_t)srcRef.getPtr();
166 addr_diff_t relativeAddr = srcVarAddr - srcAddr;
167 if ((relativeAddr >= 0) && (relativeAddr < s)){ // INTERNAL
168 //Reference & dstMemberRef = (*this)[key];
169 ref_t & dstMemberRef = link(key, (void *)(dstAddr + relativeAddr), srcRef.getType(), srcRef.getElementCount());
170 //mout.warn("local: " , key , ':' , srcRef.getElementCount() );
171 dstMemberRef.copyFormat(srcRef);
172 dstMemberRef.assignCastable(srcRef); // value
173 }
174 else {
175 //mout.warn("external: " , key );
176 switch (policy) {
177 case LINK:
178 link(key, (void *)srcVarAddr, srcRef.getType(), srcRef.getElementCount()).copyFormat(srcRef);
179 break;
180 case RESERVE:
181 // reserve(key);
182 (*this)[key];
183 //mout.warn("reserved: " , key );
184 //mout.warn("keyList: " , getKeys() );
185 break;
186 case SKIP:
187 mout.debug("skipping external variable: '" , key , '=' , srcRef , "' relative addr=" , relativeAddr );
188 break;
189 case ERROR:
190 mout.error("external variable: '" , key , '=' , srcRef , "' relative addr=" , relativeAddr );
191 break;
192 default:
193 mout.warn("unknown enum option in handling external variable '" , key , '=' , srcRef , "' relative addr=" , relativeAddr );
194 }
195 }
196 }
197 }
198};
199
200template <class T>
201inline
202std::ostream & operator<<(std::ostream & ostr, const ReferenceMap2<T> & rmap){
203 //drain::Sprinter::toStream(ostr, rmap, drain::Sprinter::jsLayout);
204 drain::Sprinter::mapToStream(ostr, rmap, drain::Sprinter::jsLayout, rmap.getKeyList());
205 return ostr;
206}
207
208
210
214// TODO: consider clear() (shallow op)
215class ReferenceMap : public SmartMap<Reference> {//public std::map<std::string,Reference> {
216
217public:
218
220 // ReferenceMap(bool ordered, char separator) : SmartMap<Reference>(ordered, separator){}; //, STRICTLY_CLOSED, keys(orderedKeyList), units(unitMap)
221 ReferenceMap(char separator=',') : SmartMap<Reference>(separator){}; //, STRICTLY_CLOSED, keys(orderedKeyList), units(unitMap)
222
224 inline
227
228 inline virtual
229 ~ReferenceMap(){};
230
231 // Temporary catch for Range
232 template <class F>
233 Reference & link(const std::string & key, Range<F> &x, const std::string & unit = std::string()){
234 Logger mout(__FILE__, __FUNCTION__);
235 mout.deprecating(" type drain::Range<> use .tuple() instead: ", key, '[', unit, ']');
236 return link(key, &x, typeid(F), 2, unit);
237 }
238
240
244 template <class F>
245 Reference & link(const std::string & key, F &x, const std::string & unit = std::string()){
246
247 if (find(key) == end()){ // not already referenced
248
249 if (keyList.empty()){
250 if (key.empty()){
251 std::cerr << "empty key referencing to " << sprinter(x) << " unit=" << unit << std::endl;
252 }
253 else if (key.at(0) == '_'){
254 throw std::runtime_error(key + ": hidden parameters can be added only after visible");
255 }
256 }
257 else if ((keyList.back().at(0) == '_') && (key.at(0) != '_')){
258 throw std::runtime_error(key + ": cannot add visible parameters after hidden");
259 }
260
261
262 keyList.push_back(key);
263
264 }
265
266 // Create
267 Reference & r = std::map<std::string,Reference>::operator[](key);
268 r.setSeparator(arraySeparator); // applicable, if array type element
269 // Link
270 r.link(x);
271 //unitMap[key] = unit;
272
273
274 if (!unit.empty()){
275 unitMap[key] = unit;
276 }
277 else {
279 }
280 /* TODO: upon new Parameter (with value space etc), use this to distinguish description and type
281 if (typeid(F) == typeid(bool)){
282 if (!unit.empty()){
283 std::cerr << "overriding issued type '" << unit << "' with 'bool'";
284 }
285 unitMap[key] = "bool";
286 }
287 else {
288 unitMap[key] = unit;
289 }
290 */
291
292
293 return r;
294 }
295
296 inline
297 Reference & link(const std::string & key, Reference &x, const std::string & unit = std::string()){
298 return link(key, x.getPtr(), x.getType(), x.getElementCount(), unit); //.fillArray = item.fillArray;
299 }
300
301
303 inline
304 Reference & link(const std::string & key, void *ptr, const std::type_info &type, size_t count, const std::string & unit = std::string()){
305
306 if (find(key) == end()) // not already referenced
307 keyList.push_back(key);
308
309 Reference & r = std::map<std::string,Reference>::operator[](key);
310 r.setSeparator(arraySeparator); // applicable, if array type element
311 r.link(ptr, type, count);
312 unitMap[key] = unit;
313 return r;
314
315 }
316
318 inline
319 Reference & link(const std::string & key, void *ptr, const std::type_info &type, const std::string & unit = std::string()){
320 return link(key, ptr, type, 1, unit);
321 }
322
324
328 template <class F>
329 Reference & referenceTop(const std::string & key, F &x, const std::string & unit = std::string()){
330
331 if (find(key) != end()) // already referenced
332 keyList.push_front(key);
333
334 //Reference & r = std::map<std::string,Reference>::operator[](key).link(x);
335 Reference & r = std::map<std::string,Reference>::operator[](key);
336 r.setSeparator(arraySeparator); // applicable, if array type element
337 // Link
338 r.link(x);
339
340 unitMap[key] = unit;
341 return r;
342 }
343
344
346 void append(ReferenceMap & rMap, bool replace=true){
347 //std::cerr << __FILE__ << " -> " << __FUNCTION__ << std::endl;
348 const std::list<std::string> & keys = rMap.getKeyList();
349 //for (std::list<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it){
350 for (const std::string & key: keys){
351 //std::cerr << " -> " << *it << std::endl;
352 if (replace || !hasKey(key)){
353 Reference & srcItem = rMap[key];
354 Reference & item = link(key, srcItem, rMap.unitMap[key]); //.fillArray = item.fillArray;
355 // Reference & item = link(key, (const Castable &)srcItem, rMap.unitMap[key]); //.fillArray = item.fillArray;
356 // Reference & item = link(key, srcItem.getPtr(), srcItem.getType(), srcItem.getElementCount(), rMap.unitMap[key]); //.fillArray = item.fillArray;
357 item.setFill(srcItem.fillArray);
358 item.setInputSeparator(srcItem.getInputSeparator());
359 item.setOutputSeparator(srcItem.getOutputSeparator());
360 }
361 }
362 }
363
364 // todo: consider NULL? (but leads to problems)
365
367 inline
368 void delink(const std::string & key){
369 std::map<std::string,Reference>::erase(key);
370 for (std::list<std::string>::iterator it = keyList.begin(); it != keyList.end(); ++it)
371 if (*it == key){
372 keyList.erase(it);
373 break;
374 }
375 unitMap.erase(key);
376 }
377
379
383 void reserve(const std::string & key){
384
385 if (find(key) == end()) // not already referenced
386 keyList.push_back(key);
387
388 // Create
389 // Reference & r =
390 std::map<std::string,Reference>::operator[](key);
391 // Now r type is unset. (ptr undefined)
392
393 }
394
396
399 virtual inline
400 void clear(){
402 keyList.clear();
403 unitMap.clear();
404 }
405
415
417
424 template <class T>
425 void copyStruct(const ReferenceMap & m, const T & src, T & dst, extLinkPolicy policy=RESERVE) {
426 //, bool copyValues = true, bool linkExternal = false){
427
428 Logger mout(__FILE__, __FUNCTION__);
429 long s = sizeof(T); // yes signed
430 //mout.warn("experimental, obj.size=" , s );
431
432 // Clearing is bad, it resets work of base class constructors
433 // if (policy==LINK)
434 // clear();
435
436 typedef unsigned long addr_t;
437 typedef long addr_diff_t;
438
439 const addr_t srcAddr = (addr_t)(&src);
440 const addr_t dstAddr = (addr_t)(&dst);
441 //for (std::list<std::string>::const_iterator it = m.getKeyList().begin(); it != m.getKeyList().end(); ++it){
442 for (const std::string & key: m.getKeyList()){
443 // const std::string & key = *it;
444 const Reference & srcRef = m[key];
445 addr_t srcVarAddr = (addr_t)srcRef.getPtr();
446 addr_diff_t relativeAddr = srcVarAddr - srcAddr;
447 if ((relativeAddr >= 0) && (relativeAddr < s)){ // INTERNAL
448 Reference & dstMemberRef = link(key, (void *)(dstAddr + relativeAddr), srcRef.getType(), srcRef.getElementCount());
449 //mout.warn("local: " , key , ':' , srcRef.getElementCount() );
450 dstMemberRef.copyFormat(srcRef);
451 dstMemberRef = srcRef; // value
452 //dstMemberRef.setInputSeparator(srcRef.getInputSeparator());
453 }
454 else {
455 //mout.warn("external: " , key );
456 switch (policy) {
457 case LINK:
458 link(key, (void *)srcVarAddr, srcRef.getType(), srcRef.getElementCount()).copyFormat(srcRef);
459 break;
460 case RESERVE:
461 reserve(key);
462 //mout.warn("reserved: " , key );
463 //mout.warn("keyList: " , getKeys() );
464 break;
465 case SKIP:
466 mout.debug("skipping external variable: '" , key , '=' , srcRef , "' relative addr=" , relativeAddr );
467 break;
468 case ERROR:
469 mout.error("external variable: '" , key , '=' , srcRef , "' relative addr=" , relativeAddr );
470 break;
471 default:
472 mout.warn("unknown enum option in handling external variable '" , key , '=' , srcRef , "' relative addr=" , relativeAddr );
473 }
474 }
475 }
476
477 // Shared properties.
478 //keyList = m.keyList;
481 unitMap = m.unitMap;
482
483 }
484
485
487 template <class T>
488 inline
490 //Logger log(__FILE__, __FUNCTION__);
491 //log.error() << "in:" << v << log.endl;
492 importMap(v);
493 return *this;
494 }
495
497 virtual
498 mapped_type & operator[](const std::string &key){
499
500 Logger mout(__FILE__, __FUNCTION__);
501
502 iterator it = this->find(key);
503 if (it != this->end()) {
504 return it->second;
505 }
506 else {
507 mout.warn("current contents: " , *this );
508
509 mout.error("key '" , key ,"' not declared (referenced)" );
510 //throw std::runtime_error(key + ": ReferenceMap & operator[] : key not found");
511 static mapped_type empty;
512 return empty;
513 }
514
515 }
516
518 virtual
519 const mapped_type & operator[](const std::string &key) const {
520
521 Logger mout(__FILE__, __FUNCTION__); //Logger mout(__FILE__, __FUNCTION__);
522
523 const_iterator it = this->find(key);
524 if (it != this->end()) {
525 return it->second;
526 }
527 else {
528 mout.error("key '" , key ,"' not declared (referenced)" );
529 //throw std::runtime_error(key + ": ReferenceMap & operator[] : key not found");
530 static const mapped_type empty;
531 return empty;
532 }
533 }
534
535 typedef std::map<std::string,std::string> unitmap_t;
536
538 inline
539 const unitmap_t & getUnitMap() const { return unitMap; };
540
542 /*
543 template <class T>
544 static
545 size_t serialize(const SmartMap<T> & rmap, std::vector<char> & memory);
546 */
547
549
555protected:
556
558 // Questionable: consider SmartReference etc. with caster and unit info?
559 unitmap_t unitMap;
560
561
562};
563
564inline
565std::ostream & operator<<(std::ostream & ostr, const ReferenceMap & rmap){
566 // drain::Sprinter::toStream(ostr, rmap, drain::Sprinter::jsLayout);
568 return ostr;
569}
570
571/*
572template <class T>
573size_t ReferenceMap::serialize(const SmartMap<T> & smap, std::vector<char> & memory){
574
575 typedef SmartMap<T> smap_t;
576
577 size_t i;
578 for (smap_t::const_iterator it = smap.begin(); it != smap.end(); ++it){
579
580 const std::string & key = it->first;
581 const Castable & value = it->second;
582
583 if (v.isString()){
584
586
587 }
588 else {
589 }
590
591 }
592
593}
594*/
595
596} // namespace drain
597
598
599#endif
600
601// Drain
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:440
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:676
Logger & error(const TT &... args)
Echoes.
Definition Log.h:426
A map of references to base type scalars, arrays or std::string; changing values in either are equiva...
Definition ReferenceMap.h:69
ref_t & link(const std::string &key, F &x)
Associates a map entry with a variable.
Definition ReferenceMap.h:84
ref_t & link(const std::string &key, void *ptr, const std::type_info &type, size_t count=1)
Create a reference to a basic type or std::string. (Also for basetype arrays.)
Definition ReferenceMap.h:94
void copyStruct(const ReferenceMap2< T > &m, const T2 &src, T2 &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition ReferenceMap.h:145
extLinkPolicy
Definition ReferenceMap.h:128
@ ERROR
Definition ReferenceMap.h:132
@ SKIP
Definition ReferenceMap.h:130
@ LINK
Definition ReferenceMap.h:129
@ RESERVE
Definition ReferenceMap.h:131
Definition ReferenceMap.h:215
Reference & link(const std::string &key, void *ptr, const std::type_info &type, size_t count, const std::string &unit=std::string())
For arrays.
Definition ReferenceMap.h:304
unitmap_t unitMap
Creating a common segment for.
Definition ReferenceMap.h:559
void append(ReferenceMap &rMap, bool replace=true)
Adopts the references of r. If replace==false, only new entries are appended.
Definition ReferenceMap.h:346
Reference & link(const std::string &key, F &x, const std::string &unit=std::string())
Associates a map entry with a variable.
Definition ReferenceMap.h:245
ReferenceMap(char separator=',')
Default constructor.
Definition ReferenceMap.h:221
Reference & link(const std::string &key, void *ptr, const std::type_info &type, const std::string &unit=std::string())
Convenience: create a reference to a scalar. For arrays, use the.
Definition ReferenceMap.h:319
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:425
void reserve(const std::string &key)
Adds a null entry, expecting the link later.
Definition ReferenceMap.h:383
ReferenceMap & operator=(const SmartMap< T > &v)
Import map, adopting the element types.
Definition ReferenceMap.h:489
const unitmap_t & getUnitMap() const
Returns measurement unit information of the actual map entries.
Definition ReferenceMap.h:539
virtual mapped_type & operator[](const std::string &key)
Return element associated with key.
Definition ReferenceMap.h:498
Reference & referenceTop(const std::string &key, F &x, const std::string &unit=std::string())
Associates a map entry with a variable, adding key in the beginning of key list.
Definition ReferenceMap.h:329
virtual void clear()
Removes all the elements of the map.
Definition ReferenceMap.h:400
virtual const mapped_type & operator[](const std::string &key) const
Calling unreferenced key throws an exception.
Definition ReferenceMap.h:519
void delink(const std::string &key)
Removes an entry from the map.
Definition ReferenceMap.h:368
ReferenceMap(const ReferenceMap &rmap)
Copy constructor copies only the separators; does not copy the items.
Definition ReferenceMap.h:225
extLinkPolicy
Definition ReferenceMap.h:409
@ ERROR
Definition ReferenceMap.h:413
@ SKIP
Definition ReferenceMap.h:412
@ LINK
Definition ReferenceMap.h:410
@ RESERVE
Definition ReferenceMap.h:411
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 arraySeparator
Default separator character for array elements (std::vector's)
Definition SmartMap.h:84
std::list< std::string > keyList
Assigns values from std::string of type "value,value2,...valueN".
Definition SmartMap.h:426
char separator
Default character used for splitting input and output. See setValues.
Definition SmartMap.h:81
virtual const keylist_t & getKeyList() const
Derived versions may produce an ordered set of keys.
Definition SmartMap.h:201
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:253
static const SprinterLayout jsLayout
JavaScript layout. Like JSON layout, but keys without hyphens.
Definition Sprinter.h:227
static std::ostream & mapToStream(std::ostream &ostr, const M &m, const SprinterLayout &layout, const K &keys)
Given a sequence or subsequence of keys, output values of a map in that order.
Definition Sprinter.h:363
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Definition Data.h:1473
VariableT< ReferenceT< Castable > > Reference
Variable-like that is linked to a standard variable: double, int, std::string . Supports multi-elemen...
Definition Reference.h:75
static const std::string & str()
Default implementation: name returned by std::type_info::name()
Definition TypeName.h:68