plptools
Loading...
Searching...
No Matches
Enum.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 2000 Henner Zeller <hzeller@to.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20#include "config.h"
21
22#include <cstring>
23
24#include "Enum.h"
25
26using namespace std;
27
28void EnumBase::i2sMapper::add(long i, const char* s) {
29 stringMap.insert(pair<long, const char* const>(i, s));
30}
31
32string EnumBase::i2sMapper::lookup (long i) const {
33 i2s_map_t::const_iterator searchPtr = stringMap.find(i);
34
35 if (searchPtr == stringMap.end())
36 return "[OUT-OF-RANGE]";
37 /*
38 * now combine the probably the multiple strings belonging to this
39 * integer
40 */
41 string result;
42 for (i = stringMap.count(i); i > 0 ; --i, ++searchPtr) {
43 // this should be the case:
44 assert(searchPtr != stringMap.end());
45 if (result.length() != 0)
46 result += string(",");
47 result += string(searchPtr->second);
48 }
49 return result;
50}
51
52
53long EnumBase::i2sMapper::lookup (const char *s) const {
54 /*
55 * look up a specific string.
56 * Since speed does not matter, we just do an exhaustive
57 * search.
58 * Otherwise we would have to maintain another map
59 * mapping strings to ints .. but it's not worth the memory
60 */
61 i2s_map_t::const_iterator run = stringMap.begin();
62 while (run != stringMap.end() && strcmp(s, run->second)) {
63 ++run;
64 }
65 if (run == stringMap.end())
66 return -1;
67 return run->first;
68}
69
70bool EnumBase::i2sMapper::inRange (long i) const {
71 return (stringMap.find(i) != stringMap.end());
72}
bool inRange(long) const
returns true, if we have an representation for the given integer.
Definition: Enum.cc:70
void add(long, const char *)
adds a new int -> string mapping Does NOT take over responsibility for the pointer (i....
Definition: Enum.cc:28
i2s_map_t stringMap
just for the record.
Definition: Enum.h:73
std::string lookup(long) const
returns the string representation for this integer.
Definition: Enum.cc:32
Definition: doctest.h:522