fix: cmake install and possible conflicting options
[KDIS.git] / src / DataTypes / KFIXED.cpp
blob8e2f12f2d75b8a85a186d41f8c6ef70672bb887e
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
30 #include "KDIS/DataTypes/KFIXED.hpp"
32 using namespace KDIS;
33 using namespace DATA_TYPE;
35 /**
36 * [n].[m] digit fixed point binary number:
38 * records fractions of integer values to precision [m] binary places
40 * stored value (using integer container) = (int or float rhs) * 2^m
42 * "2^m" is equivalent to "1 << m" which is can be literal constant.
44 * Such code is used at many points in this file.
47 //////////////////////////////////////////////////////////////////////////
48 // protected:
49 //////////////////////////////////////////////////////////////////////////
51 template <class Type, KUINT8 BinaryPoint>
52 Type KFIXED<Type, BinaryPoint>::convert(KFLOAT32 V) const {
53 return V * (1 << BinaryPoint);
56 //////////////////////////////////////////////////////////////////////////
58 template <class Type, KUINT8 BinaryPoint>
59 Type KFIXED<Type, BinaryPoint>::convert(KFLOAT64 V) const {
60 return V * (1 << BinaryPoint);
63 //////////////////////////////////////////////////////////////////////////
64 // public:
65 //////////////////////////////////////////////////////////////////////////
67 template <class Type, KUINT8 BinaryPoint>
68 KFIXED<Type, BinaryPoint>::KFIXED() : m_Val(0) {}
70 //////////////////////////////////////////////////////////////////////////
72 template <class Type, KUINT8 BinaryPoint>
73 KFIXED<Type, BinaryPoint>::KFIXED(KFLOAT32 V) : m_Val(convert(V)) {}
75 //////////////////////////////////////////////////////////////////////////
77 template <class Type, KUINT8 BinaryPoint>
78 KFIXED<Type, BinaryPoint>::KFIXED(KFLOAT64 V) : m_Val(convert(V)) {}
80 //////////////////////////////////////////////////////////////////////////
82 template <class Type, KUINT8 BinaryPoint>
83 KFIXED<Type, BinaryPoint>::KFIXED(Type V) : m_Val(V) {}
85 //////////////////////////////////////////////////////////////////////////
87 template <class Type, KUINT8 BinaryPoint>
88 KFIXED<Type, BinaryPoint>::KFIXED(KDataStream& stream) {
89 Decode(stream);
92 //////////////////////////////////////////////////////////////////////////
94 template <class Type, KUINT8 BinaryPoint>
95 KFIXED<Type, BinaryPoint>::~KFIXED() {}
97 //////////////////////////////////////////////////////////////////////////
99 template <class Type, KUINT8 BinaryPoint>
100 void KFIXED<Type, BinaryPoint>::Set(KFLOAT32 V) {
101 m_Val = convert(V);
104 //////////////////////////////////////////////////////////////////////////
106 template <class Type, KUINT8 BinaryPoint>
107 void KFIXED<Type, BinaryPoint>::Set(KFLOAT64 V) {
108 m_Val = convert(V);
111 //////////////////////////////////////////////////////////////////////////
113 template <class Type, KUINT8 BinaryPoint>
114 void KFIXED<Type, BinaryPoint>::Set(Type V) {
115 m_Val = V;
118 //////////////////////////////////////////////////////////////////////////
120 template <class Type, KUINT8 BinaryPoint>
121 Type KFIXED<Type, BinaryPoint>::Get() const {
122 return m_Val;
125 //////////////////////////////////////////////////////////////////////////
127 template <class Type, KUINT8 BinaryPoint>
128 KFLOAT32 KFIXED<Type, BinaryPoint>::GetAsFloat32() const {
129 return m_Val * (1.0 / (1 << BinaryPoint));
132 //////////////////////////////////////////////////////////////////////////
134 template <class Type, KUINT8 BinaryPoint>
135 KString KFIXED<Type, BinaryPoint>::GetAsString() const {
136 KStringStream ss;
138 ss << GetAsFloat32() << "\n";
140 return ss.str();
143 //////////////////////////////////////////////////////////////////////////
145 template <class Type, KUINT8 BinaryPoint>
146 void KFIXED<Type, BinaryPoint>::Decode(KDataStream& stream) {
147 if (stream.GetBufferSize() < sizeof(Type))
148 throw KException(__FUNCTION__, NOT_ENOUGH_DATA_IN_BUFFER);
150 stream >> m_Val;
153 //////////////////////////////////////////////////////////////////////////
155 template <class Type, KUINT8 BinaryPoint>
156 KDataStream KFIXED<Type, BinaryPoint>::Encode() const {
157 KDataStream stream;
159 KFIXED<Type, BinaryPoint>::Encode(stream);
161 return stream;
164 //////////////////////////////////////////////////////////////////////////
166 template <class Type, KUINT8 BinaryPoint>
167 void KFIXED<Type, BinaryPoint>::Encode(KDataStream& stream) const {
168 stream << m_Val;
171 //////////////////////////////////////////////////////////////////////////
172 // operators:
173 //////////////////////////////////////////////////////////////////////////
175 // Some macros to reduce the code a little
177 // DEFINE_OPERATOR
178 // ******************************************************************************************
179 #define DEFINE_OPERATOR(OPERATOR) \
181 template <class Type, KUINT8 BinaryPoint> \
182 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR( \
183 KFLOAT32 Value) { \
184 return GetAsFloat32() OPERATOR Value; \
187 template <class Type, KUINT8 BinaryPoint> \
188 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR( \
189 KFLOAT64 Value) { \
190 return GetAsFloat32() OPERATOR Value; \
193 template <class Type, KUINT8 BinaryPoint> \
194 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR( \
195 Type Value) { \
196 return *this OPERATOR KFIXED(Value).GetAsFloat32(); \
199 template <class Type, KUINT8 BinaryPoint> \
200 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR( \
201 KFIXED<Type, BinaryPoint> Value) { \
202 return GetAsFloat32() OPERATOR Value.GetAsFloat32(); \
205 // DEFINE_COMPARISION_OPERATOR
206 // ******************************************************************************
207 #define DEFINE_COMPARISION_OPERATOR(OPERATOR) \
209 template <class Type, KUINT8 BinaryPoint> \
210 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR(KFLOAT32 Value) const { \
211 return m_Val OPERATOR convert(Value); \
214 template <class Type, KUINT8 BinaryPoint> \
215 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR(KFLOAT64 Value) const { \
216 return m_Val OPERATOR convert(Value); \
219 template <class Type, KUINT8 BinaryPoint> \
220 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR(Type Value) const { \
221 return m_Val OPERATOR m_Val; \
224 template <class Type, KUINT8 BinaryPoint> \
225 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR( \
226 KFIXED<Type, BinaryPoint> Value) const { \
227 return m_Val OPERATOR Value.m_Val; \
230 //***************************** End Macros
231 //******************************************************************
233 template <class Type, KUINT8 BinaryPoint>
234 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator=(KFLOAT32 Value) {
235 m_Val = convert(Value);
236 return *this;
239 //////////////////////////////////////////////////////////////////////////
241 template <class Type, KUINT8 BinaryPoint>
242 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator=(KFLOAT64 Value) {
243 m_Val = convert(Value);
244 return *this;
247 //////////////////////////////////////////////////////////////////////////
249 template <class Type, KUINT8 BinaryPoint>
250 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator=(Type Value) {
251 m_Val = Value;
252 return *this;
255 //////////////////////////////////////////////////////////////////////////
257 template <class Type, KUINT8 BinaryPoint>
258 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator=(
259 KFIXED<Type, BinaryPoint> Value) {
260 m_Val = Value.m_Val;
261 return *this;
264 //////////////////////////////////////////////////////////////////////////
266 DEFINE_OPERATOR(+)
267 DEFINE_OPERATOR(-)
268 DEFINE_OPERATOR(*)
269 DEFINE_OPERATOR(/)
271 //////////////////////////////////////////////////////////////////////////
273 DEFINE_COMPARISION_OPERATOR(==)
274 DEFINE_COMPARISION_OPERATOR(!=)
275 DEFINE_COMPARISION_OPERATOR(<)
276 DEFINE_COMPARISION_OPERATOR(<=)
277 DEFINE_COMPARISION_OPERATOR(>)
278 DEFINE_COMPARISION_OPERATOR(>=)
280 //////////////////////////////////////////////////////////////////////////
282 template <class Type, KUINT8 BinaryPoint>
283 KFIXED<Type, BinaryPoint>::operator KFLOAT32() const {
284 return GetAsFloat32();
287 //////////////////////////////////////////////////////////////////////////
289 template <class Type, KUINT8 BinaryPoint>
290 KFIXED<Type, BinaryPoint>::operator Type() const {
291 return m_Val;
294 //////////////////////////////////////////////////////////////////////////
296 // When using a template class with implementation in a cpp file, linker
297 // errors will occur if you do not pre-declare the template types in that same
298 // file. This is good for us in this case as we only need to support a binary
299 // point of 3 and 8. If you do wish to add support for other binary point
300 // positions then simply add them below and re-compile.
302 template class KDIS::DATA_TYPE::KFIXED<KINT16, 3>;
303 template class KDIS::DATA_TYPE::KFIXED<KINT16, 8>;
304 template class KDIS::DATA_TYPE::KFIXED<KINT8, 3>;