KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FixedDatum.h
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
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.
13 
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.
24 
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
29 
30 /********************************************************************
31  class: FixedDatum
32  created: 18/09/2008
33  author: Karl Jones
34 
35  purpose: Stores fixed datum values.
36 
37  Note: See FactoryDecoder for a guide to adding support
38  for using your own FixedDatum.
39 
40  size: 64 bits / 8 octets
41 *********************************************************************/
42 
43 #pragma once
44 
45 #include "./DataTypeBase.h"
46 #include "./FactoryDecoder.h"
47 #include "./../Extras/KRef_Ptr.h"
48 
49 namespace KDIS {
50 namespace DATA_TYPE {
51 
53 using KDIS::UTILS::IsMachineBigEndian;
54 
55 /************************************************************************/
56 // Define the type of pointer we are using for FixedDatum Records,
57 // do we want a weak reference or a ref counter?
58 // By default we use a ref pointer, however if you want to use a standard
59 // pointer or one of your own then simply change it below.
60 /************************************************************************/
61 class FixedDatum;
62 typedef KDIS::UTILS::KRef_Ptr<FixedDatum> FixDtmPtr; // Ref counter
63 //typedef FixedDatum* FixDtmPtr; // Weak ref
64 
65 class KDIS_EXPORT FixedDatum : public DataTypeBase, public FactoryDecoderUser<FixedDatum>
66 {
67 protected:
68 
70 
71  KOCTET m_cDatumValue[4];
72 
73 public:
74 
75  static const KUINT16 FIXED_DATUM_SIZE = 8;
76 
77  FixedDatum();
78 
79  template<class Type>
80  FixedDatum( DatumID ID, Type Value ) throw( KException );
81 
82  FixedDatum( KDataStream & stream ) throw( KException );
83 
84  virtual ~FixedDatum();
85 
86  //************************************
87  // FullName: KDIS::DATA_TYPE::FixedDatum::SetDatumID
88  // KDIS::DATA_TYPE::FixedDatum::GetDatumID
89  //!Description: Set the datum id, indicates what the datum value
90  //! is for and what format it should be in.
91  // Parameter: DatumID ID
92  //************************************
93  virtual void SetDatumID( DatumID ID );
94  virtual DatumID GetDatumID() const;
95 
96  //************************************
97  // FullName: KDIS::DATA_TYPE::FixedDatum<Type>::SetDatumValue
98  // KDIS::DATA_TYPE::FixedDatum<Type>::GetDatumValue
99  //!Description: Returns datum value in required format, format
100  //! must be 32 bits or less.
101  // Parameter: Type val
102  //************************************
103  template<class Type>
104  void SetDatumValue( Type val ) throw( KException );
105  template<class Type>
106  Type GetDatumValue() const throw( KException );
107 
108  //************************************
109  // FullName: KDIS::DATA_TYPE::FixedDatum::GetDatumValue
110  //!Description: Copies octets into a buffer.
111  //! Buffer must be 4 octets in size.
112  //! All data types are stored in Big Endian
113  //! in the buffer.
114  // Parameter: KOCTET * Buffer
115  //************************************
116  virtual void GetDatumValue( KOCTET * Buffer, KUINT16 BufferSize ) const throw( KException );
117 
118  //************************************
119  // FullName: KDIS::DATA_TYPE::FixedDatum::GetAsString
120  //!Description: Returns a string representation
121  //************************************
122  virtual KString GetAsString() const;
123 
124  //************************************
125  // FullName: KDIS::DATA_TYPE::FixedDatum::Decode
126  //!Description: Convert From Network Data.
127  // Parameter: KDataStream & stream
128  //************************************
129  virtual void Decode( KDataStream & stream ) throw( KException );
130 
131  //************************************
132  // FullName: KDIS::DATA_TYPE::FixedDatum::Encode
133  //!Description: Convert To Network Data.
134  // Parameter: KDataStream & stream
135  //************************************
136  virtual KDataStream Encode() const;
137  virtual void Encode( KDataStream & stream ) const;
138 
139  KBOOL operator == ( const FixedDatum & Value ) const;
140  KBOOL operator != ( const FixedDatum & Value ) const;
141 };
142 
143 /////////////////////////////////////////////////////////////////////////
144 // templates
145 //////////////////////////////////////////////////////////////////////////
146 
147 template<class Type>
148 FixedDatum::FixedDatum( DatumID ID, Type Value ) throw( KException )
149 {
150  m_ui32DatumID = ID;
151  SetDatumValue( Value );
152 }
153 
154 //////////////////////////////////////////////////////////////////////////
155 
156 template<class Type>
158 {
159  if( sizeof( Type ) > 4 )throw KException( __FUNCTION__, DATA_TYPE_TOO_LARGE );
160 
161  NetToDataType<Type> NetValue( m_cDatumValue, false );
162 
163  // Do we need to convert the data back to machine endian?
164  if( IsMachineBigEndian() == false )
165  {
166  // Need to convert
167  NetValue.SwapBytes();
168  }
169 
170  return NetValue.m_Value;
171 }
172 
173 //////////////////////////////////////////////////////////////////////////
174 
175 template<class Type>
176 void FixedDatum::SetDatumValue( Type val ) throw( KException )
177 {
178  if( sizeof( Type ) > 4 )throw KException( __FUNCTION__, DATA_TYPE_TOO_LARGE );
179 
180  // Reset datum value.
181  memset( m_cDatumValue, 0x00, 4 );
182 
183  NetToDataType<Type> NetValue( val, false );
184 
185  // Now convert the data into big endian, we want to store the value like this
186  // as when we encode/decode we have no way to know what the data type is or how many
187  // octets long it is. We only convert to machine endian when the data is requested.
188  if( IsMachineBigEndian() == false )
189  {
190  // Need to convert
191  NetValue.SwapBytes();
192  }
193 
194  // Copy into datum value.
195  for( KUINT16 i = 0; i < sizeof( Type ); ++i )
196  {
197  m_cDatumValue[i] = NetValue.m_Octs[i];
198  }
199 }
200 
201 //////////////////////////////////////////////////////////////////////////
202 
203 } // END namespace DATA_TYPES
204 } // END namespace KDIS
unsigned int KUINT32
Definition: KDefines.h:103
void SwapBytes()
Definition: KEncodersDecoders.h:61
unsigned short int KUINT16
Definition: KDefines.h:101
Definition: KEncodersDecoders.h:51
Definition: KDefines.h:131
KUINT32 m_ui32DatumID
Definition: FixedDatum.h:69
Definition: FixedDatum.h:65
DataType m_Value
Definition: KEncodersDecoders.h:57
void SetDatumValue(Type val)
Definition: FixedDatum.h:176
Definition: KDefines.h:182
Definition: KDataStream.h:48
Definition: DataTypeBase.h:49
Type GetDatumValue() const
Definition: FixedDatum.h:157
bool KBOOL
Definition: KDefines.h:119
KOCTET m_Octs[sizeof(DataType)]
Definition: KEncodersDecoders.h:55
std::string KString
Definition: KDefines.h:116
ID_Enum DatumID
Definition: EnumEntityManagement.h:570
char KOCTET
Definition: KDefines.h:108
Definition: FactoryDecoder.h:104
#define KDIS_EXPORT
Definition: KDefines.h:82
Definition: KRef_Ptr.h:73
KDIS::UTILS::KRef_Ptr< FixedDatum > FixDtmPtr
Definition: FixedDatum.h:61