libcommonc++  0.7
EnumTraits.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_EnumTraits_hxx
24 #define __ccxx_EnumTraits_hxx
25 
26 #include <commonc++/Common.h++>
27 
28 #include <cstring>
29 #include <utility>
30 #include <vector>
31 
32 namespace ccxx {
33 
41 template<typename E> class EnumTraits
42 {
43  public:
44 
46  static E min();
47 
49  static E max();
50 
52  static E defval();
53 };
54 
66 template<typename T, typename E> E enum_cast(T val)
67 {
68  if((val >= static_cast<T>(EnumTraits<E>::min()))
69  && (val <= static_cast<T>(EnumTraits<E>::max())))
70  return static_cast<E>(val);
71  else
72  return EnumTraits<E>::defval();
73 }
74 
87 template<typename T, typename E> E enum_cast(T val, E defval)
88 {
89  if((val >= static_cast<T>(EnumTraits<E>::min()))
90  && (val <= static_cast<T>(EnumTraits<E>::max())))
91  return static_cast<E>(val);
92  else
93  return defval;
94 }
95 
111 template<typename E> class EnumMap
112  : private std::vector<std::pair<E, const char *> >
113 {
114  public:
115 
117  EnumMap() { }
118 
120  ~EnumMap() { }
121 
123  EnumMap& add(E value, const char* str)
124  {
125  this->push_back(make_pair(value, str));
126  return(*this);
127  }
128 
136  E valueForString(const char* str) const
137  {
138  for(typename Entries::const_iterator iter = this->begin();
139  iter != this->end();
140  ++iter)
141  {
142  if(std::strcmp(str, this->second))
143  return(this->first);
144  }
145 
146  return(EnumTraits<E>::defval());
147  }
148 
156  const char* stringForValue(E value) const
157  {
158  for(typename Entries::const_iterator iter = this->begin();
159  iter != this->end();
160  ++iter)
161  {
162  if(value == iter->first)
163  return(this->second);
164  }
165 
166  return(NULL);
167  }
168 
169  private:
170 
171  typedef std::vector<std::pair<E, const char*> > Entries;
172 };
173 
174 } // namespace ccxx
175 
176 #endif // __ccxx_EnumTraits_hxx
EnumMap()
Constructor.
Definition: EnumTraits.h++:117
EnumMap & add(E value, const char *str)
Add a mapping.
Definition: EnumTraits.h++:123
E enum_cast(T val)
Safe enumeration cast.
Definition: EnumTraits.h++:66
~EnumMap()
Destructor.
Definition: EnumTraits.h++:120
static E defval()
Get the default enumeration value.
const char * stringForValue(E value) const
Map an enum value to its string.
Definition: EnumTraits.h++:156
E valueForString(const char *str) const
Map a string to its enum value.
Definition: EnumTraits.h++:136
A class of functions that describe traits of an enumeration.
Definition: EnumTraits.h++:41
static E max()
Get the maximum enumeration value.
Definition: AllocationMap.c++:25
static E min()
Get the minimum enumeration value.
A bidirectional mapping between enum values and static string constants.
Definition: EnumTraits.h++:111