dictmap.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_dictmap_hh
22 #define mia_core_dictmap_hh
23 
24 #include <string>
25 #include <set>
26 #include <map>
27 #include <stdexcept>
28 #include <algorithm>
29 #include <cassert>
30 
31 
32 #include <mia/core/defines.hh>
33 #include <mia/core/errormacro.hh>
34 
36 
44 template <typename T>
45 class TDictMap {
46 public:
50  typedef std::map<T, std::pair<std::string, std::string> > THelpMap;
54  typedef struct {
56  const char * const name;
58  const T value;
60  const char * const help;
61  } Table;
62 
70  TDictMap(const Table *table, bool last_is_default = false);
71 
77  T get_value(const char *name) const;
78 
84  const char *get_name(T value) const;
85 
86 
92  const char *get_help(T value) const;
93 
95  const std::set<std::string> get_name_set() const;
96 
100  typename THelpMap::const_iterator get_help_begin() const;
101 
105  typename THelpMap::const_iterator get_help_end() const;
106 
107 private:
108 
109  typedef std::map<std::string, T> TMap;
110  typedef std::map<T, std::string> TBackMap;
111 
112  bool m_last_is_default;
113  TMap m_table;
114  TBackMap m_back_table;
115  THelpMap m_help;
116  T m_default;
117 
118  struct Insert {
119  Insert( std::set<std::string>& result ):m_result(result) {
120  }
121  void operator() (const typename TMap::value_type& v) {
122  m_result.insert(v.first);
123  }
124  private:
125  std::set<std::string>& m_result;
126  };
127 };
128 
129 
130 template <typename T>
131 TDictMap<T>::TDictMap(const Table *table, bool last_is_default):
132  m_last_is_default(last_is_default)
133 {
134  assert(table);
135  const Table *t = table;
136  while (t->name){
137  if (!m_table.insert(typename TMap::value_type(t->name, t->value)).second)
138  throw std::invalid_argument(std::string("TDictMap<T>::TDictMap:'") +
139  std::string(t->name) +
140  std::string("' already present"));
141  m_back_table.insert(typename TBackMap::value_type(t->value, t->name));
142  m_help.insert(typename THelpMap::value_type(t->value,
143  std::pair<std::string, std::string>(t->name, t->help ? t->help : "")));
144  ++t;
145  }
146  m_default = t->value;
147 }
148 
149 template <typename T>
150 T TDictMap<T>::get_value(const char *name) const
151 {
152  typename TMap::const_iterator i = m_table.find(name);
153  if (i == m_table.end()) {
154  if (!m_last_is_default)
155  throw std::invalid_argument(std::string("TDictMap<T>::get_value: unknown key '")+
156  std::string(name) + std::string("' provided"));
157  else
158  return m_default;
159  }
160  return i->second;
161 }
162 
163 template <typename T>
164 const char *TDictMap<T>::get_name(T value) const
165 {
166  auto i = m_back_table.find(value);
167 
168  if (i == m_back_table.end()) {
169  if (!m_last_is_default || (m_default != value))
170  throw create_exception<std::invalid_argument>("TDictMap<T>::get_name: unknown value ", value, " provided");
171  else
172  return "(default)";
173  }
174  return i->second.c_str();
175 }
176 
177 template <typename T>
178 const char *TDictMap<T>::get_help(T value) const
179 {
180  auto i = m_help.find(value);
181  if (i == m_help.end())
182  throw create_exception<std::invalid_argument>("TDictMap<T>::get_help: unknown value ", value, " provided");
183  return i->second.second.c_str();
184 }
185 
186 template <typename T>
187 const std::set<std::string> TDictMap<T>::get_name_set() const
188 {
189  std::set<std::string> result;
190  std::for_each(m_table.begin(),m_table.end(), Insert(result));
191  return result;
192 }
193 
194 template <typename T>
196 {
197  return m_help.begin();
198 }
199 
200 template <typename T>
202 {
203  return m_help.end();
204 }
205 
207 #endif
const char *const name
parameter name
Definition: dictmap.hh:56
std::map< T, std::pair< std::string, std::string > > THelpMap
Definition: dictmap.hh:50
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
const char * get_name(T value) const
Definition: dictmap.hh:164
const char *const help
help text
Definition: dictmap.hh:60
THelpMap::const_iterator get_help_begin() const
Definition: dictmap.hh:195
THelpMap::const_iterator get_help_end() const
Definition: dictmap.hh:201
TDictMap(const Table *table, bool last_is_default=false)
Definition: dictmap.hh:131
const std::set< std::string > get_name_set() const
Definition: dictmap.hh:187
const T value
parameter value
Definition: dictmap.hh:58
T get_value(const char *name) const
Definition: dictmap.hh:150
const char * get_help(T value) const
Definition: dictmap.hh:178
A mapper from emums to string values. - usefull for names flags.
Definition: dictmap.hh:45
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36