chore: structure basic
[KDIS.git] / Examples / Extending / FactoryDecoder / KDIS.cpp
blobf8b9c8c7fc8b5570357d14345e887cb2db0565d9
1 /**********************************************************************
2 The following UNLICENSE statement applies to this example.
4 This is free and unencumbered software released into the public domain.
6 Anyone is free to copy, modify, publish, use, compile, sell, or
7 distribute this software, either in source code form or as a compiled
8 binary, for any purpose, commercial or non-commercial, and by any
9 means.
11 In jurisdictions that recognize copyright laws, the author or authors
12 of this software dedicate any and all copyright interest in the
13 software to the public domain. We make this dedication for the benefit
14 of the public at large and to the detriment of our heirs and
15 successors. We intend this dedication to be an overt act of
16 relinquishment in perpetuity of all present and future rights to this
17 software under copyright law.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
27 For more information, please refer to <http://unlicense.org/>
28 *********************************************************************/
30 /*********************************************************************
31 For Further Information on KDIS:
32 http://p.sf.net/kdis/UserGuide
34 This example shows how you can add your custom data types to KDIS.
35 In this example we create our own version of a VariableDatum.
37 WARNING: If you create a derived data type you will also need to ensure that it is correctly padded. See the DIS standard for specifics.
39 For the accompanying tutorial:
40 https://sourceforge.net/p/kdis/wiki/How_to_extend_the_KDIS_Data_Types_using_the_FactoryDecoder/
41 *********************************************************************/
43 #include <iostream>
44 #include "KDIS/Extras/PDU_Factory.h"
45 #include "KDIS/DataTypes/VariableDatum.h"
46 #include "KDIS/PDU/Simulation_Management/Comment_PDU.h"
48 using namespace std;
49 using namespace KDIS;
50 using namespace DATA_TYPE;
51 using namespace PDU;
52 using namespace UTILS;
55 // This is our custom class, it will be used to decode any data that has the DatumID '500123'
57 class MyCustomClass : public VariableDatum
59 private:
61 // Lets use a 64 bit value for testing so we dont have to add any padding.
62 KUINT64 m_ui64OurTestValue;
64 public:
66 MyCustomClass( KUINT64 Val )
68 m_ui32DatumID = 500123; // Our custom DatumID.
69 m_ui32DatumLength = 64; // The Datum length is the size in bits of our custom data
70 m_ui64OurTestValue = Val;
73 MyCustomClass( KDataStream & stream )
75 Decode( stream );
78 virtual ~MyCustomClass()
82 virtual KString GetAsString() const
84 KStringStream ss;
86 ss << "This is our custom VariableDatum!!!! Our value is " << m_ui64OurTestValue << "\n";
88 return ss.str();
91 // Our custom decode function.
92 virtual void Decode( KDataStream & stream )
94 // Deocode the standard variable datum fields
95 stream >> m_ui32DatumID
96 >> m_ui32DatumLength;
98 // Now decode our 'extra' field.
99 stream >> m_ui64OurTestValue;
102 virtual void Encode( KDataStream & stream ) const
104 // Encode the standard variable datum fields
105 stream << m_ui32DatumID
106 << m_ui32DatumLength;
108 // Now encode our 'extra' field.
109 stream << m_ui64OurTestValue;
114 // Our decoder class
116 class MyDecoderClass : public FactoryDecoder<VariableDatum>
118 public:
120 MyDecoderClass(){};
122 ~MyDecoderClass(){};
124 // This is our factory decode function, we do the decoding for KDIS in here.
125 virtual VariableDatum * FactoryDecode( KINT32 EnumVal, KDataStream & stream )
127 // This should allways return a new data type, however you can return NULL. We could use a single decoder class to support multiple custom data types....
128 if( EnumVal == 500123 )
130 return new MyCustomClass( stream );
132 else return 0;
137 int main()
141 // 1
142 // Register our factory decode function.
143 VariableDatum::FacDecPtr decoder( new MyDecoderClass() );
144 VariableDatum::RegisterFactoryDecoder( 500123, decoder );
146 // 2
147 // Create an instance of our data type.
148 MyCustomClass * pMyCustomDataClass = new MyCustomClass( 123321 );
150 // 3
151 // Now create a PDU that uses this data type and add it like normal.
152 Comment_PDU ourPDU;
153 ourPDU.AddVariableDatum( pMyCustomDataClass );
155 // 4
156 // Encode the PDU.
157 KDataStream stream = ourPDU.Encode();
159 // 5
160 // Lets skip actually sending this data over the network in order to keep things simple..
163 // 6
164 // Now decode the PDU as if we have just received it from the network.
165 PDU_Factory pduFact;
166 unique_ptr<Header> pdu = pduFact.Decode( stream );
168 if( pdu.get() )
170 // Here we should see that our variable datum has been correctly decoded.
171 cout << pdu->GetAsString() << endl;
173 // Lets get the decoded class
174 Comment_PDU * ourRecPDU = ( Comment_PDU * )pdu.get();
176 // Get the first and only item.
177 VarDtmPtr pVd = ourRecPDU->GetVariableDatum()[0];
179 // Now upcast so we get our data type.
180 MyCustomClass * pMyClass = dynamic_cast<MyCustomClass*>( ( VariableDatum * )pVd );
182 if( pMyClass )
184 cout << "Ta da!, we got the custom data class back out!" << endl;
187 else
189 cout << "Something went horribly wrong!" << endl;
192 catch( exception & e )
194 cout << e.what() << endl;
197 // 7
198 // Now we should clean up memory. Our decoder will not be deleted unless we call ClearFactoryDecoders.
199 VariableDatum::ClearFactoryDecoders();
201 return 0;