KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KDefines.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  KDefines
32  created: 17/09/2008
33  author: Karl Jones
34 
35  purpose: Defines some of the core data types that KDIS uses.
36 *********************************************************************/
37 
38 #pragma once
39 
40 #include "./KSymbolicNames.h"
41 #include <cstring> // memset
42 #include <string>
43 #include <sstream>
44 #include <exception>
45 
46 #define MAX_PDU_SIZE MAX_PDU_SIZE_OCTETS
47 
48 // Comment out the following line to enable Enum descriptors or declare KDIS_USE_ENUM_DESCRIPTORS
49 // in your project pre-processor definitions (-D KDIS_USE_ENUM_DESCRIPTORS).
50 //#define KDIS_USE_ENUM_DESCRIPTORS
51 
52 #if defined( WIN32 ) | defined( _WIN32 ) | defined( WIN64 ) | defined( _WIN64 )
53 // Disable this warning, it simply warns us about any functions that have a throw qualifier.
54 #pragma warning( disable : 4290 )
55 
56 // Warning about conversion from size_t
57 #pragma warning( disable : 4267 )
58 
59 // Safe to ignore this error as we are only exporting STL objects
60 #pragma warning( disable : 4251 )
61 #endif
62 
63 /************************************************************************/
64 /* Export Options */
65 /************************************************************************/
66 
67 #ifdef EXPORT_KDIS
68  #ifdef IMPORT_KDIS
69  #pragma error( "IMPORT_KDIS & EXPORT_KDIS Can Not Be Both Defined" )
70  #endif
71 #endif
72 
73 #if defined( WIN32 ) | defined( WIN64 )
74  #if defined EXPORT_KDIS
75  #define KDIS_EXPORT __declspec( dllexport )
76  #elif defined IMPORT_KDIS
77  #define KDIS_EXPORT __declspec( dllimport )
78  #else
79  #define KDIS_EXPORT
80  #endif
81 #else
82  #define KDIS_EXPORT
83 #endif
84 
85 // Used when we include both DIS 5, 6, or 7 PDUs in a component, such as the PDU_Factory.
86 // We can use this to know what PDUs we should allow use off and what features.
87 // E.G DIS version 7 uses some of the headers padding to add an extra field.
88 #ifndef DIS_VERSION
89  #pragma message("No DIS version specified, defaulting to 6. If you wish to use 5 define 'DIS_VERSION 5' in your project. You may also define DIS version 7 for the draft 15 standard.")
90  #define DIS_VERSION 6
91 #endif
92 
93 /************************************************************************/
94 /* Type Definitions */
95 /************************************************************************/
96 
97 namespace KDIS {
98 
99 typedef unsigned char KUINT8;
100 typedef char KINT8;
101 typedef unsigned short int KUINT16;
102 typedef short int KINT16;
103 typedef unsigned int KUINT32;
104 typedef int KINT32;
105 typedef unsigned long long KUINT64;
106 typedef long long KINT64;
107 
108 typedef char KOCTET;
109 typedef unsigned char KUOCTET;
110 typedef char KCHAR8;
111 typedef unsigned char KUCHAR8;
112 
113 typedef float KFLOAT32;
114 typedef double KFLOAT64;
115 
116 typedef std::string KString;
117 typedef std::stringstream KStringStream;
118 
119 typedef bool KBOOL;
120 
121 /************************************************************************/
122 /* Error Code Definitions */
123 /************************************************************************/
124 
126 {
139 };
140 
141 /************************************************************************/
142 /* Error Code As String */
143 /************************************************************************/
144 
145 static KString GetErrorText( KUINT16 ErrorCode )
146 {
147  switch( ErrorCode )
148  {
149  case NO_ERRORS:
150  return "No Errors. ";
151  case BUFFER_TOO_SMALL:
152  return "Buffer Is Too Small. ";
154  return "Buffer Does Not Contain Enough Information To Decode. ";
156  return "String Size Too Big. ";
157  case DATA_TYPE_TOO_LARGE:
158  return "Data Type Is Too Large. ";
160  return "Incorrect PDU Type Specified In Header. ";
161  case FILE_NOT_OPEN:
162  return "Could Not Open File For Reading/Writing. ";
163  case OUT_OF_BOUNDS:
164  return "Parameter Is Out Of Bounds/Range Of Acceptable Values. ";
165  case INVALID_DATA:
166  return "Invalid Data. ";
168  return "Unsupported Data Type, Can Not Decode. ";
169  case INVALID_OPERATION:
170  return "Invalid Operation. ";
171  case PDU_TOO_LARGE:
172  return "PDU Is Too Large. PDU Must Not Exceed 8192 Bytes.";
173  default:
174  return "Unknown KDIS Error. ";
175  }
176 };
177 
178 /************************************************************************/
179 /* Exception */
180 /************************************************************************/
181 
182 class KException : public std::exception
183 {
184 public:
185 
188 
190  m_ui16ErrorCode( EC ),
191  m_sErrorText( GetErrorText( EC ) )
192  {
193  };
194 
195  KException( KString Text, KUINT16 EC ) :
196  m_ui16ErrorCode( EC ),
197  m_sErrorText( Text + ": " + GetErrorText( EC ) )
198  {
199  };
200 
201  template<class T>
202  KException( KString Text, KUINT16 EC, T AdditonalInfo ) :
203  m_ui16ErrorCode( EC ),
204  m_sErrorText( Text + ": " + GetErrorText( EC ) )
205  {
206  KStringStream ss;
207  ss << m_sErrorText << AdditonalInfo;
208  m_sErrorText = ss.str();
209  };
210 
211  virtual ~KException() throw()
212  {
213  };
214 
215  virtual const KCHAR8 * what() const throw()
216  {
217  return m_sErrorText.c_str();
218  };
219 };
220 
221 /************************************************************************/
222 /* Endian */
223 /************************************************************************/
224 
225 enum Endian
226 {
229 };
230 
231 } // END namespace KDIS
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
unsigned int KUINT32
Definition: KDefines.h:103
Definition: KDefines.h:136
Definition: KDefines.h:133
short int KINT16
Definition: KDefines.h:102
ErrorCodes
Definition: KDefines.h:125
char KCHAR8
Definition: KDefines.h:110
unsigned short int KUINT16
Definition: KDefines.h:101
Definition: KDefines.h:129
KException(KString Text, KUINT16 EC, T AdditonalInfo)
Definition: KDefines.h:202
virtual ~KException()
Definition: KDefines.h:211
Definition: KDefines.h:131
float KFLOAT32
Definition: KDefines.h:113
long long KINT64
Definition: KDefines.h:106
Definition: KDefines.h:135
Definition: KDefines.h:127
KException(KUINT16 EC)
Definition: KDefines.h:189
unsigned char KUOCTET
Definition: KDefines.h:109
Definition: KDefines.h:182
Definition: KDefines.h:228
int KINT32
Definition: KDefines.h:104
KUINT16 m_ui16ErrorCode
Definition: KDefines.h:186
unsigned long long KUINT64
Definition: KDefines.h:105
char KINT8
Definition: KDefines.h:100
Definition: KDefines.h:134
bool KBOOL
Definition: KDefines.h:119
Definition: KDefines.h:128
Definition: KDefines.h:130
Definition: KDefines.h:137
KString m_sErrorText
Definition: KDefines.h:187
std::string KString
Definition: KDefines.h:116
double KFLOAT64
Definition: KDefines.h:114
unsigned char KUCHAR8
Definition: KDefines.h:111
Endian
Definition: KDefines.h:225
Definition: KDefines.h:132
KException(KString Text, KUINT16 EC)
Definition: KDefines.h:195
std::stringstream KStringStream
Definition: KDefines.h:117
Definition: KDefines.h:138
unsigned char KUINT8
Definition: KDefines.h:99
char KOCTET
Definition: KDefines.h:108
virtual const KCHAR8 * what() const
Definition: KDefines.h:215
Definition: KDefines.h:227