plptools
Loading...
Searching...
No Matches
Enum.h
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 2000 Henner Zeller <hzeller@to.com>
5 *
6 * This program 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 2 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 along
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20#ifndef _ENUM_H_
21#define _ENUM_H_
22
23#include "config.h"
24
25#include <assert.h>
26#include <map>
27#include <string>
28
29#include "plpintl.h"
30
39class EnumBase {
40protected:
52 class i2sMapper {
53 private:
58 typedef std::multimap<long, const char*> i2s_map_t;
59
71 //typedef map<const char*, long> s2i_map_t;
72
74 public:
81 void add(long, const char*);
82
88 std::string lookup(long) const;
89
96 long lookup (const char *) const;
97
102 bool inRange(long) const;
103 };
104};
105
135template<typename E>
136class Enum : private EnumBase {
137public:
138 struct sdata {
152 std::string name;
154 };
156
161
162public:
167 Enum() : value(staticData.defaultValue) {}
168
172 Enum(E init) : value(init){
173 // if this hits you and you're sure, that the
174 // value is right .. is this Enum proper
175 // initialized in the Enum<E>::sdata::sdata() ?
176 assert(inRange(init));
177 }
178
183 Enum(const std::string& s) : value(getValueFor(s)) {
184 assert(inRange(value));
185 }
186
192 inline Enum& operator = (E setval) {
193 value = setval;
194 assert(inRange(setval));
195 return *this;
196 }
197
198 inline Enum& operator = (const Enum& rhs) {
199 if (&rhs != this)
200 value = rhs.value;
201 return *this;
202 }
203
208 inline operator E () const { return value; }
209
214 std::string toString() const { return getStringFor(value); }
215
220 operator const char *() const { return toString().c_str(); }
221
233 static bool inRange(long i) {
234 return (staticData.stringRep.inRange(i));
235 }
236
241 static std::string getEnumName() { return staticData.name; }
242
247 static std::string getStringFor(E e) {
248 return staticData.stringRep.lookup((long) e);
249 }
250
255 static E getValueFor(const std::string &s) {
256 return (E) staticData.stringRep.lookup(s.c_str());
257 }
258};
259
260template<typename E> typename Enum<E>::sdata Enum<E>::staticData;
261
299#define ENUM_DEFINITION_BEGIN(EnumName, initWith) \
300 \
304template <> Enum<EnumName>::sdata::sdata() : \
305 name(#EnumName),defaultValue(initWith) {
306
307#define ENUM_DEFINITION_END(EnumName) \
308} template Enum< EnumName >::sdata Enum< EnumName >::staticData;
309
313template <typename E>
314inline std::ostream& operator << (std::ostream& out, const Enum<E> &e) {
315 return out << e.toString().c_str();
316}
317
318#endif /* _ENUM_H_ */
std::ostream & operator<<(std::ostream &out, const Enum< E > &e)
Writes enumeration's string representation.
Definition: Enum.h:311
maps integers (typically: enumeration values) to Strings.
Definition: Enum.h:52
std::multimap< long, const char * > i2s_map_t
there can be one value, mapping to multiple strings.
Definition: Enum.h:58
bool inRange(long) const
returns true, if we have an representation for the given integer.
Definition: Enum.cc:70
void add(long, const char *)
adds a new int -> string mapping Does NOT take over responsibility for the pointer (i....
Definition: Enum.cc:28
i2s_map_t stringMap
just for the record.
Definition: Enum.h:73
std::string lookup(long) const
returns the string representation for this integer.
Definition: Enum.cc:32
the Base for the Enum template.
Definition: Enum.h:39
Wrapper class featuring range-checking and string representation of enumerated values.
Definition: Enum.h:136
static bool inRange(long i)
This static member returns true, if the integer value given fits int the range of this Enumeration.
Definition: Enum.h:233
static std::string getEnumName()
returns the Name for this enumeration.
Definition: Enum.h:241
E value
The actual value hold by this instance.
Definition: Enum.h:160
static E getValueFor(const std::string &s)
returns the Value for a specific String.
Definition: Enum.h:255
static sdata staticData
Definition: Enum.h:155
Enum()
default constructor.
Definition: Enum.h:167
static std::string getStringFor(E e)
gives the String represenatation of a specific value of this Enumeration.
Definition: Enum.h:247
Enum & operator=(E setval)
assign an Enumeration of this type.
Definition: Enum.h:192
Enum(const std::string &s)
initialize with the string representation XXX: throw Exception if not found ?
Definition: Enum.h:183
Enum(E init)
initialize with Enumeration given.
Definition: Enum.h:172
std::string toString() const
returns the String representation for the value represented by this instance.
Definition: Enum.h:214
E defaultValue
Definition: Enum.h:153
i2sMapper stringRep
Definition: Enum.h:151
sdata()
The constructor of the static data part.
std::string name
Definition: Enum.h:152