31#ifndef DRAIN_MAP_TOOLS_H
32#define DRAIN_MAP_TOOLS_H
44#include "StringTools.h"
55 template <
class K,
class V>
57 typename std::map<K,V>::const_iterator
findEntryByKey(
const std::map<K,V> & m,
const K & key){
62 template <
class K,
class V>
64 typename std::list<K,V>::const_iterator
findEntryByKey(
const std::list<K,V> & l,
const K & key){
65 for (
typename std::list<K,V>::const_iterator it=l.begin(); it!=l.end(); ++it){
66 if (it->first == key){
75 bool hasKey(
const M & m,
const typename M::key_type & key){
90 const typename M::mapped_type &
get(
const M & m,
const typename M::key_type & key){
91 typename M::const_iterator it = m.find(key);
96 static const typename M::mapped_type empty;
104 typename M::mapped_type &
get(M & m,
const typename M::key_type & key){
130 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){
142 std::string
get(
const M & m,
const typename M::key_type & key,
const char * defaultValue){
143 typename M::const_iterator it = m.find(key);
157 typedef std::list<std::string> keylist_t;
166 template <
class M,
class V,
bool STRICT=true>
168 void setValue(M & dst,
const std::string & key,
const V & value) {
174 typename M::iterator it = dst.find(key);
175 if (it != dst.end()){
184 template <
class M,
class V>
186 void setValue(M & dst,
const std::string & key,
const V & value,
bool STRICT) {
188 setValue<M,V,true>(dst, key, value);
190 setValue<M,V,false>(dst, key, value);
199 template <
class M,
class V>
201 void updateValue(M & dst,
const std::string & key,
const V & value) {
202 setValue<M,V,false>(dst, key, value);
209 template <
class M,
class S,
bool STRICT=true>
211 void setValues(M & dst,
const std::map<std::string,S> & srcMap) {
213 for (
const typename std::map<std::string,S>::value_type & entry: srcMap){
214 setValue<M,S,STRICT>(dst, entry.first, entry.second);
225 template <
class M,
class S>
228 setValues<M,S,false>(dst, src);
240 template <
class M,
bool STRICT=true>
242 void setValues(M & dst,
const std::list<std::string> & values,
char equalSign=
'=',
const std::string & trimChars =
"") {
244 const bool TRIM = !trimChars.empty();
246 for (
const std::string & entry: values){
250 Logger mout(__FILE__, __FUNCTION__);
251 if (values.size()==1){
253 mout.experimental(
"clearing a map of ", dst.size(),
" elements");
258 mout.
debug(
"parameter list contained an empty value (ok)");
264 const size_t i = entry.find(equalSign);
265 if (i != std::string::npos){
266 if (i == (entry.length()-1)){
271 setValue<M,std::string,STRICT>(dst, entry,
"");
279 setValue<M,std::string,STRICT>(dst, entry.substr(0, i), entry.substr(i+1));
284 for (
const std::string & e: values){
285 std::cerr <<
'"' << e <<
'"' << std::endl;
288 throw std::runtime_error(entry +
": positional args without keys");
303 void updateValues(M & dst,
const std::list<std::string> & values,
char equals=
'=') {
304 setValues<M,false>(dst, values, equals);
315 template <
class M,
bool STRICT=true>
317 void setValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char assignmentChar=
'=') {
319 Logger mout(__FILE__, __FUNCTION__);
325 std::list<std::string>::const_iterator kit = keys.begin();
327 bool acceptOrderedParams =
true;
331 for (
const std::string & entry: entries){
339 std::string key, value;
351 typename M::iterator it = dst.begin();
352 if (key == it->first)
361 setValue<M,std::string,STRICT>(dst, key, value);
362 acceptOrderedParams =
false;
372 if (kit != keys.end()){
374 if (!acceptOrderedParams){
375 mout.
warn(
"positional arg '" , entry ,
"' for [", *kit ,
"] given after explicit args" );
385 mout.
warn(DRAIN_LOG(sprinter(entries)));
386 mout.
warn(DRAIN_LOG(sprinter(keys)));
387 mout.
warn(DRAIN_LOG(sprinter(dst)));
388 mout.
error(
"too many (over ", dst.size() ,
") params, run out of keys with entry=" , entry );
407 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char equals=
'=') {
408 setValues<false>(dst, keys, entries, equals);
423 template <
class M,
bool STRICT=true>
425 void setValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=',
const std::string & trimChars =
"") {
426 std::list<std::string> l;
428 setValues<M,STRICT>(dst, l, equals, trimChars);
439 void updateValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=') {
440 setValues<false>(dst, values, split, equals);
451 template <
class M,
bool STRICT=true>
453 void setValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
454 std::list<std::string> l;
469 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
470 setValues<false>(dst, keys, values, split, equals);
482 template <
class M,
class V,
bool STRICT=true>
484 void setValues(M & dst,
const std::list<std::string> & keys, std::initializer_list<V> values) {
486 std::list<std::string>::const_iterator kit = keys.begin();
488 for (
const V & value: values){
489 if (kit == keys.end()){
490 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
503 template <
class M,
class V>
505 void setValues(M & dst, std::initializer_list<V> values) {
508 typename M::const_iterator it = dst.begin();
510 for (
const V & value: values){
511 if (it == dst.end()){
512 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
521 template <
class M,
typename K,
typename V>
523 void setValues(M & dst, std::initializer_list<std::pair<K,V> > values) {
524 for (
const auto & entry: values){
525 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