Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / enumtype.cc @ a3be1d55

History | View | Annotate | Download (3.16 KB)

1
//=========================================================================
2
//  ENUMTYPE.CC - 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
#include <sstream>
19
#include <algorithm>
20
#include "stringtokenizer.h"
21
#include "scaveutils.h"
22
#include "enumtype.h"
23

    
24
USING_NAMESPACE
25

    
26

    
27
EnumType::EnumType(const EnumType& list)
28
{
29
     operator=(list);
30
}
31

    
32
EnumType::EnumType()
33
{
34
}
35

    
36
EnumType::~EnumType()
37
{
38
}
39

    
40
EnumType& EnumType::operator=(const EnumType& other)
41
{
42
    valueToNameMap = other.valueToNameMap;
43
    nameToValueMap = other.nameToValueMap;
44
    return *this;
45
}
46

    
47
void EnumType::insert(int value, const char *name)
48
{
49
    valueToNameMap[value] = name;
50
    nameToValueMap[name] = value;
51
}
52

    
53
const char *EnumType::nameOf(int value) const
54
{
55
    std::map<int,std::string>::const_iterator it = valueToNameMap.find(value);
56
    return it==valueToNameMap.end() ? NULL : it->second.c_str();
57
}
58

    
59
int EnumType::valueOf(const char *name, int fallback) const
60
{
61
    std::map<std::string,int>::const_iterator it = nameToValueMap.find(name);
62
    return it==nameToValueMap.end() ? fallback : it->second;
63
}
64

    
65
static bool less(const std::pair<int,std::string> &left, const std::pair<int,std::string> &right)
66
{
67
    return left.first < right.first;
68
}
69

    
70
static std::string second(std::pair<int,std::string> pair)
71
{
72
    return pair.second;
73
}
74

    
75
std::vector<std::string> EnumType::getNames() const
76
{
77
    std::vector<std::pair<int,std::string> > pairs(valueToNameMap.begin(), valueToNameMap.end());
78
    std::sort(pairs.begin(), pairs.end(), less);
79
    std::vector<std::string> names(pairs.size());
80
    std::transform(pairs.begin(), pairs.end(), names.begin(), second);
81
    return names;
82
}
83

    
84
std::string EnumType::str() const
85
{
86
    std::stringstream out;
87
    for (std::map<std::string,int>::const_iterator it=nameToValueMap.begin(); it!=nameToValueMap.end(); ++it)
88
    {
89
        if (it!=nameToValueMap.begin())
90
            out << ", ";
91
        out << it->first << "=" << it->second;
92
    }
93
    return out.str();
94
}
95

    
96
void EnumType::parseFromString(const char *str)
97
{
98
    valueToNameMap.clear();
99
    nameToValueMap.clear();
100

    
101
    StringTokenizer tokenizer(str, ", ");
102
    int value = -1;
103
    while (tokenizer.hasMoreTokens())
104
    {
105
        std::string nameValue = tokenizer.nextToken();
106
        std::string::size_type pos = nameValue.find('=');
107
        if (pos == std::string::npos)
108
        {
109
            insert(++value, nameValue.c_str());
110
        }
111
        else
112
        {
113
            std::string name = nameValue.substr(0, pos);
114
            std::string valueStr = nameValue.substr(pos+1);
115
            if (!parseInt(valueStr.c_str(), value))
116
                throw opp_runtime_error("Enum value must be an int, found: %s", valueStr.c_str());
117
            insert(value, name.c_str());
118
        }
119
    }
120
}