31#ifndef DRAIN_MAP_TOOLS_H
32#define DRAIN_MAP_TOOLS_H
46#include "StringTools.h"
57 template <
class K,
class V>
59 typename std::map<K,V>::const_iterator
findEntryByKey(
const std::map<K,V> & m,
const K & key){
64 template <
class K,
class V>
66 typename std::list<K,V>::const_iterator
findEntryByKey(
const std::list<K,V> & l,
const K & key){
67 for (
typename std::list<K,V>::const_iterator it=l.begin(); it!=l.end(); ++it){
68 if (it->first == key){
77 bool hasKey(
const M & m,
const typename M::key_type & key){
92 const typename M::mapped_type &
get(
const M & m,
const typename M::key_type & key){
93 typename M::const_iterator it = m.find(key);
98 static const typename M::mapped_type empty;
106 typename M::mapped_type &
get(M & m,
const typename M::key_type & key){
132 const typename M::value_type::second_type &
get(
const M & m,
const typename M::value_type::first_type & key,
const typename M::value_type::second_type & defaultValue){
144 std::string
get(
const M & m,
const typename M::key_type & key,
const char * defaultValue){
145 typename M::const_iterator it = m.find(key);
159 typedef std::list<std::string> keylist_t;
168 template <
class M,
class V,
bool STRICT=true>
170 void setValue(M & dst,
const std::string & key,
const V & value) {
176 typename M::iterator it = dst.find(key);
177 if (it != dst.end()){
186 template <
class M,
class V>
188 void setValue(M & dst,
const std::string & key,
const V & value,
bool STRICT) {
190 setValue<M,V,true>(dst, key, value);
192 setValue<M,V,false>(dst, key, value);
201 template <
class M,
class V>
203 void updateValue(M & dst,
const std::string & key,
const V & value) {
204 setValue<M,V,false>(dst, key, value);
211 template <
class M,
class S,
bool STRICT=true>
213 void setValues(M & dst,
const std::map<std::string,S> & srcMap) {
215 for (
const typename std::map<std::string,S>::value_type & entry: srcMap){
216 setValue<M,S,STRICT>(dst, entry.first, entry.second);
227 template <
class M,
class S>
230 setValues<M,S,false>(dst, src);
242 template <
class M,
bool STRICT=true>
244 void setValues(M & dst,
const std::list<std::string> & values,
char equalSign=
'=',
const std::string & trimChars =
"") {
246 const bool TRIM = !trimChars.empty();
248 for (
const std::string & entry: values){
252 Logger mout(__FILE__, __FUNCTION__);
253 if (values.size()==1){
255 mout.experimental(
"clearing a map of ", dst.size(),
" elements");
260 mout.
debug(
"parameter list contained an empty value (ok)");
266 const size_t i = entry.find(equalSign);
267 if (i != std::string::npos){
268 if (i == (entry.length()-1)){
273 setValue<M,std::string,STRICT>(dst, entry,
"");
281 setValue<M,std::string,STRICT>(dst, entry.substr(0, i), entry.substr(i+1));
286 for (
const std::string & e: values){
287 std::cerr <<
'"' << e <<
'"' << std::endl;
290 throw std::runtime_error(entry +
": positional args without keys");
305 void updateValues(M & dst,
const std::list<std::string> & values,
char equals=
'=') {
306 setValues<M,false>(dst, values, equals);
317 template <
class M,
bool STRICT=true>
319 void setValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char assignmentChar=
'=') {
321 Logger mout(__FILE__, __FUNCTION__);
327 std::list<std::string>::const_iterator kit = keys.begin();
329 bool acceptOrderedParams =
true;
333 for (
const std::string & entry: entries){
341 std::string key, value;
353 typename M::iterator it = dst.begin();
354 if (key == it->first)
363 setValue<M,std::string,STRICT>(dst, key, value);
364 acceptOrderedParams =
false;
374 if (kit != keys.end()){
376 if (!acceptOrderedParams){
377 mout.
warn(
"positional arg '" , entry ,
"' for [", *kit ,
"] given after explicit args" );
387 mout.
error(
"too many (over ", dst.size() ,
") params, run out of keys with entry=" , entry );
406 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char equals=
'=') {
407 setValues<false>(dst, keys, entries, equals);
422 template <
class M,
bool STRICT=true>
424 void setValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=',
const std::string & trimChars =
"") {
425 std::list<std::string> l;
427 setValues<M,STRICT>(dst, l, equals, trimChars);
438 void updateValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=') {
439 setValues<false>(dst, values, split, equals);
450 template <
class M,
bool STRICT=true>
452 void setValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
453 std::list<std::string> l;
468 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
469 setValues<false>(dst, keys, values, split, equals);
481 template <
class M,
class V,
bool STRICT=true>
483 void setValues(M & dst,
const std::list<std::string> & keys, std::initializer_list<V> values) {
485 std::list<std::string>::const_iterator kit = keys.begin();
487 for (
const V & value: values){
488 if (kit == keys.end()){
489 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
502 template <
class M,
class V>
504 void setValues(M & dst, std::initializer_list<V> values) {
507 typename M::const_iterator it = dst.begin();
509 for (
const V & value: values){
510 if (it == dst.end()){
511 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
520 template <
class M,
typename K,
typename V>
522 void setValues(M & dst, std::initializer_list<std::pair<K,V> > values) {
523 for (
const auto & entry: values){
524 setValue(dst, entry.first, entry.second);
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
Definition DataSelector.cpp:1277