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#pragma once
21
22#include "config.h"
23
24#include <assert.h>
25#include <map>
26#include <string>
27
28#include "plpintl.h"
29
38class EnumBase {
39protected:
51 class i2sMapper {
52 private:
57 typedef std::multimap<long, const char*> i2s_map_t;
58
70 //typedef map<const char*, long> s2i_map_t;
71
73 public:
80 void add(long, const char*);
81
87 std::string lookup(long) const;
88
95 long lookup (const char *) const;
96
101 bool inRange(long) const;
102 };
103};
104
134template<typename E>
135class Enum : private EnumBase {
136public:
137 struct sdata {
151 std::string name;
153 };
155
160
161public:
166 Enum() : value(staticData.defaultValue) {}
167
171 Enum(E init) : value(init){
172 // if this hits you and you're sure, that the
173 // value is right .. is this Enum proper
174 // initialized in the Enum<E>::sdata::sdata() ?
175 assert(inRange(init));
176 }
177
182 Enum(const std::string& s) : value(getValueFor(s)) {
183 assert(inRange(value));
184 }
185
191 inline Enum& operator = (E setval) {
192 value = setval;
193 assert(inRange(setval));
194 return *this;
195 }
196
197 inline Enum& operator = (const Enum& rhs) {
198 if (&rhs != this)
199 value = rhs.value;
200 return *this;
201 }
202
207 inline operator E () const { return value; }
208
213 std::string toString() const { return getStringFor(value); }
214
219 operator const char *() const { return toString().c_str(); }
220
232 static bool inRange(long i) {
233 return (staticData.stringRep.inRange(i));
234 }
235
240 static std::string getEnumName() { return staticData.name; }
241
246 static std::string getStringFor(E e) {
247 return staticData.stringRep.lookup((long) e);
248 }
249
254 static E getValueFor(const std::string &s) {
255 return (E) staticData.stringRep.lookup(s.c_str());
256 }
257};
258
259template<typename E> typename Enum<E>::sdata Enum<E>::staticData;
260
298#define ENUM_DEFINITION_BEGIN(EnumName, initWith) \
299 \
303template <> Enum<EnumName>::sdata::sdata() : \
304 name(#EnumName),defaultValue(initWith) {
305
306#define ENUM_DEFINITION_END(EnumName) \
307} template Enum< EnumName >::sdata Enum< EnumName >::staticData;
308
312template <typename E>
313inline std::ostream& operator << (std::ostream& out, const Enum<E> &e) {
314 return out << e.toString().c_str();
315}
std::ostream & operator<<(std::ostream &out, const Enum< E > &e)
Writes enumeration's string representation.
Definition: Enum.h:310
maps integers (typically: enumeration values) to Strings.
Definition: Enum.h:51
std::multimap< long, const char * > i2s_map_t
there can be one value, mapping to multiple strings.
Definition: Enum.h:57
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:72
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:38
Wrapper class featuring range-checking and string representation of enumerated values.
Definition: Enum.h:135
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:232
static std::string getEnumName()
returns the Name for this enumeration.
Definition: Enum.h:240
E value
The actual value hold by this instance.
Definition: Enum.h:159
static E getValueFor(const std::string &s)
returns the Value for a specific String.
Definition: Enum.h:254
static sdata staticData
Definition: Enum.h:154
Enum()
default constructor.
Definition: Enum.h:166
static std::string getStringFor(E e)
gives the String represenatation of a specific value of this Enumeration.
Definition: Enum.h:246
Enum & operator=(E setval)
assign an Enumeration of this type.
Definition: Enum.h:191
Enum(const std::string &s)
initialize with the string representation XXX: throw Exception if not found ?
Definition: Enum.h:182
Enum(E init)
initialize with Enumeration given.
Definition: Enum.h:171
std::string toString() const
returns the String representation for the value represented by this instance.
Definition: Enum.h:213
E defaultValue
Definition: Enum.h:152
i2sMapper stringRep
Definition: Enum.h:150
sdata()
The constructor of the static data part.
std::string name
Definition: Enum.h:151