chore(include): changed include files from .h to .hpp
[KDIS.git] / include / KDIS / DataTypes / FactoryDecoder.hpp
blob839bf704f1774aac6d043456c1b07aa68b5b3328
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 /********************************************************************
31 class: FactoryDecoder
32 created: 15/03/2011
33 author: Karl Jones
35 purpose: This is an Abstract class that can be used to create your own custom decoders for some of the more extendable data types.
36 For example the VariableDatum can support many different variations including your own custom ones, using a FactoryDecoder
37 you can add your custom classes into KDIS.
39 If you want KDIS to decode your custom classes simply call the RegisterFactoryDecoder function of the relevant class.
41 Example:
43 // You could create a seperate decoder class just for the decoding task, here I use multiple inheritance...
44 class MyCustomVariableDatumClass : public variableDatum, public FactoryDecoder<VariableDatum,
46 ...
47 variableDatum * FactoryDecode( int TypeEnum, KDataStream & stream )
49 // Do you custom decoding in here.
51 ...
55 // Now when your application starts up register you new FactoryDecoderClass like so:
57 // 123 could be your type enum value.
58 VariableDatum::RegisterFactoryDecoder( 123, new MyCustomVariableDatumClass );
60 // Now when ever a VariableDatum with type 123 is received it will be decoded with your custom class.
62 // When you no longer need the custom decoders you should call 'ClearFactoryDecoders', which can also free up the memory for you.
64 *********************************************************************/
66 #pragma once
68 #include "./../KDataStream.h"
69 #include "./../Extras/KRef_Ptr.h"
70 #include <map>
72 namespace KDIS {
73 namespace DATA_TYPE {
75 template<class DecoderBaseTyp>
76 class FactoryDecoder
78 public:
80 FactoryDecoder(){};
82 virtual ~FactoryDecoder(){};
84 //************************************
85 // FullName: KDIS::DATA_TYPE::FactoryDecoder::FactoryDecode
86 // Description: This is where you decode the stream into your custom class,
87 // the enum value is also passed back so you can perform decoding
88 // of multiple types within a single FactoryDecoder.
89 // Parameter: KINT32 EnumVal
90 // Parameter: KDataStream & stream
91 //************************************
92 virtual DecoderBaseTyp * FactoryDecode( KINT32 EnumVal, KDataStream & stream ) = 0;
95 /********************************************************************
96 class: FactoryDecoderUser
97 created: 17/03/2011
98 author: Karl Jones
100 purpose: This class adds support for custom decoding.
101 *********************************************************************/
103 template<class DecoderBaseTyp>
104 class FactoryDecoderUser
106 public:
108 typedef KDIS::UTILS::KRef_Ptr< FactoryDecoder<DecoderBaseTyp> > FacDecPtr;
110 protected:
112 static std::map<KINT32, FacDecPtr> m_mDecoders;
114 public:
116 //************************************
117 // FullName: KDIS::DATA_TYPE::FactoryDecoderUser::RegisterFactoryDecoder
118 // Description: Registers a decoder for a custom class.
119 // EnumVal is the relevant enum value representing the new class.
120 // E.G DatumID enum for VariableDatum.
121 // Exception thrown if a decoder already exists for this enum.
122 // Parameter: KINT32 EnumVal
123 // Parameter: FacDecPtr Decoder
124 //************************************
125 static void RegisterFactoryDecoder( KINT32 EnumVal, FacDecPtr Decoder )
127 if( m_mDecoders.find( EnumVal ) != m_mDecoders.end() )
129 KStringStream ss;
130 ss << "A decoder already exists for this enum: " << EnumVal;
131 throw KException( __FUNCTION__, INVALID_OPERATION, ss.str() );
134 // Register the new decoder.
135 m_mDecoders[EnumVal] = Decoder;
138 //************************************
139 // FullName: KDIS::DATA_TYPE::FactoryDecoderUser::FactoryDecode
140 // Description: Attempts to find a decoder for the enum, returns NULL if none are found.
141 // Note: An exception may be thrown by a decoder.
142 // Parameter: KINT32 EnumVal
143 // Parameter: KDataStream & stream
144 //************************************
145 static DecoderBaseTyp * FactoryDecode( KINT32 EnumVal, KDataStream & stream )
147 // Try to find a decoder
148 typename std::map<KINT32, FacDecPtr>::iterator itr = m_mDecoders.find( EnumVal );
149 if( itr != m_mDecoders.end() )
151 return itr->second->FactoryDecode( EnumVal, stream );
154 // No decoders found so return NULL.
155 return 0;
158 //************************************
159 // FullName: KDIS::DATA_TYPE::FactoryDecoderUser::FactoryDecode
160 // Description: Removes all factory decoders.
161 //************************************
162 static void ClearFactoryDecoders()
164 m_mDecoders.clear();
168 // Init static map variable.
169 template<class DecoderBaseTyp>
170 std::map<KINT32, KDIS::UTILS::KRef_Ptr< FactoryDecoder<DecoderBaseTyp> > > FactoryDecoderUser<DecoderBaseTyp>::m_mDecoders = std::map<KINT32, KDIS::UTILS::KRef_Ptr< FactoryDecoder<DecoderBaseTyp> > >();
172 } // END namespace DATA_TYPES
173 } // END namespace KDIS