summaryrefslogtreecommitdiff
path: root/src/include/tome/enum_string_map.hpp
blob: 814827feaf5a6d76284898c149174ae905941456 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once

#include <boost/bimap.hpp>
#include <boost/noncopyable.hpp>
#include <string>
#include <cassert>

/**
 * Bidirectional mapping between enumerated values
 * and strings.
 */
template <class E>
class EnumStringMap : boost::noncopyable {

private:
	typedef boost::bimap< E, std::string > bimap_type;
	typedef typename bimap_type::value_type value_type;

	bimap_type bimap;

public:
	explicit EnumStringMap(std::initializer_list< std::pair<E, const char *> > in) {
		for (auto es : in)
		{
			bimap.insert(value_type(es.first, es.second));
		}
		// Sanity check that there were no
		// duplicate mappings.
		assert(bimap.size() == in.size());
	}

	const char *stringify(E e) const {
		auto i = bimap.left.find(e);
		assert(i != bimap.left.end() && "Missing mapping for enumerated value");
		return i->second.c_str();
	}

	E parse(const char *s) const {
		E e;
		bool result = parse(s, &e);
		assert(result && "Missing string->enum mapping");
		return e;
	}

	bool parse(const char *s, E *e) const {
		auto i = bimap.right.find(s);
		if (i == bimap.right.end())
		{
			return false;
		}

		*e = i->second;
		return true;
	}
};