| 1 |
// $Id$ |
| 2 |
|
| 3 |
//============================================================================= |
| 4 |
/** |
| 5 |
* @file b_enumerator.h |
| 6 |
* |
| 7 |
* @author Fukasawa Mitsuo |
| 8 |
* |
| 9 |
* |
| 10 |
* Copyright (C) 2004 BEE Co.,Ltd. All rights reserved. |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or |
| 13 |
* modify it under the terms of the GNU General Public License |
| 14 |
* as published by the Free Software Foundation; either version 2 |
| 15 |
* of the License, or (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 |
*/ |
| 26 |
//============================================================================= |
| 27 |
|
| 28 |
#ifndef JYUGEM_BEE_ENUMERATOR_H |
| 29 |
#define JYUGEM_BEE_ENUMERATOR_H |
| 30 |
|
| 31 |
#include <vector> |
| 32 |
#include <list> |
| 33 |
#include <map> |
| 34 |
#include <stack> |
| 35 |
#include <set> |
| 36 |
#include <string> |
| 37 |
#include <algorithm> |
| 38 |
#include <iostream> |
| 39 |
using namespace std; |
| 40 |
|
| 41 |
|
| 42 |
// |
| 43 |
// Enumerate Type |
| 44 |
// |
| 45 |
class b_enumerator |
| 46 |
{ |
| 47 |
public: |
| 48 |
|
| 49 |
// Enumerate element |
| 50 |
class enum_element |
| 51 |
{ |
| 52 |
friend class b_enumerator; |
| 53 |
public: |
| 54 |
enum_element(const char * name, int num) |
| 55 |
: m_name(name), m_num(num) {} |
| 56 |
enum_element(int num, const char * name) |
| 57 |
: m_num(num), m_name(name) {} |
| 58 |
enum_element(const enum_element& rhs) |
| 59 |
{ |
| 60 |
m_name = rhs.m_name; |
| 61 |
m_num = rhs.m_num; |
| 62 |
} |
| 63 |
enum_element& operator=(const enum_element& rhs) |
| 64 |
{ |
| 65 |
if (this == &rhs) |
| 66 |
return *this; |
| 67 |
m_name = rhs.m_name; |
| 68 |
m_num = rhs.m_num; |
| 69 |
return *this; |
| 70 |
} |
| 71 |
|
| 72 |
int value() const { return m_num; } |
| 73 |
const char * name() const { return m_name; } |
| 74 |
|
| 75 |
protected: |
| 76 |
enum_element() : m_name(0), m_num(0) {} |
| 77 |
|
| 78 |
protected: |
| 79 |
const char * m_name; |
| 80 |
int m_num; |
| 81 |
}; |
| 82 |
|
| 83 |
// |
| 84 |
typedef map<string, int, std::less<string>, allocator<int> > map_by_name; |
| 85 |
typedef map<int, string, std::less<int>, allocator<string> > |
| 86 |
map_by_int; |
| 87 |
|
| 88 |
// Constructor |
| 89 |
b_enumerator(const char * name) : m_name(name) {} |
| 90 |
b_enumerator(const char * name, const enum_element * initv) : m_name(name) |
| 91 |
{ |
| 92 |
while (initv->m_name == NULL) |
| 93 |
{ |
| 94 |
m_named.insert(std::make_pair(initv->m_name, initv->m_num)); |
| 95 |
m_numberd.insert(std::make_pair(initv->m_num, initv->m_name)); |
| 96 |
initv++; |
| 97 |
} |
| 98 |
} |
| 99 |
b_enumerator(const char *, vector<char *>& initv) |
| 100 |
{ |
| 101 |
for (size_t i = 0; i < initv.size(); i++) |
| 102 |
{ |
| 103 |
m_named.insert(std::make_pair(initv[i], i)); |
| 104 |
m_numberd.insert(std::make_pair(i, initv[i])); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
b_enumerator(const b_enumerator& rhs) |
| 109 |
{ |
| 110 |
m_name = rhs.m_name; |
| 111 |
m_named = rhs.m_named; |
| 112 |
m_numberd = rhs.m_numberd; |
| 113 |
} |
| 114 |
|
| 115 |
// Destructor |
| 116 |
virtual ~b_enumerator() {} |
| 117 |
|
| 118 |
// Get/Set member variables |
| 119 |
const string& name() const { return m_name; } |
| 120 |
const char * name_p() const { return m_name.c_str(); } |
| 121 |
|
| 122 |
// Add enumerator element |
| 123 |
int add(const string& mbr, int num) |
| 124 |
{ |
| 125 |
pair<map_by_name::iterator, bool> mnge_name; |
| 126 |
mnge_name = m_named.insert(std::make_pair(mbr, num)); |
| 127 |
if (mnge_name.second == false) |
| 128 |
{ // member exist already |
| 129 |
return -1; |
| 130 |
} |
| 131 |
pair<map_by_int::iterator, bool> mnge_num; |
| 132 |
mnge_num = m_numberd.insert(std::make_pair(num, mbr)); |
| 133 |
if (mnge_num.second == false) |
| 134 |
{ // member exist already, erase the member in named map |
| 135 |
m_named.erase(mnge_name.first); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
// Get enum number by name |
| 142 |
bool stringToNum(const string& elm, int& retnum) |
| 143 |
{ |
| 144 |
map_by_name::iterator iter = m_named.find(elm); |
| 145 |
if (iter == m_named.end()) |
| 146 |
{ |
| 147 |
return false; |
| 148 |
} |
| 149 |
retnum = iter->second; |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
// Get enum name by number |
| 154 |
bool numToString(int num, string& ename) |
| 155 |
{ |
| 156 |
map_by_int::iterator iter = m_numberd.find(num); |
| 157 |
if (iter == m_numberd.end()) |
| 158 |
{ |
| 159 |
return false; |
| 160 |
} |
| 161 |
ename = iter->second; |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
static b_enumerator * parse(const char * name, const char * str); |
| 167 |
|
| 168 |
string toString() |
| 169 |
{ |
| 170 |
string result = "{"; |
| 171 |
bool first = true; |
| 172 |
map_by_int::iterator iter = m_numberd.begin(); |
| 173 |
for ( ; iter != m_numberd.end(); iter++) |
| 174 |
{ |
| 175 |
int num = iter->first; |
| 176 |
if (first) |
| 177 |
first = false; |
| 178 |
else |
| 179 |
result += ", "; |
| 180 |
char buf[64]; |
| 181 |
sprintf(buf, " = %d", num); |
| 182 |
result += iter->second; |
| 183 |
result += buf; |
| 184 |
} |
| 185 |
result += "};"; |
| 186 |
return result; |
| 187 |
} |
| 188 |
|
| 189 |
// |
| 190 |
protected: |
| 191 |
string m_name; |
| 192 |
map_by_name m_named; |
| 193 |
map_by_int m_numberd; |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
#endif // JYUGEM_BEE_ENUMERATOR_H |