OMNeT++ API 6.1
Discrete Event Simulation Library
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 
22 namespace omnetpp {
23 
33 class 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:
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 
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 
134  const std::map<std::string,intval_t>& getNameValueMap() const {return nameToValueMap;}
136 
144  static cEnum *find(const char *enumName, const char *contextNamespace=nullptr);
145 
149  static cEnum *get(const char *enumName, const char *contextNamespace=nullptr);
151 };
152 
153 } // namespace omnetpp
154 
155 
156 #endif
157 
omnetpp::cEnum::~cEnum
virtual ~cEnum()
Definition: cenum.h:83
omnetpp::cEnum::dup
virtual cEnum * dup() const override
Definition: cenum.h:99
omnetpp::cEnum::getNameValueMap
const std::map< std::string, intval_t > & getNameValueMap() const
Definition: cenum.h:134
omnetpp::cEnum
Provides string representation for enums.
Definition: cenum.h:33
omnetpp::intval_t
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
omnetpp::cOwnedObject
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:105