OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
TypeSafeId.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_TypeSafeId_h_GUID_137CA336_382A_4796_7735_4521F02D5AC2
26 #define INCLUDED_TypeSafeId_h_GUID_137CA336_382A_4796_7735_4521F02D5AC2
27 
28 // Internal Includes
29 #include <osvr/Util/StdInt.h>
31 
32 // Library/third-party includes
33 // - none
34 
35 // Standard includes
36 #include <limits>
37 
38 namespace osvr {
39 namespace util {
40 
41  template <class Tag> class TypeSafeId;
42  template <class Tag> class TypeSafeIdBase;
43  template <class Tag> class TypeSafeIdRefAccessorBase;
46  namespace typesafeid_traits {
49  template <typename Tag> struct WrappedType { typedef uint32_t type; };
50 
55  enum { value = true };
56  };
57 
66  template <typename Tag> struct ProvideReferenceAccessor {
67  enum { value = false };
68  };
69 
72  template <typename Tag> struct ComputeBaseClass {
74  TypeSafeIdRefAccessorBase<Tag>,
75  TypeSafeIdBase<Tag> >::type type;
76  };
77 
81  template <typename Tag> struct SentinelValue {
82  typedef typename WrappedType<Tag>::type wrapped_type;
83  static wrapped_type get() {
84  return std::numeric_limits<wrapped_type>::max();
85  }
86  };
87 
88  } // namespace typesafeid_traits
89 
104  template <class Tag> class TypeSafeIdBase {
105  public:
107  typedef TypeSafeId<Tag> type;
108 
110  typedef typename typesafeid_traits::WrappedType<Tag>::type wrapped_type;
111 
113  static type invalid() { return type(); }
114 
117  TypeSafeIdBase() : m_val(sentinel()) {}
118 
120  explicit TypeSafeIdBase(wrapped_type val) : m_val(val) {}
121 
123  TypeSafeIdBase(TypeSafeIdBase const &other) : m_val(other.m_val) {}
124 
126  bool empty() const { return m_val == sentinel(); }
127 
129  wrapped_type value() const { return m_val; }
130 
131  protected:
133  static wrapped_type sentinel() {
135  }
136  wrapped_type m_val;
137  };
138 
139  template <class Tag>
140  class TypeSafeIdRefAccessorBase : public TypeSafeIdBase<Tag> {
141  public:
142  typedef TypeSafeIdBase<Tag> Base;
143  typedef typename typesafeid_traits::WrappedType<Tag>::type wrapped_type;
144  TypeSafeIdRefAccessorBase() : Base() {}
145  explicit TypeSafeIdRefAccessorBase(wrapped_type val) : Base(val) {}
146  TypeSafeIdRefAccessorBase(TypeSafeIdRefAccessorBase const &other)
147  : Base(other) {}
148 
152  wrapped_type &value() { return Base::m_val; }
153  };
154 
155  template <class Tag>
156  class TypeSafeId : public typesafeid_traits::ComputeBaseClass<Tag>::type {
157  public:
159  typedef TypeSafeId<Tag> type;
161  typedef typename typesafeid_traits::ComputeBaseClass<Tag>::type Base;
162  typedef typename typesafeid_traits::WrappedType<Tag>::type wrapped_type;
163  TypeSafeId() : Base() {}
164  explicit TypeSafeId(wrapped_type val) : Base(val) {}
165  TypeSafeId(TypeSafeId const &other) : Base(other) {}
166  };
167 
170  template <typename Tag>
171  inline bool operator==(TypeSafeId<Tag> const a, TypeSafeId<Tag> const b) {
172  return a.value() == b.value();
173  }
174 
177  template <typename Tag>
178  inline bool operator!=(TypeSafeId<Tag> const a, TypeSafeId<Tag> const b) {
179  return a.value() != b.value();
180  }
181 
182 } // namespace util
183 } // namespace osvr
184 
185 #endif // INCLUDED_TypeSafeId_h_GUID_137CA336_382A_4796_7735_4521F02D5AC2
TypeSafeIdBase(wrapped_type val)
Explicit constructor from the wrapped type.
Definition: TypeSafeId.h:120
static wrapped_type sentinel()
Utility function to access the SentinelValue trait.
Definition: TypeSafeId.h:133
bool empty() const
Check whether the ID is empty/invalid.
Definition: TypeSafeId.h:126
wrapped_type value() const
Read-only accessor to the (non-type-safe!) wrapped value.
Definition: TypeSafeId.h:129
Header wrapping the C99 standard stdint header.
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
Quick C++11-aligned conditional (if/then/elese) implementation.
static type invalid()
Static factory method to return an invalid/empty ID.
Definition: TypeSafeId.h:113
Header containing some basic, C++11-aligned implementations of functionality provided by
A generic typesafe (as long as you use differing tag types) wrapper for identifiers (typically intege...
Definition: TypeSafeId.h:42
Explicitly specialize for your tag type if you want a different signal value for invalid/empty: defau...
Definition: TypeSafeId.h:81
TypeSafeId< Tag > type
The "public" type of the current class.
Definition: TypeSafeId.h:107
Explicitly specialize for your tag type if you want a different underlying type.
Definition: TypeSafeId.h:49
bool operator==(const OSVR_TimeValue &tvA, const OSVR_TimeValue &tvB)
Operator == overload for time values.
Definition: TimeValueC.h:251
typesafeid_traits::WrappedType< Tag >::type wrapped_type
The contained/wrapped type.
Definition: TypeSafeId.h:110
bool operator!=(const OSVR_TimeValue &tvA, const OSVR_TimeValue &tvB)
Operator == overload for time values.
Definition: TimeValueC.h:262
TypeSafeIdBase(TypeSafeIdBase const &other)
Copy constructor.
Definition: TypeSafeId.h:123