FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
enum_string_map.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (1 Mar. 2011)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 #include "hash_fnv.h"
19 
20 #include <unordered_map>
21 #include <vector>
22 
23 namespace cpp {
24 
45 template<typename ENUM>
47 {
48 public:
49  typedef std::pair<ENUM, const char*> Mapping;
50 
51  EnumStringMap();
52 
53  void map(ENUM eval, const char* str);
54 
55  std::size_t size() const;
56  std::size_t index(ENUM eval) const;
57 
58  ENUM valueAt(std::size_t i) const;
59  ENUM value(const char* str) const;
60  const char* string(ENUM eval) const;
61 
62  Mapping mapping(std::size_t i) const;
63 
64 private:
65  struct StrEqual
66  {
67  bool operator()(const char* lhs, const char* rhs) const;
68  };
69 
70  typedef cpp::hash_fnv_1a<> StrHash;
71 
72  typename std::vector<Mapping>::const_iterator findCppSql(ENUM eval) const;
73 
74  std::unordered_map<const char*, ENUM, StrHash, StrEqual> m_strEnumMap;
75  std::vector<Mapping> m_mappingVec;
76 };
77 
78 } // namespace cpp
79 
80 // --
81 // -- Implementation
82 // --
83 
84 #include <algorithm>
85 #include <cassert>
86 #include <cstring>
87 
88 namespace cpp {
89 
90 template<typename ENUM>
92 {
93 }
94 
95 template<typename ENUM>
96 void EnumStringMap<ENUM>::map(ENUM eval, const char* str)
97 {
98  m_strEnumMap.emplace(str, eval);
99  m_mappingVec.emplace_back(eval, str);
100 }
101 
102 template<typename ENUM>
103 std::size_t EnumStringMap<ENUM>::size() const
104 {
105  return m_mappingVec.size();
106 }
107 
108 template<typename ENUM>
109 std::size_t EnumStringMap<ENUM>::index(ENUM eval) const
110 {
111  return this->findCppSql(eval) - m_mappingVec.begin();
112 }
113 
114 template<typename ENUM>
115 ENUM EnumStringMap<ENUM>::valueAt(std::size_t i) const
116 {
117  assert(i < m_mappingVec.size());
118  return m_mappingVec.at(i).first;
119 }
120 
121 template<typename ENUM>
122 ENUM EnumStringMap<ENUM>::value(const char *str) const
123 {
124  auto it = m_strEnumMap.find(str);
125  assert(it != m_strEnumMap.cend());
126  return (*it).second;
127 }
128 
129 template<typename ENUM>
130 const char *EnumStringMap<ENUM>::string(ENUM eval) const
131 {
132  auto it = this->findCppSql(eval);
133  return (*it).second;
134 }
135 
136 template<typename ENUM>
138 {
139  assert(i < m_mappingVec.size());
140  return m_mappingVec.at(i);
141 }
142 
143 template<typename ENUM>
144 typename std::vector<typename EnumStringMap<ENUM>::Mapping>::const_iterator
145 EnumStringMap<ENUM>::findCppSql(ENUM eval) const
146 {
147  auto it = std::find_if(m_mappingVec.cbegin(), m_mappingVec.cend(),
148  [=] (const Mapping& map) { return map.first == eval; } );
149  assert(it != m_mappingVec.cend());
150  return it;
151 }
152 
153 template<typename ENUM>
154 bool EnumStringMap<ENUM>::StrEqual::operator()(const char *lhs, const char *rhs) const
155 {
156  return std::strcmp(lhs, rhs) == 0;
157 }
158 
159 } // namespace cpp
EnumStringMap()
Definition: enum_string_map.h:91
Definition: basic_shared_pointer.h:20
Mapping mapping(std::size_t i) const
Definition: enum_string_map.h:137
void map(ENUM eval, const char *str)
Definition: enum_string_map.h:96
Provides mapping between a C++ enum type values and C strings.
Definition: enum_string_map.h:46
ENUM valueAt(std::size_t i) const
Definition: enum_string_map.h:115
std::pair< ENUM, const char * > Mapping
Definition: enum_string_map.h:49
const char * string(ENUM eval) const
Definition: enum_string_map.h:130
ENUM value(const char *str) const
Definition: enum_string_map.h:122
Definition: hash_fnv.h:62
std::size_t size() const
Definition: enum_string_map.h:103
std::size_t index(ENUM eval) const
Definition: enum_string_map.h:109