42#include <drain/Reference.h>
43#include <drain/StringTools.h>
68template <
class T=Reference>
84 ref_t &
link(
const std::string & key, F &x){
86 ref_t & r = (*this)[key];
94 ref_t &
link(
const std::string & key,
void *ptr,
const std::type_info &type,
size_t count=1){
96 ref_t & r = (*this)[key];
97 r.link(ptr, type, count);
113 void unlink(
const std::string & key){
115 for (std::list<std::string>::iterator it = this->
keyList.begin(); it != this->
keyList.end(); ++it){
148 Logger mout(__FILE__, __FUNCTION__);
156 typedef unsigned long addr_t;
157 typedef long addr_diff_t;
159 const addr_t srcAddr = (addr_t)(&src);
160 const addr_t dstAddr = (addr_t)(&dst);
162 for (
const std::string & key: m.
getKeyList()){
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)){
169 ref_t & dstMemberRef =
link(key, (
void *)(dstAddr + relativeAddr), srcRef.getType(), srcRef.getElementCount());
171 dstMemberRef.copyFormat(srcRef);
172 dstMemberRef.assignCastable(srcRef);
178 link(key, (
void *)srcVarAddr, srcRef.getType(), srcRef.getElementCount()).copyFormat(srcRef);
187 mout.
debug(
"skipping external variable: '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
190 mout.
error(
"external variable: '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
193 mout.
warn(
"unknown enum option in handling external variable '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
202std::ostream & operator<<(std::ostream & ostr,
const ReferenceMap2<T> & rmap){
236 Reference & link(
const std::string & key, Range<F> &x,
const std::string & unit = std::string()){
237 Logger mout(__FILE__, __FUNCTION__);
238 mout.deprecating(
" type drain::Range<> use .tuple() instead: ", key,
'[', unit,
']');
239 return link(key, &x,
typeid(F), 2, unit);
248 Reference &
link(
const std::string & key, F &x,
const std::string & unit = std::string()){
250 if (find(key) == end()){
254 std::cerr <<
"empty key referencing to " << sprinter(x) <<
" unit=" << unit << std::endl;
256 else if (key.at(0) ==
'_'){
257 throw std::runtime_error(key +
": hidden parameters can be added only after visible");
260 else if ((
keyList.back().at(0) ==
'_') && (key.at(0) !=
'_')){
261 throw std::runtime_error(key +
": cannot add visible parameters after hidden");
270 Reference & r = std::map<std::string,Reference>::operator[](key);
287 Reference & link(
const std::string & key,
Reference &x,
const std::string & unit = std::string()){
288 return link(key, x.getPtr(), x.getType(), x.getElementCount(), unit);
294 Reference &
link(
const std::string & key,
void *ptr,
const std::type_info &type,
size_t count,
const std::string & unit = std::string()){
296 if (find(key) == end())
299 Reference & r = std::map<std::string,Reference>::operator[](key);
301 r.link(ptr, type, count);
309 Reference &
link(
const std::string & key,
void *ptr,
const std::type_info &type,
const std::string & unit = std::string()){
310 return link(key, ptr, type, 1, unit);
321 if (find(key) != end())
325 Reference & r = std::map<std::string,Reference>::operator[](key);
338 const std::list<std::string> & keys = rMap.
getKeyList();
340 for (
const std::string & key: keys){
342 if (replace || !hasKey(key)){
347 item.setFill(srcItem.fillArray);
348 item.setInputSeparator(srcItem.getInputSeparator());
349 item.setOutputSeparator(srcItem.getOutputSeparator());
359 std::map<std::string,Reference>::erase(key);
360 for (std::list<std::string>::iterator it =
keyList.begin(); it !=
keyList.end(); ++it)
375 if (find(key) == end())
380 std::map<std::string,Reference>::operator[](key);
418 Logger mout(__FILE__, __FUNCTION__);
426 typedef unsigned long addr_t;
427 typedef long addr_diff_t;
429 const addr_t srcAddr = (addr_t)(&src);
430 const addr_t dstAddr = (addr_t)(&dst);
432 for (
const std::string & key: m.
getKeyList()){
435 addr_t srcVarAddr = (addr_t)srcRef.getPtr();
436 addr_diff_t relativeAddr = srcVarAddr - srcAddr;
437 if ((relativeAddr >= 0) && (relativeAddr < s)){
438 Reference & dstMemberRef = link(key, (
void *)(dstAddr + relativeAddr), srcRef.getType(), srcRef.getElementCount());
440 dstMemberRef.copyFormat(srcRef);
441 dstMemberRef = srcRef;
448 link(key, (
void *)srcVarAddr, srcRef.getType(), srcRef.getElementCount()).copyFormat(srcRef);
456 mout.
debug(
"skipping external variable: '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
459 mout.
error(
"external variable: '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
462 mout.
warn(
"unknown enum option in handling external variable '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
490 Logger mout(__FILE__, __FUNCTION__);
493 if (it != this->end()) {
497 mout.
warn(
"current contents: " , *
this );
499 mout.
error(
"key '" , key ,
"' not declared (referenced)" );
501 static mapped_type empty;
509 const mapped_type &
operator[](
const std::string &key)
const {
511 Logger mout(__FILE__, __FUNCTION__);
513 const_iterator it = this->find(key);
514 if (it != this->end()) {
518 mout.
error(
"key '" , key ,
"' not declared (referenced)" );
520 static const mapped_type empty;
525 typedef std::map<std::string,std::string> unitmap_t;
555std::ostream & operator<<(std::ostream & ostr,
const ReferenceMap & rmap){
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:431
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:667
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
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:294
unitmap_t unitMap
Creating a common segment for.
Definition ReferenceMap.h:549
void append(ReferenceMap &rMap, bool replace=true)
Adopts the references of r. If replace==false, only new entries are appended.
Definition ReferenceMap.h:336
Reference & link(const std::string &key, F &x, const std::string &unit=std::string())
Associates a map entry with a variable.
Definition ReferenceMap.h:248
ReferenceMap(char separator=',')
Default constructor.
Definition ReferenceMap.h:224
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:309
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:415
void reserve(const std::string &key)
Adds a null entry, expecting the link later.
Definition ReferenceMap.h:373
ReferenceMap & operator=(const SmartMap< T > &v)
Import map, adopting the element types.
Definition ReferenceMap.h:479
const unitmap_t & getUnitMap() const
Returns measurement unit information of the actual map entries.
Definition ReferenceMap.h:529
virtual mapped_type & operator[](const std::string &key)
Return element associated with key.
Definition ReferenceMap.h:488
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:319
virtual void clear()
Removes all the elements of the map.
Definition ReferenceMap.h:390
virtual const mapped_type & operator[](const std::string &key) const
Calling unreferenced key throws an exception.
Definition ReferenceMap.h:509
void delink(const std::string &key)
Removes an entry from the map.
Definition ReferenceMap.h:358
ReferenceMap(const ReferenceMap &rmap)
Copy constructor copies only the separators; does not copy the items.
Definition ReferenceMap.h:228
extLinkPolicy
Definition ReferenceMap.h:399
@ ERROR
Definition ReferenceMap.h:403
@ SKIP
Definition ReferenceMap.h:402
@ LINK
Definition ReferenceMap.h:400
@ RESERVE
Definition ReferenceMap.h:401
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 arraySeparator
Default separator character for array elements (std::vector's)
Definition SmartMap.h:88
std::list< std::string > keyList
Assigns values from std::string of type "value,value2,...valueN".
Definition SmartMap.h:430
char separator
Default character used for splitting input and output. See setValues.
Definition SmartMap.h:85
virtual const keylist_t & getKeyList() const
Derived versions may produce an ordered set of keys.
Definition SmartMap.h:200
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:255
static const SprinterLayout jsLayout
JavaScript layout. Like JSON layout, but keys without hyphens.
Definition Sprinter.h:228
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:364
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Definition DataSelector.cpp:1277
VariableT< ReferenceT< Castable > > Reference
Variable-like that is linked to a standard variable: double, int, std::string . Supports multi-elemen...
Definition Reference.h:78