KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KEncodersDecoders.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  KDISEncodersDecoders
32  created: 17/9/2008
33  author: Karl Jones
34 
35  purpose: Implementation of Encoding and Decoding classes.
36  Convert an array of KOCTETs/bytes/chars etc into a
37  appropriate data type
38 *********************************************************************/
39 
40 #pragma once
41 
42 #include "./KDefines.h"
43 
44 /************************************************************************/
45 /* Encode / Decode Network Data. */
46 /************************************************************************/
47 
48 namespace KDIS {
49 
50 template<class DataType>
52 {
53  struct
54  {
55  KOCTET m_Octs[sizeof( DataType )];
56  };
57  DataType m_Value;
58 
59  //////////////////////////////////////////////////////
60 
61  void SwapBytes()
62  {
63  KOCTET Temp[sizeof( DataType )];
64 
65  // Copy Data To temp;
66  for( KUINT16 i = 0; i < sizeof( DataType ); ++i )
67  {
68  Temp[i] = m_Octs[i];
69  }
70 
71  // Now swap bytes
72  KUINT16 j = ( sizeof( DataType ) ) - 1 ;
73 
74  for( KUINT16 i = 0; i < sizeof( DataType ); ++i )
75  {
76  m_Octs[i] = Temp[j];
77  --j;
78  }
79  };
80 
81  //////////////////////////////////////////////////////
82 
84  {
85  };
86 
87  //////////////////////////////////////////////////////
88 
89  NetToDataType( const KOCTET * O, KBOOL SwapByteOrder = true )
90  {
91  for( KUINT16 i = 0; i < sizeof( DataType ); ++i )
92  {
93  m_Octs[i] = O[i];
94  }
95 
96  if( SwapByteOrder )SwapBytes();
97  };
98 
99  //////////////////////////////////////////////////////
100 
101  NetToDataType( DataType DT, KBOOL SwapByteOrder = true )
102  {
103  m_Value = DT;
104 
105  if( SwapByteOrder )SwapBytes();
106  };
107 };
108 
109 /************************************************************************/
110 /* Typedefs for all conversions */
111 /************************************************************************/
112 
120 
121 };
NetToDataType< KINT16 > NetToKINT16
Definition: KEncodersDecoders.h:116
void SwapBytes()
Definition: KEncodersDecoders.h:61
unsigned short int KUINT16
Definition: KDefines.h:101
Definition: KEncodersDecoders.h:51
NetToDataType()
Definition: KEncodersDecoders.h:83
NetToDataType< KUINT64 > NetToKUINT64
Definition: KEncodersDecoders.h:117
NetToDataType< KFLOAT64 > NetToKFLOAT64
Definition: KEncodersDecoders.h:119
NetToDataType< KFLOAT32 > NetToKFLOAT32
Definition: KEncodersDecoders.h:118
DataType m_Value
Definition: KEncodersDecoders.h:57
bool KBOOL
Definition: KDefines.h:119
NetToDataType(DataType DT, KBOOL SwapByteOrder=true)
Definition: KEncodersDecoders.h:101
KOCTET m_Octs[sizeof(DataType)]
Definition: KEncodersDecoders.h:55
NetToDataType< KUINT16 > NetToKUINT16
Definition: KEncodersDecoders.h:114
char KOCTET
Definition: KDefines.h:108
NetToDataType< KINT32 > NetToKINT32
Definition: KEncodersDecoders.h:115
NetToDataType< KUINT32 > NetToKUINT32
Definition: KEncodersDecoders.h:113
NetToDataType(const KOCTET *O, KBOOL SwapByteOrder=true)
Definition: KEncodersDecoders.h:89