OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
BeaconIdTypes.h
Go to the documentation of this file.
1 
11 // Copyright 2016 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_BeaconIdTypes_h_GUID_8C85DE41_5CAC_4DA2_6C2A_962680202E1B
26 #define INCLUDED_BeaconIdTypes_h_GUID_8C85DE41_5CAC_4DA2_6C2A_962680202E1B
27 
28 // Internal Includes
29 #include "BodyIdTypes.h"
30 
31 // Library/third-party includes
32 #include <osvr/Util/TypeSafeId.h>
33 #include <boost/assert.hpp>
34 
35 // Standard includes
36 #include <stdexcept>
37 
38 namespace osvr {
39 namespace vbtracker {
40  namespace detail {
42  struct ZeroBasedBeaconIdTag;
44  struct OneBasedBeaconIdTag;
45 
46  } // namespace detail
47 
49  using UnderlyingBeaconIdType = int;
50 } // namespace vbtracker
51 } // namespace osvr
52 
53 namespace osvr {
54 namespace util {
55  namespace typesafeid_traits {
57  template <>
58  struct WrappedType<vbtracker::detail::ZeroBasedBeaconIdTag> {
59  using type = vbtracker::UnderlyingBeaconIdType;
60  };
62  template <> struct WrappedType<vbtracker::detail::OneBasedBeaconIdTag> {
63  using type = vbtracker::UnderlyingBeaconIdType;
64  };
65  } // namespace typesafeid_traits
66 
67 } // namespace util
68 } // namespace osvr
69 
70 namespace osvr {
71 namespace vbtracker {
73  using ZeroBasedBeaconId = util::TypeSafeId<detail::ZeroBasedBeaconIdTag>;
75  using OneBasedBeaconId = util::TypeSafeId<detail::OneBasedBeaconIdTag>;
76 
79  inline OneBasedBeaconId makeOneBased(ZeroBasedBeaconId id) {
80  OneBasedBeaconId ret;
81  if (id.empty()) {
82  return ret;
83  } else if (id.value() < 0) {
84  ret = OneBasedBeaconId(id.value());
85  } else {
86  ret = OneBasedBeaconId(id.value() + 1);
87  }
88  return ret;
89  }
90 
93  inline OneBasedBeaconId const &makeOneBased(OneBasedBeaconId const &id) {
94  return id;
95  }
96 
99  inline ZeroBasedBeaconId makeZeroBased(OneBasedBeaconId id) {
100  ZeroBasedBeaconId ret;
101  if (id.empty()) {
102  return ret;
103  } else if (id.value() < 0) {
104  ret = ZeroBasedBeaconId(id.value());
105  } else {
106  ret = ZeroBasedBeaconId(id.value() - 1);
107  }
108  return ret;
109  }
110 
113  inline ZeroBasedBeaconId const &makeZeroBased(ZeroBasedBeaconId const &id) {
114  return id;
115  }
116 
118  inline bool beaconIdentified(ZeroBasedBeaconId id) {
119  return (!id.empty() && id.value() >= 0);
120  }
122  inline bool beaconIdentified(OneBasedBeaconId id) {
123  return (!id.empty() && id.value() > 0);
124  }
125 
128  inline std::size_t asIndex(ZeroBasedBeaconId id) {
129  BOOST_ASSERT_MSG(beaconIdentified(id), "A beacon id must correspond to "
130  "an identified beacon to be "
131  "used as an index!");
132  if (!beaconIdentified(id)) {
133  throw std::logic_error("A beacon id must correspond to an "
134  "identified beacon to be used as an index!");
135  }
136  return id.value();
137  }
138 
140  inline std::size_t asIndex(OneBasedBeaconId id) {
141  return asIndex(makeZeroBased(id));
142  }
143 
144 } // namespace vbtracker
145 } // namespace osvr
146 #endif // INCLUDED_BeaconIdTypes_h_GUID_8C85DE41_5CAC_4DA2_6C2A_962680202E1B
Explicitly specialize for your tag type if you want a different underlying type.
Definition: TypeSafeId.h:49
Header.