FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
TypeIDMap.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Oct 15, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 Richard Bateman, Firebreath development team
13 \**********************************************************/
14 
15 #pragma once
16 #ifndef H_FB_STRINGIDMAP
17 #define H_FB_STRINGIDMAP
18 
19 #include <map>
20 #include <vector>
21 #include <string>
22 
23 #include "APITypes.h"
24 
25 namespace FB {
26 
32  template <class IDTYPE>
33  class TypeIDMap
34  {
35  protected:
36  class compare_variants { // simple comparison function
37  public:
38  bool operator()(const FB::variant &x, const FB::variant &y) const
39  {
40  return x < y;
41  }
42  };
43 
44  class compare_numerics { // simple numeric comparison function
45  public:
46  bool operator()(const IDTYPE &x, const IDTYPE &y) const
47  {
48  long long x2 = (long long)(x);
49  long long y2 = (long long)(y);
50  return x2 < y2;
51  }
52  };
53 
59  typedef std::map<IDTYPE, FB::variant, compare_numerics> IdVariantMap;
60 
66  typedef std::map<FB::variant, IDTYPE, compare_variants> VariantIdMap;
67 
68  public:
69 
77  TypeIDMap(IDTYPE startValue) : nextId((unsigned long)startValue) { }
78  virtual ~TypeIDMap() { }
79 
90  void setIdForValue(IDTYPE idt, const variant& val)
91  {
92  if (m_idVariant.find(idt) != m_idVariant.end()
93  || m_variantId.find(val) != m_variantId.end()) {
94  throw std::runtime_error("ID or value already defined");
95  }
96  m_variantId[val] = idt;
97  m_idVariant[idt] = val;
98  }
99 
110  IDTYPE getIdForValue(const variant& val)
111  {
112  typename VariantIdMap::iterator it = m_variantId.find(val);
113 
114  if (it != m_variantId.end()) {
115  return it->second;
116  } else {
117  IDTYPE newId = (IDTYPE)nextId++;
118  m_variantId[val] = newId;
119  m_idVariant[newId] = val;
120  return newId;
121  }
122  }
123 
133  const std::type_info& idGetType(IDTYPE idt)
134  {
135  typename IdVariantMap::iterator it = m_idVariant.find(idt);
136 
137  if (it != m_idVariant.end()) {
138  return it->second.get_type();
139  } else {
140  return typeid(void);
141  }
142  }
143 
153  bool idExists(IDTYPE idt)
154  {
155  return m_idVariant.find(idt) != m_idVariant.end();
156  }
157 
172  template <class T>
173  bool idIsType(IDTYPE idt)
174  {
175  typename IdVariantMap::iterator it = m_idVariant.find(idt);
176 
177  if (it != m_idVariant.end()) {
178  return it->second.get_type() == typeid(T);
179  } else {
180  return false;
181  }
182  }
183 
196  template <class T>
197  T getValueForId(IDTYPE idt)
198  {
199  typename IdVariantMap::iterator it = m_idVariant.find(idt);
200 
201  if (it != m_idVariant.end()) {
202  T retVal = (*it).second.template convert_cast<T>();
203  return retVal;
204  } else {
205  throw FB::bad_variant_cast(typeid(void), typeid(T));
206  }
207  }
208 
209  protected:
210  IdVariantMap m_idVariant;
211  VariantIdMap m_variantId;
212  unsigned long nextId;
213 
214  };
215 
216 };
217 
218 #endif // H_FB_STRINGIDMAP
219 
Bidirectional map between an identifier and a variant.
Definition: TypeIDMap.h:33
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
std::map< IDTYPE, FB::variant, compare_numerics > IdVariantMap
Defines an alias representing the map from the id type to a variant.
Definition: TypeIDMap.h:59
Thrown when variant::cast<type> or variant::convert_cast<type> fails because the type of the value st...
Definition: variant.h:133
IDTYPE getIdForValue(const variant &val)
Gets the identifier assigned to the given value; if none is yet assigned, assigns a sequential identi...
Definition: TypeIDMap.h:110
bool idIsType(IDTYPE idt)
Queries if the provided identifier is assigned to a value of the given type.
Definition: TypeIDMap.h:173
std::map< FB::variant, IDTYPE, compare_variants > VariantIdMap
Defines an alias representing the map from a variant to the id.
Definition: TypeIDMap.h:66
const std::type_info & idGetType(IDTYPE idt)
Returns the type of the value the given identifier is assigned to.
Definition: TypeIDMap.h:133
TypeIDMap(IDTYPE startValue)
Constructor.
Definition: TypeIDMap.h:77
void setIdForValue(IDTYPE idt, const variant &val)
Sets an identifier for value.
Definition: TypeIDMap.h:90
T getValueForId(IDTYPE idt)
Gets the value assigned to the identifier.
Definition: TypeIDMap.h:197
bool idExists(IDTYPE idt)
Queries if a given identifier has been assigned to anything.
Definition: TypeIDMap.h:153