Statistics
| Branch: | Revision:

root / include / cenum.h @ 47c4b975

History | View | Annotate | Download (3.31 KB)

1
//==========================================================================
2
//  CENUM.H - part of
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//
8
//  Declaration of the following classes:
9
//    cEnum : effective integer-to-string mapping
10
//
11
//==========================================================================
12

    
13
/*--------------------------------------------------------------*
14
  Copyright (C) 1992-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20

    
21
#ifndef __CENUM_H
22
#define __CENUM_H
23

    
24
#include "cownedobject.h"
25

    
26
NAMESPACE_BEGIN
27

    
28
#define Register_Enum(NAME, VALUES)  \
29
    EXECUTE_ON_STARTUP(enums.getInstance()->add((new cEnum(#NAME))->registerNames(#VALUES)->registerValues VALUES))
30

    
31
/**
32
 * Provides string representation for enums. The class basically implements
33
 * effective integer-to-string and string-to-integer mapping. Primary usage
34
 * is to support displaying symbolic names for integer values that represent
35
 * some code (such as an enum or \#define).
36
 *
37
 * @ingroup Internals
38
 */
39
class SIM_API cEnum : public cOwnedObject
40
{
41
  private:
42
     std::map<int,std::string> valueToNameMap;
43
     std::map<std::string,int> nameToValueMap;
44
     std::vector<std::string> tmpNames;
45

    
46
  public:
47
    // internal: helper for the Register_Enum() macro
48
    cEnum *registerNames(const char *nameList);
49
    // internal: helper for the Register_Enum() macro
50
    cEnum *registerValues(int first, ...);
51

    
52
  public:
53
    /** @name Constructors, destructor, assignment. */
54
    //@{
55
    /**
56
     * Constructor.
57
     */
58
    cEnum(const char *name=NULL);
59

    
60
    /**
61
     * Copy constructor.
62
     */
63
    cEnum(const cEnum& cenum);
64

    
65
    /**
66
     * Destructor.
67
     */
68
    virtual ~cEnum();
69

    
70
    /**
71
     * Assignment operator. The name member is not copied;
72
     * see cOwnedObject's operator=() for more details.
73
     */
74
    cEnum& operator=(const cEnum& list);
75
    //@}
76

    
77
    /** @name Redefined cObject member functions. */
78
    //@{
79

    
80
    /**
81
     * Creates and returns an exact copy of this object.
82
     * See cObject for more details.
83
     */
84
    virtual cEnum *dup() const  {return new cEnum(*this);}
85

    
86
    /**
87
     * Produces a one-line description of the object's contents.
88
     * See cObject for more details.
89
     */
90
    virtual std::string info() const;
91
    //@}
92

    
93
    /** @name Insertion and lookup. */
94
    //@{
95
    /**
96
     * Add an item to the enum. If that numeric code exist, overwrite it.
97
     */
98
    void insert(int value, const char *name);
99

    
100
    /**
101
     * Look up value and return string representation. Return
102
     * NULL if not found.
103
     */
104
    const char *getStringFor(int value);
105

    
106
    /**
107
     * Look up string and return numeric code. If not found, return
108
     * second argument (or -1).
109
     */
110
    int lookup(const char *name, int fallback=-1);
111
    //@}
112

    
113
    /**
114
     * Returns a textual representation of this enum.
115
     */
116
    std::string str() const;
117

    
118
    /**
119
     * Finds a registered enum by name. Returns NULL if not found.
120
     */
121
    static cEnum *find(const char *name);
122

    
123
    /**
124
     * Returns the enum with the given name. Throws an error if not found.
125
     */
126
    static cEnum *get(const char *name);
127
};
128

    
129
NAMESPACE_END
130

    
131

    
132
#endif
133