alaCarte Maps
Renderer for OpenStreetMap tiles
typedId.hpp
Go to the documentation of this file.
1 
21 #pragma once
22 #ifndef TYPEDID_HPP
23 #define TYPEDID_HPP
24 
25 /*
26  * =====================================================================================
27  *
28  * Filename: typedId.hpp
29  *
30  * Description: Utility class for rectangle operations.
31  *
32  * =====================================================================================
33  */
34 
35 
36 template<unsigned short TypeNumber>
37 class TypedId
38 {
40 public:
41  static const unsigned short type_number = TypeNumber;
42 
43  inline explicit TypedId()
44  : id(-1)
45  {
46  }
47 
48  inline explicit TypedId(unsigned int id)
49  : id(id)
50  {
51  }
52 
53  inline unsigned int getRaw() const
54  {
55  return id;
56  }
57 
58 
59  inline bool operator ==(const TypedId& other) const
60  {
61  return other.id == id;
62  }
63 
64  inline bool operator !=(const TypedId& other) const
65  {
66  return other.id != id;
67  }
68 
69  inline bool operator <(const TypedId& right) const
70  {
71  return id < right.id;
72  }
73 
74  inline bool operator >(const TypedId& right) const
75  {
76  return id > right.id;
77  }
78 
79  inline bool operator >=(const TypedId& right) const
80  {
81  return id >= right.id;
82  }
83 
84  inline bool operator <=(const TypedId& right) const
85  {
86  return id <= right.id;
87  }
88 private:
89  template<typename Archive>
90  void serialize(Archive &ar, const unsigned int version){
91  ar & id;
92  }
93 
94  unsigned int id;
95 };
96 
97 template<unsigned short TypeNumber>
98 inline std::size_t hash_value(TypedId<TypeNumber> id)
99 {
100  return static_cast<std::size_t>(id.getRaw());
101 }
102 
103 template<unsigned short TypeNumber>
104 inline std::ostream& operator <<(std::ostream& o, const TypedId<TypeNumber>& id)
105 {
106  o << id.getRaw();
107  return o;
108 }
109 
110 
111 class AnyId
112 {
113 public:
114  template<unsigned int TypeNumber>
115  inline AnyId(const TypedId<TypeNumber>& id)
116  : type(TypedId<TypeNumber>::type_number)
117  , id(id.getRaw())
118  {
119 
120  }
121 
122  inline bool operator ==(const AnyId& other) const
123  {
124  return id == other.id && type == other.type;
125  }
126 
127  template<typename IdType>
128  inline IdType is() const
129  {
130  return IdType::type_number == type;
131  }
132 
133 
134  template<typename IdType>
135  inline IdType get() const
136  {
137  assert(type == IdType::type_number);
138 
139  return IdType(id);
140  }
141 
142 private:
143  unsigned short type;
144  unsigned int id;
145 };
146 
147 
148 
149 
150 
151 
152 #endif
unsigned short type
Definition: typedId.hpp:143
bool operator!=(const TypedId &other) const
Definition: typedId.hpp:64
AnyId(const TypedId< TypeNumber > &id)
Definition: typedId.hpp:115
TypedId()
Definition: typedId.hpp:43
bool operator>=(const TypedId &right) const
Definition: typedId.hpp:79
unsigned int id
Definition: typedId.hpp:94
unsigned int getRaw() const
Definition: typedId.hpp:53
bool operator>(const TypedId &right) const
Definition: typedId.hpp:74
void serialize(Archive &ar, const unsigned int version)
Definition: typedId.hpp:90
TypedId(unsigned int id)
Definition: typedId.hpp:48
friend class boost::serialization::access
Definition: typedId.hpp:39
std::size_t hash_value(TypedId< TypeNumber > id)
Definition: typedId.hpp:98
static const unsigned short type_number
Definition: typedId.hpp:41
IdType is() const
Definition: typedId.hpp:128
bool operator==(const TypedId &other) const
Definition: typedId.hpp:59
bool operator<=(const TypedId &right) const
Definition: typedId.hpp:84
bool operator<(const TypedId &right) const
Definition: typedId.hpp:69
unsigned int id
Definition: typedId.hpp:144