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);
225 template <
class M,
bool STRICT=true>
227 void setValues(M & dst,
const std::list<std::string> & values,
char equalSign=
'=') {
228 for (
const std::string & entry: values){
230 Logger mout(__FILE__, __FUNCTION__);
231 if (values.size()==1){
233 mout.experimental(
"clearing a map of ", dst.size(),
" elements");
238 mout.
warn(
"parameter list contained an empty value");
242 const size_t i = entry.find(equalSign);
243 if (i != std::string::npos){
244 if (i == (entry.length()-1)){
245 setValue<M,std::string,STRICT>(dst, entry,
"");
248 setValue<M,std::string,STRICT>(dst, entry.substr(0, i), entry.substr(i+1));
252 for (
const std::string & e: values){
253 std::cerr <<
'"' << e <<
'"' << std::endl;
256 throw std::runtime_error(entry +
": positional args without keys");
262 template <
class M,
bool STRICT=true>
264 void setValues(M & dst,
const std::initializer_list<Variable::init_pair_t > &l){
265 for (
const auto & entry: l){
266 drain::SmartMapTools::setValue<M,Variable,STRICT>(dst, entry.first, entry.second);
278 void updateValues(M & dst,
const std::list<std::string> & values,
char equals=
'=') {
279 setValues<M,false>(dst, values, equals);
290 template <
class M,
bool STRICT=true>
292 void setValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char assignmentSymbol=
'=') {
294 Logger mout(__FILE__, __FUNCTION__);
297 const std::string assignmentSymbols(1, assignmentSymbol);
300 std::list<std::string>::const_iterator kit = keys.begin();
303 bool acceptOrderedParams =
true;
307 for (
const std::string & entry: entries){
313 if (assignmentSymbol){
315 std::string key, value;
326 typename M::iterator it = dst.begin();
327 if (key == it->first)
336 setValue<M,std::string,STRICT>(dst, key, value);
337 acceptOrderedParams =
false;
347 if (kit != keys.end()){
349 if (!acceptOrderedParams){
350 mout.
warn(
"positional arg '" , entry ,
"' for [", *kit ,
"] given after explicit args" );
360 mout.
error(
"too many (over ", dst.size() ,
") params, run out of keys with entry=" , entry );
402 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::list<std::string> & entries,
char equals=
'=') {
403 setValues<false>(dst, keys, entries, equals);
418 template <
class M,
bool STRICT=true>
420 void setValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=') {
421 std::list<std::string> l;
434 void updateValues(M & dst,
const std::string & values,
char split=
',',
char equals=
'=') {
435 setValues<false>(dst, values, split, equals);
446 template <
class M,
bool STRICT=true>
448 void setValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
449 std::list<std::string> l;
464 void updateValues(M & dst,
const std::list<std::string> & keys,
const std::string & values,
char split=
',',
char equals=
'=') {
465 setValues<false>(dst, keys, values, split, equals);
477 template <
class M,
class V,
bool STRICT=true>
479 void setValues(M & dst,
const std::list<std::string> & keys, std::initializer_list<V> values) {
481 std::list<std::string>::const_iterator kit = keys.begin();
483 for (
const V & value: values){
484 if (kit == keys.end()){
485 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
498 template <
class M,
class V>
500 void setValues(M & dst, std::initializer_list<V> values) {
503 typename M::const_iterator it = dst.begin();
505 for (
const V & value: values){
506 if (it == dst.end()){
507 throw std::runtime_error(std::string(__FILE__) +
" run out of keys");
Definition: Castable.h:76
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:426
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition: VariableT.h:87
Definition: DataSelector.cpp:1277