Statistics
| Branch: | Revision:

root / src / scave / enumtype.h @ e1750c09

History | View | Annotate | Download (2.2 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  ENUMTYPE.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#ifndef __ENUMTYPE_H
19
#define __ENUMTYPE_H
20
21
#include <map>
22
#include <vector>
23
#include <string>
24
#include "scavedefs.h"
25
26
NAMESPACE_BEGIN
27
28
/**
29
 * Provides string representation for enums. The class basically implements
30
 * effective integer-to-string and string-to-integer mapping.
31
 */
32
class SCAVE_API EnumType
33
{
34
  private:
35
    std::map<int,std::string> valueToNameMap;
36
    std::map<std::string,int> nameToValueMap;
37
38
  public:
39
    /**
40
     * Constructor.
41
     */
42
    EnumType();
43
44
    /**
45
     * Copy constructor.
46
     */
47
    EnumType(const EnumType& list);
48
49
    /**
50
     * Destructor.
51
     */
52
    virtual ~EnumType();
53
54
    /**
55
     * Assignment operator. The name member is not copied;
56
     * see cOwnedObject's operator=() for more details.
57
     */
58
    EnumType& operator=(const EnumType& list);
59
60
    /**
61
     * Add an item to the enum. If that numeric code exist, overwrite it.
62
     */
63
    void insert(int value, const char *name);
64
65
    /**
66
     * Look up value and return string representation. Return
67
     * NULL if not found.
68
     */
69
    const char *nameOf(int value) const;
70
71
    /**
72
     * Look up string and return numeric code. If not found,
73
     * return the second argument.
74
     */
75
    int valueOf(const char *name, int fallback=-1) const;
76
77
    /**
78
     * Returns the names of the enum members sorted by their values.
79
     */
80
    std::vector<std::string> getNames() const;
81
82
    /**
83
     * String representation of the definition of this enum.
84
     */
85
    std::string str() const;
86
87
    /**
88
     * Parses the name/value pairs from a string.
89
     */
90
    void parseFromString(const char *str);
91
};
92
93
NAMESPACE_END
94
95
96
#endif