vrpn 07.35
Virtual Reality Peripheral Network
 
Loading...
Searching...
No Matches
vrpn_FixedPoint.h
Go to the documentation of this file.
1
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 {
31 struct IntegerOverflow;
32
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.
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> {
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.
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
86
88 template <int BITS, bool SIGNED = true>
91 template <int BITS>
92 struct IntegerOfSizeAndSignedness<BITS, false>
93 : UnsignedIntegerOfSize<BITS> {
94 };
95 } // namespace detail
96
105 template <int INTEGER_BITS, int FRACTIONAL_BITS, bool SIGNED = true>
107 public:
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
126 : value_(0)
127 {
128 }
129 explicit FixedPoint(
131 : value_(x)
132 {
133 }
134 explicit FixedPoint(
136 : value_(x)
137 {
138 }
139 explicit FixedPoint(
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
155
160 template <typename T> T get() const
161 {
162 return get(reinterpret_cast<TypeWrapper<T> *>(NULL));
163 }
164
168 RawType value() const { return value_; }
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_
detail::IntegerOfSizeAndSignedness< INTEGER_BITS+FRACTIONAL_BITS, SIGNED >::type RawType
RawType value() const
FixedPoint(typename detail::IntegerOfSizeAndSignedness< 8, SIGNED >::type x)
FixedPoint(typename detail::IntegerOfSizeAndSignedness< 16, SIGNED >::type x)
T get() const
Returns a floating-point representation of this fixed-point value.
FixedPoint(typename detail::IntegerOfSizeAndSignedness< 32, SIGNED >::type x)
detail::IntegerOfSizeAndSignedness< INTEGER_BITS, SIGNED >::type IntegerType
Find an integer type large enough to hold INTEGER_BITS.
Namespace enclosing internal implementation details.
IntegerOfSize< NUM_BITS+1 >::type type
UnsignedIntegerOfSize< NUM_BITS+1 >::type type