vrpn  07.33
Virtual Reality Peripheral Network
vrpn_FixedPoint.h
Go to the documentation of this file.
1 
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 
23 namespace vrpn {
24 
25  namespace detail {
31  struct IntegerOverflow;
32 
44  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 
70 
72  } // namespace detail
73 
80  template <int INTEGER_BITS, int FRACTIONAL_BITS> class FixedPoint {
81  public:
86 
87  typedef typename detail::IntegerOfSize<INTEGER_BITS +
88  FRACTIONAL_BITS>::type RawType;
89 
100  : value_(0)
101  {
102  }
103  explicit FixedPoint(vrpn_int8 x)
104  : value_(x)
105  {
106  }
107  explicit FixedPoint(vrpn_int16 x)
108  : value_(x)
109  {
110  }
111  explicit FixedPoint(vrpn_int32 x)
112  : value_(x)
113  {
114  }
115  explicit FixedPoint(vrpn_uint8 x)
116  : value_(x)
117  {
118  }
119  explicit FixedPoint(vrpn_uint16 x)
120  : value_(x)
121  {
122  }
123  explicit FixedPoint(vrpn_uint32 x)
124  : value_(x)
125  {
126  }
127  explicit FixedPoint(double x)
128  : value_(x * (1 << FRACTIONAL_BITS))
129  {
130  }
131  explicit FixedPoint(float x)
132  : value_(x * (1 << FRACTIONAL_BITS))
133  {
134  }
136 
138 
143  template <typename T> T get() const
144  {
145  return get(reinterpret_cast<TypeWrapper<T> *>(NULL));
146  }
147 
149  RawType value() const { return value_; }
153 
154  private:
155  template <typename T> struct TypeWrapper;
156  vrpn_float32 get(TypeWrapper<vrpn_float32> *) const
157  {
158  return static_cast<vrpn_float32>(value_) / (1 << FRACTIONAL_BITS);
159  }
160 
161  vrpn_float64 get(TypeWrapper<vrpn_float64> *) const
162  {
163  return static_cast<vrpn_float64>(value_) / (1 << FRACTIONAL_BITS);
164  }
165 
166  RawType value_;
167  };
168 
169 } // namespace vrpn
170 
171 #endif // VRPN_FIXED_POINT_H_
vrpn::FixedPoint::FixedPoint
FixedPoint(vrpn_uint8 x)
Definition: vrpn_FixedPoint.h:115
vrpn::FixedPoint::IntegerType
detail::IntegerOfSize< INTEGER_BITS >::type IntegerType
Find an integer type large enough to hold INTEGER_BITS.
Definition: vrpn_FixedPoint.h:85
vrpn_Types.h
vrpn
Definition: vrpn_FixedPoint.h:23
detail
Namespace enclosing internal implementation details.
Definition: vrpn_ConnectionPtr.h:225
vrpn::FixedPoint::FixedPoint
FixedPoint()
Definition: vrpn_FixedPoint.h:99
vrpn::detail::IntegerOfSize::type
IntegerOfSize< NUM_BITS+1 >::type type
Definition: vrpn_FixedPoint.h:48
vrpn::detail::IntegerOfSize< 32 >::type
vrpn_int32 type
Definition: vrpn_FixedPoint.h:60
vrpn::FixedPoint::RawType
detail::IntegerOfSize< INTEGER_BITS+FRACTIONAL_BITS >::type RawType
Definition: vrpn_FixedPoint.h:88
vrpn::FixedPoint::FixedPoint
FixedPoint(double x)
Definition: vrpn_FixedPoint.h:127
vrpn::FixedPoint::FixedPoint
FixedPoint(vrpn_int32 x)
Definition: vrpn_FixedPoint.h:111
vrpn::detail::IntegerOfSize< 8 >::type
vrpn_int8 type
Definition: vrpn_FixedPoint.h:52
vrpn::FixedPoint
A fixed-point value class.
Definition: vrpn_FixedPoint.h:80
vrpn::detail::IntegerOfSize< 16 >::type
vrpn_int16 type
Definition: vrpn_FixedPoint.h:56
vrpn::FixedPoint::FixedPoint
FixedPoint(vrpn_uint16 x)
Definition: vrpn_FixedPoint.h:119
vrpn::detail::IntegerOfSize
Definition: vrpn_FixedPoint.h:45
vrpn::FixedPoint::FixedPoint
FixedPoint(vrpn_int8 x)
Definition: vrpn_FixedPoint.h:103
vrpn::FixedPoint::value
RawType value() const
Definition: vrpn_FixedPoint.h:151
vrpn::FixedPoint::FixedPoint
FixedPoint(vrpn_uint32 x)
Definition: vrpn_FixedPoint.h:123
vrpn::detail::IntegerOfSize< 64 >::type
IntegerOfSize type
Definition: vrpn_FixedPoint.h:64
vrpn::FixedPoint::FixedPoint
FixedPoint(vrpn_int16 x)
Definition: vrpn_FixedPoint.h:107
vrpn::FixedPoint::FixedPoint
FixedPoint(float x)
Definition: vrpn_FixedPoint.h:131
vrpn::FixedPoint::get
T get() const
Returns a floating-point representation of this fixed-point value.
Definition: vrpn_FixedPoint.h:143