31 #ifndef DRAIN_SMARTMAP_TOOLS_H
32 #define DRAIN_SMARTMAP_TOOLS_H
34 #include <drain/Log.h>
35 #include <drain/Reference.h>
43 #include <sys/syslog.h>
47 #include "Reference.h"
69 const typename M::mapped_type &
get(
const M & m,
const typename M::key_type & key){
70 typename M::const_iterator it = m.find(key);
75 static const typename M::mapped_type empty;
88 template <
class M,
class F>
90 void get(
const M & m,
const typename M::key_type & key, F & value){
91 typename M::const_iterator it = m.find(key);
110 typedef std::list<std::string> keylist_t;
131 template <
class M,
class V,
bool STRICT=true>
133 void setValue(M & dst,
const std::string & key,
const V & value) {
139 typename M::iterator it = dst.find(key);
140 if (it != dst.end()){
149 template <
class M,
class V>
151 void setValue(M & dst,
const std::string & key,
const V & value,
bool STRICT) {
153 setValue<M,V,true>(dst, key, value);
155 setValue<M,V,false>(dst, key, value);
164 template <
class M,
class V>
166 void updateValue(M & dst,
const std::string & key,
const V & value) {
167 setValue<M,V,false>(dst, key, value);
174 template <
class M,
class S,
bool STRICT=true>
176 void setValues(M & dst,
const std::map<std::string,S> & srcMap) {
178 for (
const typename std::map<std::string,S>::value_type & entry: srcMap){
179 setValue<M,S,STRICT>(dst, entry.first, entry.second);
183 template <
class M,
class S,
bool STRICT=true>
185 void setCastableValues(M & dst,
const std::map<std::string,S> & srcMap) {
187 for (
const typename std::map<std::string,S>::value_type & entry: srcMap){
188 setValue<M,drain::Castable,STRICT>(dst, entry.first, (
const drain::Castable &) entry.second);
203 template <
class M,
class S>
206 setValues<M,S,false>(dst, src);
213 template <
class M,
class S>
216 setCastableValues<M,S,false>(dst, src);
227 template <
class M,
bool STRICT=true>
229 void setValues(M & dst,
const std::list<std::string> & values,
char equalSign=
'=',
const std::string & trimChars =
"") {
231 const bool TRIM = !trimChars.empty();
233 for (
const std::string & entry: values){
237 Logger mout(__FILE__, __FUNCTION__);
238 if (values.size()==1){
240 mout.experimental(
"clearing a map of ", dst.size(),
" elements");
245 mout.
debug(
"parameter list contained an empty value (ok)");
251 const size_t i = entry.find(equalSign);
252 if (i != std::string::npos){
253 if (i == (entry.length()-1)){
258 setValue<M,std::string,STRICT>(dst, entry,
"");
266 setValue<M,std::string,STRICT>(dst, entry.substr(0, i), entry.substr(i+1));
271 for (
const std::string & e: values){
272 std::cerr <<
'"' << e <<
'"' << std::endl;
275 throw std::runtime_error(entry +
": positional args without keys");
281 template <
class M,
bool STRICT=true>
283 void setValues(M & dst,
const std::initializer_list<Variable::init_pair_t > &l){
284 for (
const auto & entry: l){
285 drain::SmartMapTools::setValue<M,Variable,STRICT>(dst, entry.first, entry.second);
297 void updateValues(M & dst,
const std::list<std::string> & values,
char equals=
'=') {
298 setValues<M,false>(dst, values, equals);
309 template <
class M,
bool STRICT=true>
311 void setValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char assignmentSymbol=
'=') {
313 Logger mout(__FILE__, __FUNCTION__);
316 const std::string assignmentSymbols(1, assignmentSymbol);
319 std::list<std::string>::const_iterator kit = keys.begin();
322 bool acceptOrderedParams =
true;
326 for (
const std::string & entry: entries){
332 if (assignmentSymbol){
334 std::string key, value;
345 typename M::iterator it = dst.begin();
346 if (key == it->first)
355 setValue<M,std::string,STRICT>(dst, key, value);
356 acceptOrderedParams =
false;
366 if (kit != keys.end()){
368 if (!acceptOrderedParams){
369 mout.
warn(
"positional arg '" , entry ,
"' for [", *kit ,
"] given after explicit args" );
379 mout.
error(
"too many (over ", dst.size() ,
") params, run out of keys with entry=" , entry );
421 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char equals=
'=') {
422 setValues<false>(dst, keys, entries, equals);
437 template <
class M,
bool STRICT=true>
439 void setValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=',
const std::string & trimChars =
"") {
440 std::list<std::string> l;
453 void updateValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=') {
454 setValues<false>(dst, values, split, equals);
465 template <
class M,
bool STRICT=true>
467 void setValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
468 std::list<std::string> l;
483 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
484 setValues<false>(dst, keys, values, split, equals);
496 template <
class M,
class V,
bool STRICT=true>
498 void setValues(M & dst,
const std::list<std::string> & keys, std::initializer_list<V> values) {
500 std::list<std::string>::const_iterator kit = keys.begin();
502 for (
const V & value: values){
503 if (kit == keys.end()){
504 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
517 template <
class M,
class V>
519 void setValues(M & dst, std::initializer_list<V> values) {
522 typename M::const_iterator it = dst.begin();
524 for (
const V & value: values){
525 if (it == dst.end()){
526 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
535 template <
class M,
typename K,
typename V>
537 void setValues(M & dst, std::initializer_list<std::pair<K,V> > values) {
538 for (
const auto & entry: values){
539 setValue(dst, entry.first, entry.second);
Definition: Castable.h:76
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:414
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:428
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:678
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition: VariableT.h:87
Definition: DataSelector.cpp:1277