KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FactoryDecoder.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: FactoryDecoder
32  created: 15/03/2011
33  author: Karl Jones
34 
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.
38 
39  If you want KDIS to decode your custom classes simply call the RegisterFactoryDecoder function of the relevant class.
40 
41  Example:
42 
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,
45  {
46  ...
47  variableDatum * FactoryDecode( int TypeEnum, KDataStream & stream )
48  {
49  // Do you custom decoding in here.
50  }
51  ...
52  }
53 
54 
55  // Now when your application starts up register you new FactoryDecoderClass like so:
56 
57  // 123 could be your type enum value.
58  VariableDatum::RegisterFactoryDecoder( 123, new MyCustomVariableDatumClass );
59 
60  // Now when ever a VariableDatum with type 123 is received it will be decoded with your custom class.
61 
62  // When you no longer need the custom decoders you should call 'ClearFactoryDecoders', which can also free up the memory for you.
63 
64 *********************************************************************/
65 
66 #pragma once
67 
68 #include "./../KDataStream.h"
69 #include "./../Extras/KRef_Ptr.h"
70 #include <map>
71 
72 namespace KDIS {
73 namespace DATA_TYPE {
74 
75 template<class DecoderBaseTyp>
77 {
78 public:
79 
81 
82  virtual ~FactoryDecoder(){};
83 
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;
93 };
94 
95 /********************************************************************
96  class: FactoryDecoderUser
97  created: 17/03/2011
98  author: Karl Jones
99 
100  purpose: This class adds support for custom decoding.
101 *********************************************************************/
102 
103 template<class DecoderBaseTyp>
105 {
106 public:
107 
109 
110 protected:
111 
112  static std::map<KINT32, FacDecPtr> m_mDecoders;
113 
114 public:
115 
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 ) throw( KException )
126  {
127  if( m_mDecoders.find( EnumVal ) != m_mDecoders.end() )
128  {
129  KStringStream ss;
130  ss << "A decoder already exists for this enum: " << EnumVal;
131  throw KException( __FUNCTION__, INVALID_OPERATION, ss.str() );
132  }
133 
134  // Register the new decoder.
135  m_mDecoders[EnumVal] = Decoder;
136  }
137 
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 ) throw( KException )
146  {
147  // Try to find a decoder
148  typename std::map<KINT32, FacDecPtr>::iterator itr = m_mDecoders.find( EnumVal );
149  if( itr != m_mDecoders.end() )
150  {
151  return itr->second->FactoryDecode( EnumVal, stream );
152  }
153 
154  // No decoders found so return NULL.
155  return 0;
156  }
157 
158  //************************************
159  // FullName: KDIS::DATA_TYPE::FactoryDecoderUser::FactoryDecode
160  //!Description: Removes all factory decoders.
161  //************************************
162  static void ClearFactoryDecoders()
163  {
164  m_mDecoders.clear();
165  }
166 };
167 
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> > >();
171 
172 } // END namespace DATA_TYPES
173 } // END namespace KDIS
174 
virtual ~FactoryDecoder()
Definition: FactoryDecoder.h:82
KDIS::UTILS::KRef_Ptr< FactoryDecoder< DecoderBaseTyp > > FacDecPtr
Definition: FactoryDecoder.h:108
static void ClearFactoryDecoders()
Description: Removes all factory decoders.
Definition: FactoryDecoder.h:162
Definition: FactoryDecoder.h:76
static DecoderBaseTyp * FactoryDecode(KINT32 EnumVal, KDataStream &stream)
Note: An exception may be thrown by a decoder.
Definition: FactoryDecoder.h:145
FactoryDecoder()
Definition: FactoryDecoder.h:80
static std::map< KINT32, FacDecPtr > m_mDecoders
Definition: FactoryDecoder.h:112
Definition: KDefines.h:182
Definition: KDataStream.h:48
int KINT32
Definition: KDefines.h:104
virtual DecoderBaseTyp * FactoryDecode(KINT32 EnumVal, KDataStream &stream)=0
Definition: KDefines.h:137
std::stringstream KStringStream
Definition: KDefines.h:117
Definition: FactoryDecoder.h:104
Definition: KRef_Ptr.h:73
static void RegisterFactoryDecoder(KINT32 EnumVal, FacDecPtr Decoder)
Definition: FactoryDecoder.h:125