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){
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);
245 Reference &
link(
const std::string & key, F &x,
const std::string & unit = std::string()){
247 if (find(key) == end()){
251 std::cerr <<
"empty key referencing to " << sprinter(x) <<
" unit=" << unit << std::endl;
253 else if (key.at(0) ==
'_'){
254 throw std::runtime_error(key +
": hidden parameters can be added only after visible");
257 else if ((
keyList.back().at(0) ==
'_') && (key.at(0) !=
'_')){
258 throw std::runtime_error(key +
": cannot add visible parameters after hidden");
267 Reference & r = std::map<std::string,Reference>::operator[](key);
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);
304 Reference &
link(
const std::string & key,
void *ptr,
const std::type_info &type,
size_t count,
const std::string & unit = std::string()){
306 if (find(key) == end())
309 Reference & r = std::map<std::string,Reference>::operator[](key);
311 r.link(ptr, type, count);
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);
331 if (find(key) != end())
335 Reference & r = std::map<std::string,Reference>::operator[](key);
348 const std::list<std::string> & keys = rMap.
getKeyList();
350 for (
const std::string & key: keys){
352 if (replace || !hasKey(key)){
357 item.setFill(srcItem.fillArray);
358 item.setInputSeparator(srcItem.getInputSeparator());
359 item.setOutputSeparator(srcItem.getOutputSeparator());
369 std::map<std::string,Reference>::erase(key);
370 for (std::list<std::string>::iterator it =
keyList.begin(); it !=
keyList.end(); ++it)
385 if (find(key) == end())
390 std::map<std::string,Reference>::operator[](key);
428 Logger mout(__FILE__, __FUNCTION__);
436 typedef unsigned long addr_t;
437 typedef long addr_diff_t;
439 const addr_t srcAddr = (addr_t)(&src);
440 const addr_t dstAddr = (addr_t)(&dst);
442 for (
const std::string & key: m.
getKeyList()){
445 addr_t srcVarAddr = (addr_t)srcRef.getPtr();
446 addr_diff_t relativeAddr = srcVarAddr - srcAddr;
447 if ((relativeAddr >= 0) && (relativeAddr < s)){
448 Reference & dstMemberRef = link(key, (
void *)(dstAddr + relativeAddr), srcRef.getType(), srcRef.getElementCount());
450 dstMemberRef.copyFormat(srcRef);
451 dstMemberRef = srcRef;
458 link(key, (
void *)srcVarAddr, srcRef.getType(), srcRef.getElementCount()).copyFormat(srcRef);
466 mout.
debug(
"skipping external variable: '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
469 mout.
error(
"external variable: '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
472 mout.
warn(
"unknown enum option in handling external variable '" , key ,
'=' , srcRef ,
"' relative addr=" , relativeAddr );
500 Logger mout(__FILE__, __FUNCTION__);
503 if (it != this->end()) {
507 mout.
warn(
"current contents: " , *
this );
509 mout.
error(
"key '" , key ,
"' not declared (referenced)" );
511 static mapped_type empty;
519 const mapped_type &
operator[](
const std::string &key)
const {
521 Logger mout(__FILE__, __FUNCTION__);
523 const_iterator it = this->find(key);
524 if (it != this->end()) {
528 mout.
error(
"key '" , key ,
"' not declared (referenced)" );
530 static const mapped_type empty;
535 typedef std::map<std::string,std::string> unitmap_t;
565std::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: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
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