OSVR-Core
SizedInt.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_SizedInt_h_GUID_E228EDD2_8659_488A_8D2D_8E9BF81E7A36
26 #define INCLUDED_SizedInt_h_GUID_E228EDD2_8659_488A_8D2D_8E9BF81E7A36
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <cstdint>
36 #include <cstddef>
37 
38 namespace osvr {
39 namespace util {
40  namespace detail {
41  template <std::size_t bytes, bool isSigned> struct sized_int;
43 #define OSVR_INT_TRAITS(BITS) \
44  template <> struct sized_int<sizeof(std::int##BITS##_t), true> { \
45  using type = std::int##BITS##_t; \
46  }; \
47  template <> struct sized_int<sizeof(std::uint##BITS##_t), false> { \
48  using type = std::uint##BITS##_t; \
49  }
50  OSVR_INT_TRAITS(8);
51  OSVR_INT_TRAITS(16);
52  OSVR_INT_TRAITS(32);
53  OSVR_INT_TRAITS(64);
54 #undef OSVR_INT_TRAITS
55  static const auto MAX_SIZE = sizeof(std::uint64_t);
57 
59  static const auto ERROR_SPECIALIZATION_SIZE = MAX_SIZE + 1;
60 
62  template <bool isSigned>
63  struct sized_int<ERROR_SPECIALIZATION_SIZE, isSigned> {};
66  template <std::size_t bytes, bool isSigned>
67  struct sized_int
68  : sized_int<(bytes > MAX_SIZE) ? ERROR_SPECIALIZATION_SIZE
69  : bytes + 1,
70  isSigned> {
71 
72  static_assert(bytes <= MAX_SIZE,
73  "No standard integral types larger than [u]int64_t.");
74  };
75 
76  } // namespace detail
79  template <std::size_t minBytes, bool isSigned = false>
81 
82 } // namespace util
83 } // namespace osvr
84 
85 #endif // INCLUDED_SizedInt_h_GUID_E228EDD2_8659_488A_8D2D_8E9BF81E7A36
Definition: RunLoopManager.h:42
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Case for non-exact sizes, incrementing by one byte if we haven&#39;t skipped right over the error case al...
Definition: SizedInt.h:41
typename detail::sized_int< minBytes, isSigned >::type sized_int_t
Alias providing an integer type (signed or unsigned, your choice) containing at least as many bytes a...
Definition: SizedInt.h:80
Definition: newuoa.h:1888
#define OSVR_INT_TRAITS(BITS)
Set up specializations for the standard int types.
Definition: SizedInt.h:43