OSVR-Core
NumberTypeManipulation.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_NumberTypeManipulation_h_GUID_D6AC63FF_9257_488B_E06B_0B3FF403E822
26 #define INCLUDED_NumberTypeManipulation_h_GUID_D6AC63FF_9257_488B_E06B_0B3FF403E822
27 
28 // Internal Includes
29 #include <osvr/Util/StdInt.h>
30 
31 // Library/third-party includes
32 #include <boost/mpl/identity.hpp>
33 #include <boost/noncopyable.hpp>
34 #include <boost/type_traits/is_floating_point.hpp>
35 #include <boost/type_traits/is_signed.hpp>
36 
37 // Standard includes
38 #include <stdexcept>
39 
40 namespace osvr {
41 namespace util {
42  namespace detail {
43  class NumberTypeDecompositionFunctor;
44  } // namespace detail
45 
48  public:
51  template <typename T> static NumberTypeData get();
52 
54  NumberTypeData() : m_signed(false), m_sizeof(1), m_float(false) {}
55 
63  NumberTypeData(size_t size, bool isTypeSigned, bool isTypeFloating)
64  : m_signed(isTypeSigned), m_sizeof(size), m_float(isTypeFloating) {
65  if (m_float) {
66  m_signed = false;
67  }
68  switch (m_sizeof) {
69  case 1:
70  case 2:
71  case 4:
72  case 8:
73  break;
74  default:
75  throw std::logic_error(
76  "Can't describe a numeric type of that size!");
77  }
78  }
79 
81  bool isSigned() const { return m_signed; }
82 
84  bool isFloatingPoint() const { return m_float; }
85 
87  size_t getSize() const { return m_sizeof; }
88 
91  template <typename Functor> void callFunctorWithType(Functor &f) {
92  using boost::mpl::identity;
93  switch (getSize()) {
94  case 1:
95  if (isFloatingPoint()) {
96  throw std::logic_error(
97  "Can't construct a 1-byte floating point type!");
98  }
99  if (isSigned()) {
100  f(identity<int8_t>());
101  } else {
102  f(identity<uint8_t>());
103  }
104  return;
105  case 2:
106  if (isFloatingPoint()) {
107  throw std::logic_error(
108  "Can't construct a 2-byte floating point type!");
109  }
110  if (isSigned()) {
111  f(identity<int16_t>());
112  } else {
113  f(identity<uint16_t>());
114  }
115  return;
116  case 4:
117  if (isFloatingPoint()) {
118  f(identity<float>());
119  } else {
120  if (isSigned()) {
121  f(identity<int32_t>());
122  } else {
123  f(identity<uint32_t>());
124  }
125  }
126  return;
127  case 8:
128  if (isFloatingPoint()) {
129  f(identity<double>());
130  } else {
131  if (isSigned()) {
132  f(identity<int64_t>());
133  } else {
134  f(identity<uint64_t>());
135  }
136  }
137  return;
138  default:
139  throw std::logic_error(
140  "Can't construct a numeric type of that size!");
141  }
142  }
143 
144  private:
145  bool m_signed;
146  size_t m_sizeof;
147  bool m_float;
149  };
150 
151  namespace detail {
152  class NumberTypeDecompositionFunctor : boost::noncopyable {
153  public:
155  : m_data(data) {}
156  template <typename WrappedT> void operator()(WrappedT const &) {
157  typedef typename WrappedT::type T;
158  m_data.m_sizeof = sizeof(T);
159  m_data.m_float = boost::is_floating_point<T>::value;
160  m_data.m_signed = boost::is_signed<T>::value;
161  }
162 
163  private:
164  NumberTypeData &m_data;
165  };
166  } // namespace detail
167 
168  template <typename T> inline NumberTypeData NumberTypeData::get() {
169  NumberTypeData d;
171  f(boost::mpl::identity<T>());
172  return d;
173  }
174 } // namespace util
175 } // namespace osvr
176 
177 #endif // INCLUDED_NumberTypeManipulation_h_GUID_D6AC63FF_9257_488B_E06B_0B3FF403E822
void callFunctorWithType(Functor &f)
Calls the user-provided functor with the type described by this runtime data, wrapped in a boost::mpl...
Definition: NumberTypeManipulation.h:91
bool isFloatingPoint() const
is the type a floating-point type?
Definition: NumberTypeManipulation.h:84
Definition: NonLinearOptimization.cpp:107
Definition: RunLoopManager.h:42
Runtime data on numeric types.
Definition: NumberTypeManipulation.h:47
bool isSigned() const
is the type a signed integer?
Definition: NumberTypeManipulation.h:81
static NumberTypeData get()
Get runtime data on a given numeric type, passed as the template argument.
Definition: NumberTypeManipulation.h:168
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
detail::size< coerce_list< Ts... >> size
Get the size of a list (number of elements.)
Definition: Size.h:56
Definition: newuoa.h:1888
NumberTypeData(size_t size, bool isTypeSigned, bool isTypeFloating)
Construct a runtime number type descriptor.
Definition: NumberTypeManipulation.h:63
NumberTypeData()
Default constructor, for an unsigned 1-byte (8 bit) type.
Definition: NumberTypeManipulation.h:54
Definition: NumberTypeManipulation.h:152
size_t getSize() const
Get the size in bytes of the type.
Definition: NumberTypeManipulation.h:87