OSVR-Core
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 {
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:
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) {}
147  : Base(other) {}
148 
152  wrapped_type &value() { return Base::m_val; }
153  };
154 
155  template <class Tag>
157  public:
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
Definition: gtest_unittest.cc:5031
Class for your specialization of ProvideReferenceAccessor to inherit from if you want to provide a no...
Definition: TypeSafeId.h:54
Definition: RunLoopManager.h:42
Header wrapping the C99 standard stdint header.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
wrapped_type & value()
Non-const reference accessor to the (non-type-safe!) wrapped value - only available if specifically p...
Definition: TypeSafeId.h:152
Quick C++11-aligned conditional (if/then/elese) implementation.
Definition: BasicTypeTraits.h:48
bool operator!=(TypeSafeId< Tag > const a, TypeSafeId< Tag > const b)
Inequality comparison operator for type-safe IDs.
Definition: TypeSafeId.h:178
Header containing some basic, C++11-aligned implementations of functionality provided by <type_traits...
static type invalid()
Static factory method to return an invalid/empty ID.
Definition: TypeSafeId.h:113
Definition: TypeSafeId.h:41
typesafeid_traits::WrappedType< Tag >::type wrapped_type
The contained/wrapped type.
Definition: TypeSafeId.h:110
static wrapped_type sentinel()
Utility function to access the SentinelValue trait.
Definition: TypeSafeId.h:133
TypeSafeIdBase(wrapped_type val)
Explicit constructor from the wrapped type.
Definition: TypeSafeId.h:120
typesafeid_traits::ComputeBaseClass< Tag >::type Base
The implementation base.
Definition: TypeSafeId.h:161
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
TypeSafeId< Tag > type
The type of the current class.
Definition: TypeSafeId.h:159
Selects one of the base classes based on whether we need that reference accessor. ...
Definition: TypeSafeId.h:72
wrapped_type value() const
Read-only accessor to the (non-type-safe!) wrapped value.
Definition: TypeSafeId.h:129
bool empty() const
Check whether the ID is empty/invalid.
Definition: TypeSafeId.h:126
Explicitly specialize this for your tag type to derive from ProvideLValueReferenceAccessor if you wan...
Definition: TypeSafeId.h:66
Definition: TypeSafeId.h:43
bool operator==(TypeSafeId< Tag > const a, TypeSafeId< Tag > const b)
Equality comparison operator for type-safe IDs.
Definition: TypeSafeId.h:171
Explicitly specialize for your tag type if you want a different underlying type.
Definition: TypeSafeId.h:49
TypeSafeIdBase()
Default constructor which will set m_val to the empty/invalid value.
Definition: TypeSafeId.h:117
TypeSafeIdBase(TypeSafeIdBase const &other)
Copy constructor.
Definition: TypeSafeId.h:123
A generic typesafe (as long as you use differing tag types) wrapper for identifiers (typically intege...
Definition: TypeSafeId.h:42