Registry.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef DRAIN_REGISTRY_H_
32 #define DRAIN_REGISTRY_H_
33 
34 #include <drain/Log.h>
35 #include <map>
36 #include <set>
37 
38 #include "Dictionary.h"
39 #include "ReferenceMap.h"
40 #include "VariableMap.h"
41 //#include "../util/StringMapper.h"
42 
43 //#include "Command.h"
44 
45 
46 namespace drain {
47 
48 //#define REGISTRY_DEFAULT "DEFAULT"
49 
50 
51 
53 /*
54  * Commands are stored in a map of type std::map<std::string, T &>.
55  *
56  *
57  * \see drain::Bank<T>
58  */
59 template <class T>
60 class Registry {
61 
62 public:
63 
64  typedef std::map<std::string, T &> map_t;
65 
66  //typedef map<string, set<string> > SectionMap;
67 
68  inline
69  Registry(){}; // : DEFAULT_HANDLER("_handler"), expandVariables(false), statusFormatter("[a-zA-Z0-9_:]+") { ++index; }; // note ':' in statusFormatter
70 
71 
72  virtual inline
73  ~Registry(){};
74 
76  virtual
77  void add(T & r, const std::string & key, char alias = 0);
78 
79 
80 
82  bool has(const std::string & key) const;
83 
85  char getAlias(const std::string & key) const;
86 
88  const std::string & getKey(char alias) const;
89 
91  T & get(const std::string & name) const;
92 
94  map_t & getMap(){ return entryMap; };
95 
97  const map_t & getMap() const { return entryMap; };
98 
100  inline
101  const typename map_t::const_iterator find(const std::string & name) const {
102  return (name.length() == 1) ? entryMap.find(getKey(name.at(0))) : entryMap.find(name);
103  };
104 
106  inline
107  typename map_t::iterator find(const std::string & name) {
108  //return entryMap.find(resolveKey(name));
109  return (name.length() == 1) ? entryMap.find(getKey(name.at(0))) : entryMap.find(name);
110  };
111 
112  inline
113  const drain::Dictionary<char, std::string> & getAliases() const {
114  return aliasesNew;
115  }
116 
117 protected:
118 
119  map_t entryMap;
120 
122 
123 };
124 
125 
126 template <class T>
127 void Registry<T>::add(T & r, const std::string & name, char alias){
128 
129  entryMap.insert(std::pair<std::string, T &>(name, r));
130 
131  if (alias){
132  aliasesNew.add(alias, name);
133  }
134 
135 }
136 
137 
138 
139 template <class T>
140 bool Registry<T>::has(const std::string & name) const {
141  //const typename map_t::const_iterator it = find(name);
142  return (find(name) != entryMap.end());
143 }
144 
145 template <class T>
146 const std::string & Registry<T>::getKey(char alias) const{
147 
148  Dictionary<char,std::string>::const_iterator it = aliasesNew.findByKey(alias);
149 
150  if (it != aliasesNew.end())
151  return it->second;
152  else{
153  static const std::string empty;
154  return empty;
155  }
156 
157 
158 }
159 
160 
161 template <class T>
162 char Registry<T>::getAlias(const std::string &name) const{
163 
164  Dictionary<char,std::string>::const_iterator it = aliasesNew.findByValue(name);
165 
166  if (it != aliasesNew.end()){
167  return it->first;
168  }
169  else{
170  //std::cout << "not found: " << name << '\n';
171  return 0;
172  }
173 
174 }
175 
176 template <class T>
177 T & Registry<T>::get(const std::string & name) const {
178  const typename map_t::const_iterator it = find(name);
179  if (it != entryMap.end())
180  return it->second;
181  else
182  throw std::runtime_error(name + ": no such entry");
183 
184 }
185 
186 
187 } /* namespace drain */
188 
189 #endif /* DRAINREGISTRY_H_ */
190 
191 // Drain
A container for storing global objects like functors, operators or commands. Supports aliases.
Definition: Registry.h:60
char getAlias(const std::string &key) const
Returns a single-letter abbreviation of a command.
Definition: Registry.h:162
T & get(const std::string &name) const
Given a single-letter abbreviation or full command name, returns the command object.
Definition: Registry.h:177
const std::string & getKey(char alias) const
Given a single-letter abbreviation, return the command name.
Definition: Registry.h:146
const map_t::const_iterator find(const std::string &name) const
Like find(), but handles aliases.
Definition: Registry.h:101
map_t::iterator find(const std::string &name)
Like find(), but handles aliases.
Definition: Registry.h:107
Registry()
TODO: reference not recommended.
Definition: Registry.h:69
map_t & getMap()
Returns the actual map containig the entries.
Definition: Registry.h:94
bool has(const std::string &key) const
Queries whether a command has been added.
Definition: Registry.h:140
virtual void add(T &r, const std::string &key, char alias=0)
Adds entry of (base) class T to current section of registry.
Definition: Registry.h:127
const map_t & getMap() const
Returns the actual map containig the entries.
Definition: Registry.h:97
Definition: DataSelector.cpp:1277