Statistics
| Branch: | Revision:

root / src / sim / cenum.cc @ e26d3d25

History | View | Annotate | Download (3.35 KB)

1
//=========================================================================
2
//  CENUM.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//   Member functions of
8
//    cEnum : effective integer-to-string mapping
9
//
10
//  Author: Andras Varga
11
//
12
//=========================================================================
13

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

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

    
22
#include <stdio.h>           // sprintf, fprintf
23
#include <string.h>          // memcmp, memcpy, memset
24
#include <sstream>
25
#include "globals.h"
26
#include "csimulation.h"
27
#include "cenum.h"
28
#include "cexception.h"
29
#include "cstringtokenizer.h"
30

    
31
USING_NAMESPACE
32

    
33

    
34
Register_Class(cEnum);
35

    
36
cEnum::cEnum(const cEnum& list) : cOwnedObject()
37
{
38
     setName(list.getName());
39
     operator=(list);
40
}
41

    
42
cEnum::cEnum(const char *name) : cOwnedObject(name, false)
43
{
44
}
45

    
46
cEnum::~cEnum()
47
{
48
}
49

    
50
cEnum& cEnum::operator=(const cEnum& other)
51
{
52
    cOwnedObject::operator=(other);
53
    valueToNameMap = other.valueToNameMap;
54
    nameToValueMap = other.nameToValueMap;
55
    return *this;
56
}
57

    
58
std::string cEnum::info() const
59
{
60
    if (valueToNameMap.size()==0)
61
        return std::string("empty");
62
    return str();
63
}
64

    
65
void cEnum::insert(int value, const char *name)
66
{
67
    valueToNameMap[value] = name;
68
    nameToValueMap[name] = value;
69
}
70

    
71
const char *cEnum::getStringFor(int value)
72
{
73
    std::map<int,std::string>::const_iterator it = valueToNameMap.find(value);
74
    return it==valueToNameMap.end() ? NULL : it->second.c_str();
75
}
76

    
77
int cEnum::lookup(const char *name, int fallback)
78
{
79
    std::map<std::string,int>::const_iterator it = nameToValueMap.find(name);
80
    return it==nameToValueMap.end() ? fallback : it->second;
81
}
82

    
83
cEnum *cEnum::find(const char *name)
84
{
85
    return dynamic_cast<cEnum *>(enums.getInstance()->lookup(name));
86
}
87

    
88
cEnum *cEnum::get(const char *name)
89
{
90
    cEnum *p = find(name);
91
    if (!p)
92
        throw cRuntimeError("Enum \"%s\" not found -- its declaration may be missing "
93
                            "from .msg files, or the code was not linked in", name);
94
    return p;
95
}
96

    
97
cEnum *cEnum::registerNames(const char *nameList)
98
{
99
    tmpNames.clear();
100
    cStringTokenizer tokenizer(nameList, "(), ");
101
    while (tokenizer.hasMoreTokens())
102
    {
103
        const char *token = tokenizer.nextToken();
104
        if (strstr(token, "::"))
105
            token = strstr(token, "::")+2;
106
        tmpNames.push_back(token);
107
    }
108
    return this;
109
}
110

    
111
cEnum *cEnum::registerValues(int firstValue, ...)
112
{
113
    ASSERT(!tmpNames.empty());
114

    
115
    va_list va;
116
    va_start(va, firstValue);
117
    insert(firstValue, tmpNames[0].c_str());
118
    for (int i=1; i < (int)tmpNames.size(); i++)
119
    {
120
        int value = va_arg(va, int);
121
        insert(value, tmpNames[i].c_str());
122
    }
123
    va_end(va);
124
    tmpNames.clear();
125
    return this;
126
}
127

    
128
std::string cEnum::str() const
129
{
130
    std::stringstream out;
131
    for (std::map<std::string,int>::const_iterator it=nameToValueMap.begin(); it!=nameToValueMap.end(); ++it)
132
    {
133
        if (it!=nameToValueMap.begin())
134
            out << ", ";
135
        out << it->first << "=" << it->second;
136
    }
137
    return out.str();
138
}
139

    
140