RflySimSDK v3.08
RflySimSDK说明文档
载入中...
搜索中...
未找到
vrpn_FixedPoint.h
浏览该文件的文档.
1/** @file vrpn_FixedPoint.h
2 @brief Header
3
4 @date 2014
5
6 @author
7 Kevin M. Godby
8 <kevin@godby.org>
9*/
10
11#ifndef VRPN_FIXED_POINT_H_
12#define VRPN_FIXED_POINT_H_
13
14// Internal Includes
15#include "vrpn_Types.h"
16
17// Library/third-party includes
18// - none
19
20// Standard includes
21#include <cstddef> // for NULL
22
23namespace vrpn {
24
25 namespace detail {
26 /**
27 * Set as the type member of vrpn_IntegerOfSize if too many bits are
28 * required.
29 * We only support up to 32 bits currently.
30 */
31 struct IntegerOverflow;
32
33 /**
34 * \name Integer types.
35 *
36 * In the general case, we'll start adding bits to the required size
37 *until we
38 * land on one of the specialized cases below, then return that
39 *specialized
40 * case. The end result is that we return an integer type that can
41 *contain a
42 * value require NUM_BITS bits.
43 */
44 ///@{
45 template <int NUM_BITS> struct IntegerOfSize {
46 // An integer requiring n bits can be represented by an integer of
47 // size n+1 bits.
48 typedef typename IntegerOfSize<NUM_BITS + 1>::type type;
49 };
50
51 template <> struct IntegerOfSize<8> {
52 typedef vrpn_int8 type;
53 };
54
55 template <> struct IntegerOfSize<16> {
56 typedef vrpn_int16 type;
57 };
58
59 template <> struct IntegerOfSize<32> {
60 typedef vrpn_int32 type;
61 };
62
63 template <> struct IntegerOfSize<64> {
64 typedef IntegerOfSize type;
65 };
66 template <int NUM_BITS> struct UnsignedIntegerOfSize {
67 // An integer requiring n bits can be represented by an integer of
68 // size n+1 bits.
69 typedef typename UnsignedIntegerOfSize<NUM_BITS + 1>::type type;
70 };
71
72 template <> struct UnsignedIntegerOfSize<8> {
73 typedef vrpn_uint8 type;
74 };
75
76 template <> struct UnsignedIntegerOfSize<16> {
77 typedef vrpn_uint16 type;
78 };
79
80 template <> struct UnsignedIntegerOfSize<32> {
81 typedef vrpn_uint32 type;
82 };
83
84 /// @todo allow for larger types if we can establish VRPN versions of
85 /// them (e.g., equivalents to uint64_t).
86
87 ///@}
88 template <int BITS, bool SIGNED = true>
91 template <int BITS>
92 struct IntegerOfSizeAndSignedness<BITS, false>
93 : UnsignedIntegerOfSize<BITS> {
94 };
95 } // namespace detail
96
97 /**
98 * A fixed-point value class. All values are signed, two's-complement, by
99 * default.
100 *
101 * @tparam INTEGER_BITS The number of bits devoted to the integer part.
102 * @tparam FRACTIONAL_BITS The number of bits devoted to the fractional
103 * part.
104 */
105 template <int INTEGER_BITS, int FRACTIONAL_BITS, bool SIGNED = true>
107 public:
108 /**
109 * Find an integer type large enough to hold INTEGER_BITS.
110 */
111 typedef typename detail::IntegerOfSizeAndSignedness<
112 INTEGER_BITS, SIGNED>::type IntegerType;
113
114 typedef typename detail::IntegerOfSizeAndSignedness<
115 INTEGER_BITS + FRACTIONAL_BITS, SIGNED>::type RawType;
116
117 /**
118 * \name Constructors.
119 *
120 * The bits of an integral type passed to the constructor will be
121 * interpreted as as fixed-point value. A floating-point type passed to
122 * the constructor will be converted to a fixed-point value.
123 */
124 //@{
125 FixedPoint()
126 : value_(0)
127 {
128 }
129 explicit FixedPoint(
130 typename detail::IntegerOfSizeAndSignedness<8, SIGNED>::type x)
131 : value_(x)
132 {
133 }
134 explicit FixedPoint(
135 typename detail::IntegerOfSizeAndSignedness<16, SIGNED>::type x)
136 : value_(x)
137 {
138 }
139 explicit FixedPoint(
140 typename detail::IntegerOfSizeAndSignedness<32, SIGNED>::type x)
141 : value_(x)
142 {
143 }
144 explicit FixedPoint(double x)
145 : value_(x * (1 << FRACTIONAL_BITS))
146 {
147 }
148 explicit FixedPoint(float x)
149 : value_(x * (1 << FRACTIONAL_BITS))
150 {
151 }
152 //@}
153
154 /// @todo add operators, lots and lots of operators?
155
156 /**
157 * Returns a floating-point representation of this
158 * fixed-point value.
159 */
160 template <typename T> T get() const
161 {
162 return get(reinterpret_cast<TypeWrapper<T> *>(NULL));
163 }
164
165 /** \name Debugging functions. */
166 ///@{
167 /// @todo remove these functions after debugging
168 RawType value() const { return value_; }
169 ///@}
170
171 private:
172 template <typename T> struct TypeWrapper;
173 vrpn_float32 get(TypeWrapper<vrpn_float32> *) const
174 {
175 return static_cast<vrpn_float32>(value_) / (1 << FRACTIONAL_BITS);
176 }
177
178 vrpn_float64 get(TypeWrapper<vrpn_float64> *) const
179 {
180 return static_cast<vrpn_float64>(value_) / (1 << FRACTIONAL_BITS);
181 }
182
183 RawType value_;
184 };
185
186} // namespace vrpn
187
188#endif // VRPN_FIXED_POINT_H_
定义 vrpn_FixedPoint.h:106
detail::IntegerOfSizeAndSignedness< INTEGER_BITS, SIGNED >::type IntegerType
定义 vrpn_FixedPoint.h:112
RawType value() const
定义 vrpn_FixedPoint.h:168
T get() const
定义 vrpn_FixedPoint.h:160
Namespace enclosing internal implementation details
定义 vrpn_ConnectionPtr.h:225
定义 vrpn_FixedPoint.h:89
定义 vrpn_FixedPoint.h:45
定义 vrpn_FixedPoint.h:66