My Project
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>
34
36
44template <typename T>
46{
47public:
51 typedef std::map<T, std::pair<std::string, std::string>> THelpMap;
55 typedef struct {
57 const char *const name;
59 const T value;
61 const char *const help;
62 } Table;
63
71 TDictMap(const Table *table, bool last_is_default = false);
72
78 T get_value(const char *name) const;
79
85 const char *get_name(T value) const;
86
87
93 const char *get_help(T value) const;
94
96 const std::set<std::string> get_name_set() const;
97
101 typename THelpMap::const_iterator get_help_begin() const;
102
106 typename THelpMap::const_iterator get_help_end() const;
107
108private:
109
110 typedef std::map<std::string, T> TMap;
111 typedef std::map<T, std::string> TBackMap;
112
113 bool m_last_is_default;
114 TMap m_table;
115 TBackMap m_back_table;
116 THelpMap m_help;
117 T m_default;
118
119 struct Insert {
120 Insert( std::set<std::string>& result ): m_result(result)
121 {
122 }
123 void operator() (const typename TMap::value_type& v)
124 {
125 m_result.insert(v.first);
126 }
127 private:
128 std::set<std::string>& m_result;
129 };
130};
131
132
133template <typename T>
134TDictMap<T>::TDictMap(const Table *table, bool last_is_default):
135 m_last_is_default(last_is_default)
136{
137 assert(table);
138 const Table *t = table;
139
140 while (t->name) {
141 if (!m_table.insert(typename TMap::value_type(t->name, t->value)).second)
142 throw std::invalid_argument(std::string("TDictMap<T>::TDictMap:'") +
143 std::string(t->name) +
144 std::string("' already present"));
145
146 m_back_table.insert(typename TBackMap::value_type(t->value, t->name));
147 m_help.insert(typename THelpMap::value_type(t->value,
148 std::pair<std::string, std::string>(t->name, t->help ? t->help : "")));
149 ++t;
150 }
151
152 m_default = t->value;
153}
154
155template <typename T>
156T TDictMap<T>::get_value(const char *name) const
157{
158 typename TMap::const_iterator i = m_table.find(name);
159
160 if (i == m_table.end()) {
161 if (!m_last_is_default)
162 throw std::invalid_argument(std::string("TDictMap<T>::get_value: unknown key '") +
163 std::string(name) + std::string("' provided"));
164 else
165 return m_default;
166 }
167
168 return i->second;
169}
170
171template <typename T>
172const char *TDictMap<T>::get_name(T value) const
173{
174 auto i = m_back_table.find(value);
175
176 if (i == m_back_table.end()) {
177 if (!m_last_is_default || (m_default != value))
178 throw create_exception<std::invalid_argument>("TDictMap<T>::get_name: unknown value ", value, " provided");
179 else
180 return "(default)";
181 }
182
183 return i->second.c_str();
184}
185
186template <typename T>
187const char *TDictMap<T>::get_help(T value) const
188{
189 auto i = m_help.find(value);
190
191 if (i == m_help.end())
192 throw create_exception<std::invalid_argument>("TDictMap<T>::get_help: unknown value ", value, " provided");
193
194 return i->second.second.c_str();
195}
196
197template <typename T>
198const std::set<std::string> TDictMap<T>::get_name_set() const
199{
200 std::set<std::string> result;
201 std::for_each(m_table.begin(), m_table.end(), Insert(result));
202 return result;
203}
204
205template <typename T>
207{
208 return m_help.begin();
209}
210
211template <typename T>
213{
214 return m_help.end();
215}
216
218#endif
A mapper from emums to string values. - usefull for names flags.
Definition dictmap.hh:46
const char * get_help(T value) const
Definition dictmap.hh:187
THelpMap::const_iterator get_help_end() const
Definition dictmap.hh:212
std::map< T, std::pair< std::string, std::string > > THelpMap
Definition dictmap.hh:51
const char * get_name(T value) const
Definition dictmap.hh:172
TDictMap(const Table *table, bool last_is_default=false)
Definition dictmap.hh:134
THelpMap::const_iterator get_help_begin() const
Definition dictmap.hh:206
T get_value(const char *name) const
Definition dictmap.hh:156
const std::set< std::string > get_name_set() const
Definition dictmap.hh:198
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
const T value
parameter value
Definition dictmap.hh:59
const char *const name
parameter name
Definition dictmap.hh:57
const char *const help
help text
Definition dictmap.hh:61