Image Component Library (ICL)
ParamList.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/ParamList.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/Macros.h>
34 #include <ICLUtils/Any.h>
35 #include <map>
36 
37 namespace icl{
38  namespace utils{
39 
41 
44  struct ParamList : public std::map<std::string, Any>{
45 
47  typedef std::string Key;
48 
50  typedef Any Value;
51 
53  inline ParamList(){}
54 
56 
58  inline ParamList(const std::string &commaSepKeyValueString) {
59  init(commaSepKeyValueString);
60  }
61 
63  inline ParamList(const char *commaSepKeyValueString) {
64  init(commaSepKeyValueString);
65  }
66 
67  inline void init(const std::string commaSepKeyValueString) {
68  std::vector<std::string> ts = tok(commaSepKeyValueString,",",true,'\\');
69  for(size_t i=0;i<ts.size();++i){
70  std::vector<std::string> kv = tok(ts[i],"=",true,'\\');
71  ICLASSERT_THROW(kv.size() == 2, ICLException("ParamList(string): invalid token :'"
72  + ts[i] + "'"));
73  const_cast<ParamList*>(this)->operator[](kv[0]) = kv[1];
74  }
75  }
76 
77 
79 
80  inline ParamList(const Key &key0, const Value &value0,
81  const Key &key1="", const Value &value1="",
82  const Key &key2="", const Value &value2="",
83  const Key &key3="", const Value &value3="",
84  const Key &key4="", const Value &value4="",
85  const Key &key5="", const Value &value5="",
86  const Key &key6="", const Value &value6="",
87  const Key &key7="", const Value &value7="",
88  const Key &key8="", const Value &value8="",
89  const Key &key9="", const Value &value9="" ){
90  if(key0.length()) operator[](key0) = value0;
91  if(key1.length()) operator[](key1) = value1;
92  if(key2.length()) operator[](key2) = value2;
93  if(key3.length()) operator[](key3) = value3;
94  if(key4.length()) operator[](key4) = value4;
95  if(key5.length()) operator[](key5) = value5;
96  if(key6.length()) operator[](key6) = value6;
97  if(key7.length()) operator[](key7) = value7;
98  if(key8.length()) operator[](key8) = value8;
99  if(key9.length()) operator[](key9) = value9;
100  }
101 
103 
104  inline bool hasKey(const Key &key) const { return find(key) != end(); }
105 
107 
108  inline void removeKey(const Key &key){
109  iterator it = find(key);
110  if(it != end()) erase(it);
111  }
112 
114  inline const Any &operator[](const Key &key) const {
115  const_iterator it = find(key);
116  if(it == end()) throw ICLException("error in ParamList::operator[](key) with key=" + key + ": key not found!");
117  return it->second;
118  }
119 
121  using std::map<std::string,Any>::operator[];
122  };
123 
124 
125  } // namespace utils
126 }
127 
void removeKey(const Key &key)
removes the given key from the map
Definition: ParamList.h:108
Any Value
The Value type (icl::Any)
Definition: ParamList.h:50
undocument this line if you encounter any issues!
Definition: Any.h:37
ParamList()
creates an empty param list instance
Definition: ParamList.h:53
bool hasKey(const Key &key) const
returns whether the map contains the given key
Definition: ParamList.h:104
Utility structure that utilizes an std::map as parameter list.
Definition: ParamList.h:44
void init(const std::string commaSepKeyValueString)
Definition: ParamList.h:67
const Any & operator[](const Key &key) const
extension for the unconst operator that is provided by the std::map class
Definition: ParamList.h:114
ParamList(const std::string &commaSepKeyValueString)
creates a param list from a single given string
Definition: ParamList.h:58
Base class for Exception handling in the ICL.
Definition: Exception.h:42
ParamList(const Key &key0, const Value &value0, const Key &key1="", const Value &value1="", const Key &key2="", const Value &value2="", const Key &key3="", const Value &value3="", const Key &key4="", const Value &value4="", const Key &key5="", const Value &value5="", const Key &key6="", const Value &value6="", const Key &key7="", const Value &value7="", const Key &key8="", const Value &value8="", const Key &key9="", const Value &value9="")
Constructor, that can get up to 10 key-value pairs.
Definition: ParamList.h:80
std::string Key
The Key type (std::string)
Definition: ParamList.h:47
ParamList(const char *commaSepKeyValueString)
this allows for implicit creation of a ParamList instance from a given const char *
Definition: ParamList.h:63
Simple generic data type implementation that uses a string based data representation.
Definition: Any.h:109
ICLUtils_API std::vector< std::string > tok(const std::string &s, const std::string &delims=" ", bool singleCharDelims=true, char escapeChar='\0')
tokenizes a string with given delimiters (internally using a temporary StrTok instance)
#define ICLASSERT_THROW(X, OBJ)
Definition: Macros.h:155