31#ifndef DRAIN_MAP_TOOLS_H
32#define DRAIN_MAP_TOOLS_H
46#include "StringTools.h"
65 const typename M::mapped_type &
get(
const M & m,
const typename M::key_type & key){
66 typename M::const_iterator it = m.find(key);
71 static const typename M::mapped_type empty;
78 typedef std::list<std::string> keylist_t;
87 template <
class M,
class V,
bool STRICT=true>
89 void setValue(M & dst,
const std::string & key,
const V & value) {
95 typename M::iterator it = dst.find(key);
105 template <
class M,
class V>
107 void setValue(M & dst,
const std::string & key,
const V & value,
bool STRICT) {
109 setValue<M,V,true>(dst, key, value);
111 setValue<M,V,false>(dst, key, value);
120 template <
class M,
class V>
122 void updateValue(M & dst,
const std::string & key,
const V & value) {
123 setValue<M,V,false>(dst, key, value);
130 template <
class M,
class S,
bool STRICT=true>
132 void setValues(M & dst,
const std::map<std::string,S> & srcMap) {
134 for (
const typename std::map<std::string,S>::value_type & entry: srcMap){
135 setValue<M,S,STRICT>(dst, entry.first, entry.second);
146 template <
class M,
class S>
149 setValues<M,S,false>(dst, src);
161 template <
class M,
bool STRICT=true>
163 void setValues(M & dst,
const std::list<std::string> & values,
char equalSign=
'=',
const std::string & trimChars =
"") {
165 const bool TRIM = !trimChars.empty();
167 for (
const std::string & entry: values){
171 Logger mout(__FILE__, __FUNCTION__);
172 if (values.size()==1){
174 mout.experimental(
"clearing a map of ", dst.size(),
" elements");
179 mout.
debug(
"parameter list contained an empty value (ok)");
185 const size_t i = entry.find(equalSign);
186 if (i != std::string::npos){
187 if (i == (entry.length()-1)){
192 setValue<M,std::string,STRICT>(dst, entry,
"");
200 setValue<M,std::string,STRICT>(dst, entry.substr(0, i), entry.substr(i+1));
205 for (
const std::string & e: values){
206 std::cerr <<
'"' << e <<
'"' << std::endl;
209 throw std::runtime_error(entry +
": positional args without keys");
224 void updateValues(M & dst,
const std::list<std::string> & values,
char equals=
'=') {
225 setValues<M,false>(dst, values, equals);
236 template <
class M,
bool STRICT=true>
238 void setValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char assignmentChar=
'=') {
240 Logger mout(__FILE__, __FUNCTION__);
246 std::list<std::string>::const_iterator kit = keys.begin();
248 bool acceptOrderedParams =
true;
252 for (
const std::string & entry: entries){
260 std::string key, value;
272 typename M::iterator it = dst.begin();
273 if (key == it->first)
282 setValue<M,std::string,STRICT>(dst, key, value);
283 acceptOrderedParams =
false;
293 if (kit != keys.end()){
295 if (!acceptOrderedParams){
296 mout.
warn(
"positional arg '" , entry ,
"' for [", *kit ,
"] given after explicit args" );
306 mout.
error(
"too many (over ", dst.size() ,
") params, run out of keys with entry=" , entry );
325 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char equals=
'=') {
326 setValues<false>(dst, keys, entries, equals);
341 template <
class M,
bool STRICT=true>
343 void setValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=',
const std::string & trimChars =
"") {
344 std::list<std::string> l;
346 setValues<M,STRICT>(dst, l, equals, trimChars);
357 void updateValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=') {
358 setValues<false>(dst, values, split, equals);
369 template <
class M,
bool STRICT=true>
371 void setValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
372 std::list<std::string> l;
387 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
388 setValues<false>(dst, keys, values, split, equals);
400 template <
class M,
class V,
bool STRICT=true>
402 void setValues(M & dst,
const std::list<std::string> & keys, std::initializer_list<V> values) {
404 std::list<std::string>::const_iterator kit = keys.begin();
406 for (
const V & value: values){
407 if (kit == keys.end()){
408 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
421 template <
class M,
class V>
423 void setValues(M & dst, std::initializer_list<V> values) {
426 typename M::const_iterator it = dst.begin();
428 for (
const V & value: values){
429 if (it == dst.end()){
430 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
439 template <
class M,
typename K,
typename V>
441 void setValues(M & dst, std::initializer_list<std::pair<K,V> > values) {
442 for (
const auto & entry: values){
443 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