cenum.h Source File

API: cenum.h Source File
API
cenum.h
1//==========================================================================
2// CENUM.H - part of
3// OMNeT++/OMNEST
4// Discrete System Simulation in C++
5//
6//==========================================================================
7
8/*--------------------------------------------------------------*
9 Copyright (C) 1992-2017 Andras Varga
10 Copyright (C) 2006-2017 OpenSim Ltd.
11
12 This file is distributed WITHOUT ANY WARRANTY. See the file
13 `license' for details on this and other legal matters.
14*--------------------------------------------------------------*/
15
16#ifndef __OMNETPP_CENUM_H
17#define __OMNETPP_CENUM_H
18
19#include <initializer_list>
20#include "cownedobject.h"
21
22namespace omnetpp {
23
33class SIM_API cEnum : public cOwnedObject
34{
35 private:
36 std::map<intval_t,std::string> valueToNameMap;
37 std::map<std::string,intval_t> nameToValueMap;
38 std::vector<std::string> tmpNames;
39
40 private:
41 void copy(const cEnum& other);
42
43 public:
44 // internal: helper for the Register_Enum() macro
45 cEnum *registerNames(const char *nameList);
46
47 template<typename T>
48 cEnum *registerValues(const std::initializer_list<T>& values) {
49 int i = 0;
50 for (T value : values)
51 insert((intval_t)value, tmpNames[i++].c_str());
52 tmpNames.clear();
53 return this;
54 }
55
56 template<typename T>
57 void addPairs(const std::initializer_list<std::pair<const char*,T>> pairList) {
58 for (const auto& pair : pairList)
59 insert((intval_t)pair.second, pair.first);
60 }
61
62 // internal: helper for obsolete macro Register_Enum2()
63 // usage example: e.bulkInsert("IDLE", IDLE, "BUSY", BUSY, nullptr);
64 [[deprecated("Use Register_Enum_Custom() macro instead of Register_Enum2()")]]
65 void bulkInsert(const char *name1, ...);
66
67 public:
70
73 explicit cEnum(const char *name=nullptr);
74
78 cEnum(const cEnum& cenum);
79
83 virtual ~cEnum() {}
84
89 cEnum& operator=(const cEnum& list);
91
94
99 virtual cEnum *dup() const override {return new cEnum(*this);}
100
104 virtual std::string str() const override;
106
109
112 void insert(intval_t value, const char *name);
113
118 const char *getStringFor(intval_t value);
119
124 intval_t lookup(const char *name, intval_t fallback=-1);
125
129 intval_t resolve(const char *name);
130
142 template<typename E>
143 static E resolveName(const char *name) {
144 cEnum *e = get(opp_typename(typeid(E)));
145 return static_cast<E>(e->resolve(name));
146 }
147
160 template<typename E>
161 static const char *getNameForValue(E value) {
162 cEnum *e = get(opp_typename(typeid(E)));
163 return e->getStringFor(static_cast<int>(value));
164 }
165
169 const std::map<std::string,intval_t>& getNameValueMap() const {return nameToValueMap;}
171
174
179 static cEnum *find(const char *enumName, const char *contextNamespace=nullptr);
180
184 static cEnum *get(const char *enumName, const char *contextNamespace=nullptr);
186};
187
188} // namespace omnetpp
189
190
191#endif
192
static E resolveName(const char *name)
Definition cenum.h:143
static cEnum * find(const char *enumName, const char *contextNamespace=nullptr)
cEnum(const cEnum &cenum)
const char * getStringFor(intval_t value)
static cEnum * get(const char *enumName, const char *contextNamespace=nullptr)
cEnum(const char *name=nullptr)
virtual ~cEnum()
Definition cenum.h:83
void insert(intval_t value, const char *name)
intval_t resolve(const char *name)
const std::map< std::string, intval_t > & getNameValueMap() const
Definition cenum.h:169
virtual std::string str() const override
cEnum & operator=(const cEnum &list)
static const char * getNameForValue(E value)
Definition cenum.h:161
intval_t lookup(const char *name, intval_t fallback=-1)
virtual cEnum * dup() const override
Definition cenum.h:99
int64_t intval_t
Signed integer type which is guaranteed to be at least 64 bits wide. It is used throughout the librar...
Definition simkerneldefs.h:101
SIM_API const char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.