KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KFIXED.h
Go to the documentation of this file.
1 /*********************************************************************
2 
3 Copyright 2013 Karl Jones
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 For Further Information Please Contact me at
27 Karljj1@yahoo.com
28 http://p.sf.net/kdis/UserGuide
29 *********************************************************************/
30 
31 /********************************************************************
32  class: KFIXED
33  created: 01/07/2010
34  author: Karl Jones
35 
36  purpose: A fixed point number used in Live Entity family.
37  The binary point is defined by the sub protocol, but for the purposes
38  of this standard the binary point has also been defined when a fixed point number is used,
39  the point varies depending on the data type.
40 
41  This template class only supports certain fixed point numbers by default.
42  They are:
43  16 bit fixed with point at bit 3
44  16 bit fixed with point at bit 8
45  8 bit fixed with point at bit 3 - The DIS standard does not state where the bit goes so 3 is an educated guess..
46 
47  If you wish to add support for other binary point value then edit the end of the KFIXED.cpp file.
48  See http://en.wikipedia.org/wiki/Fixed-point_arithmetic for more infomation on fixed-point numbers.
49 
50 *********************************************************************/
51 
52 #pragma once
53 
54 #include "./DataTypeBase.h"
55 
56 namespace KDIS {
57 namespace DATA_TYPE {
58 
59 template<class Type, KUINT8 BinaryPoint>
61 {
62 protected:
63 
64  Type m_Val;
65 
66  //************************************
67  // FullName: KDIS::DATA_TYPE::KFIXED<Type, BinaryPoint>::convert
68  //!Description: convert from a float to a fixed value.
69  // Parameter: KFLOAT32 V
70  //************************************
71  Type convert( KFLOAT32 V ) const;
72  Type convert( KFLOAT64 V ) const;
73 
74 public:
75 
76  KFIXED();
77 
78  KFIXED( KFLOAT32 V );
79 
80  KFIXED( KFLOAT64 V );
81 
82  KFIXED( Type V );
83 
84  KFIXED( KDataStream & stream )throw( KException );
85 
86  virtual ~KFIXED();
87 
88  //************************************
89  // FullName: KDIS::DATA_TYPE::KFIXED::Set
90  //!Description: Sets the internal value.
91  // Parameter: KFLOAT32 V
92  //************************************
93  void Set( KFLOAT32 V );
94 
95  //************************************
96  // FullName: KDIS::DATA_TYPE::KFIXED::Set
97  //!Description: Sets the internal value.
98  // Parameter: KFLOAT64 V
99  //************************************
100  void Set( KFLOAT64 V );
101 
102  //************************************
103  // FullName: KDIS::DATA_TYPE::KFIXED::Set
104  //!Description: Set the internal value.
105  // Parameter: Type V
106  //************************************
107  void Set( Type V );
108 
109  //************************************
110  // FullName: KDIS::DATA_TYPE::KFIXED::Get
111  //!Description: Returns internal value.
112  //************************************
113  Type Get() const;
114 
115  //************************************
116  // FullName: KDIS::DATA_TYPE::KFIXED::GetAsFloat32
117  //!Description: Convert value to a float 32 bit.
118  //************************************
119  KFLOAT32 GetAsFloat32() const;
120 
121  //************************************
122  // FullName: KDIS::DATA_TYPE::KFIXED::GetAsString
123  //!Description: Returns a string representation.
124  //************************************
125  virtual KString GetAsString() const;
126 
127  //************************************
128  // FullName: KDIS::DATA_TYPE::KFIXED::Decode
129  //!Description: Convert From Network Data.
130  // Parameter: KDataStream & stream
131  //************************************
132  virtual void Decode( KDataStream & stream ) throw( KException );
133 
134  //************************************
135  // FullName: KDIS::DATA_TYPE::KFIXED::Encode
136  //!Description: Convert To Network Data.
137  // Parameter: KDataStream & stream
138  //************************************
139  virtual KDataStream Encode() const;
140  virtual void Encode( KDataStream & stream ) const;
141 
142  // operators
143 
144  // Helper macros:
145 #define DECLARE_OPERATOR( OPERATOR ) \
146  \
147  KFIXED operator OPERATOR ( KFLOAT32 Value ); \
148  KFIXED operator OPERATOR ( KFLOAT64 Value ); \
149  KFIXED operator OPERATOR ( Type Value ); \
150  KFIXED operator OPERATOR ( KFIXED<Type, BinaryPoint> Value ); \
151 
152 #define DECLARE_COMPARISON_OPERATOR( OPERATOR ) \
153  \
154  KBOOL operator OPERATOR ( KFLOAT32 Value ) const; \
155  KBOOL operator OPERATOR ( KFLOAT64 Value ) const; \
156  KBOOL operator OPERATOR ( Type Value ) const; \
157  KBOOL operator OPERATOR ( KFIXED<Type, BinaryPoint> Value ) const; \
158  // End of macros
159 
160  // Supported operators
161  DECLARE_OPERATOR( = )
162  DECLARE_OPERATOR( + )
163  DECLARE_OPERATOR( - )
164  DECLARE_OPERATOR( * )
165  DECLARE_OPERATOR( / )
166 
167  // Comparison operators:
174 
175  // We are finished with the macros now so lets remove them.
176 #undef DECLARE_OPERATOR
177 #undef DECLARE_COMPARISON_OPERATOR
178 
179  // Casting
180  operator KFLOAT32 () const;
181  operator Type () const;
182 };
183 
184 // Predefined KFIXED types.
185 // KFIXEDx_y
186 // x = Size of data type in bits
187 // y = The position of the binary point.
191 
192 } // END namespace DATA_TYPES
193 } // END namespace KDIS
#define DECLARE_OPERATOR(OPERATOR)
Definition: KFIXED.h:145
Definition: KFIXED.h:60
KFIXED< KINT16, 3 > KFIXED16_3
Definition: KFIXED.h:189
float KFLOAT32
Definition: KDefines.h:113
Definition: KDefines.h:182
Definition: KDataStream.h:48
Definition: DataTypeBase.h:49
#define DECLARE_COMPARISON_OPERATOR(OPERATOR)
Definition: KFIXED.h:152
KFIXED< KINT16, 8 > KFIXED16_8
Definition: KFIXED.h:190
std::string KString
Definition: KDefines.h:116
Type m_Val
Definition: KFIXED.h:64
double KFLOAT64
Definition: KDefines.h:114
KFIXED< KINT8, 3 > KFIXED8_3
Definition: KFIXED.h:188
#define KDIS_EXPORT
Definition: KDefines.h:82