[go: up one dir, main page]

Menu

[r789]: / mia2 / mia / core / dictmap.hh  Maximize  Restore  History

Download this file

114 lines (94 with data), 2.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -*- mia-c++ -*-
* Copyright (c) 2007 Gert Wollny <gert dot wollny at acm dot org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef mia_core_dictmap_hh
#define mia_core_dictmap_hh
#include <string>
#include <set>
#include <stdexcept>
#include <mia/core/defines.hh>
/**
A mapper from emums to string values. - usefull for names flags
*/
template <typename T>
class TDictMap {
public:
/**
The initialisation table. The last entry must have the name pointer pointing to 0.
*/
typedef struct {
const char *name;
const T value;
} Table;
/// Create the map by providing an initialisation map
TDictMap(const Table *table);
/**
\param name
\returns corresponding flag
\remark throws std::invalid_argument if the name is unknown
*/
T get_value(const char *name) const;
/**
\param name
\returns corresponding flag
\remark throws std::invalid_argument if the value is unknown
*/
const char *get_name(T value) const;
/// \returns a set of all available names
const std::set<std::string> get_name_set() const;
private:
const Table *_M_table;
};
template <typename T>
TDictMap<T>::TDictMap(const Table *table):
_M_table(table)
{
}
template <typename T>
T TDictMap<T>::get_value(const char *name) const
{
const Table *t = _M_table;
while (t->name && strcmp(t->name, name))
++t;
if ( !t->name) {
throw std::invalid_argument(std::string("TDictMap<T>::get_name: unknown name '")+std::string(name)+std::string("' provided"));
}
return t->value;
}
template <typename T>
const char *TDictMap<T>::get_name(T value) const
{
const Table *t = _M_table;
while (t->name && t->value != value)
++t;
if (!t->name)
throw std::invalid_argument("TDictMap<T>::get_name: unknown value provided");
return t->name;
}
template <typename T>
const std::set<std::string> TDictMap<T>::get_name_set() const
{
std::set<std::string> result;
const Table *t = _M_table;
while (t->name != NULL) {
result.insert(t->name);
++t;
}
return result;
}
#endif