EncoderProtocol.h

00001 /**
00002  * @class   wosh::EncoderProtocol
00003  * @brief   
00004  *
00005  * @ingroup CoreInterfaces
00006  ****************************************************************************
00007  * @version 0.8.399 $Id: EncoderProtocol.h 3775 2011-01-01 16:38:17Z alex $
00008  * @author  Alessandro Polo
00009  ****************************************************************************/
00010 /* Copyright (c) 2007-2011, WOSH - Wide Open Smart Home 
00011  * by Alessandro Polo - OpenSmartHome.com
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *     * Redistributions of source code must retain the above copyright
00017  *       notice, this list of conditions and the following disclaimer.
00018  *     * Redistributions in binary form must reproduce the above copyright
00019  *       notice, this list of conditions and the following disclaimer in the
00020  *       documentation and/or other materials provided with the distribution.
00021  *     * Neither the name of the OpenSmartHome.com WOSH nor the
00022  *       names of its contributors may be used to endorse or promote products
00023  *       derived from this software without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
00026  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00030  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00032  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00034  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  ****************************************************************************/
00036 
00037 #ifndef __WOSH_Core_EncoderProtocol_H__
00038  #define __WOSH_Core_EncoderProtocol_H__
00039 
00040  #include <woshDefs.h>
00041  #include <core/Object.h>
00042  #include <core/LogContext.h>
00043  #include <core/MethodsProvider.h>
00044  #include <core/PropertiesProvider.h>
00045  #include <core/DataFields.h>
00046 
00047 
00048 namespace wosh {
00049 
00050 class StreamSink;
00051 class BufferStreamInput;
00052 
00053 #undef WOSH_ENCODERBASE
00054 #define WOSH_ENCODERBASE(CLASSNAME, OBJECT_TYPE) \
00055         WOSH_CLASSNAME(CLASSNAME)                                           \
00056         public: static const char* mappedObject() { return #OBJECT_TYPE; }  \
00057         public: virtual inline const char* getMappedObject() const { return mappedObject(); }
00058 
00059 
00060 class EncoderOptions
00061  {
00062     public:
00063         EncoderOptions() {
00064             clear();
00065          }
00066         virtual ~EncoderOptions() { }
00067         
00068         static EncoderOptions Compact( bool value = true );
00069 
00070     public:
00071         inline bool isCompact() const                   { return this->compact; }
00072 
00073     public:
00074         inline void setCompact( bool value = true )     { this->compact = value; }
00075 
00076         virtual void clear();
00077         
00078 
00079     public:
00080         virtual EncoderOptions& operator=(const EncoderOptions& m);
00081         virtual bool operator==(const EncoderOptions &other) const;
00082         virtual bool operator!=(const EncoderOptions &other) const { return !(*this == other);  }
00083 
00084     protected:
00085         bool compact;
00086 
00087 }; // class def
00088 //-------------------------------------------------------------------------------------------------------------------
00089 
00090 
00091 class EncoderBase : public virtual IReflection,
00092                     public virtual IDataFieldRW
00093  {
00094     WOSH_CLASSNAME(wosh::EncoderBase)
00095     WOSH_DF_MEYER_RW(wosh::EncoderBase, EncoderBase)
00096 
00097     public:
00098         virtual WRESULT serializeTo( const ISerializable* object, StreamSink& output, const EncoderOptions& options ) = 0;
00099         virtual int64 deserializeFrom( ISerializable* object, const char* buffer, int64 buffer_size ) = 0;
00100 
00101     public:
00102         virtual const char* getMappedObject() const = 0;
00103 
00104     public:
00105         virtual ~EncoderBase() { }
00106 
00107 
00108 }; // class def
00109 //-------------------------------------------------------------------------------------------------------------------
00110 
00111 #undef WOSH_ENCODER_BIND
00112 #define WOSH_ENCODER_BIND(ENCODER_PROTOCOL, ENCODER_CLASS, OBJECT_CLASSNAME, VERSION, TMP ) \
00113     wosh::EncoderMapperT<ENCODER_PROTOCOL> static_encoder_mapper_##TMP(new ENCODER_CLASS(), OBJECT_CLASSNAME, VERSION); 
00114 
00115 template <class ENCODER_PROTOCOL>
00116 class EncoderMapperT
00117  {
00118     public:
00119         EncoderMapperT( EncoderBase* obj, const char* classname, double current_version = 0.0 ) {
00120             this->object = NULL;
00121             registerMySelf(obj, classname, current_version);
00122         }
00123         virtual ~EncoderMapperT() {
00124             unRegisterMySelf();
00125          }
00126 
00127     private:
00128         void registerMySelf( EncoderBase* obj, const char* classname, double current_version = 0.0 ) {
00129             if ( obj == NULL || classname == NULL || strlen(classname) == 0 ) return;
00130             this->object = obj;
00131             ObjectTypeInfo info;
00132             info.name = classname;
00133             info.version = current_version;
00134             info.allocable = true;
00135             ENCODER_PROTOCOL::getEncoders().set_safe( info, this->object);
00136          }
00137 
00138         void unRegisterMySelf() {
00139             if ( this->object == NULL ) return;
00140             ENCODER_PROTOCOL::getEncoders().transactionBeginWrite();
00141             ENCODER_PROTOCOL::getEncoders().eraseByValue(this->object);
00142             delete this->object; this->object = NULL;
00143             ENCODER_PROTOCOL::getEncoders().transactionEnd();
00144          }
00145 
00146     protected:
00147         EncoderBase* object;
00148         friend class PersistenceManager;
00149         friend class EncoderProtocol;
00150 
00151 }; // class def
00152 //-------------------------------------------------------------------------------------------------------------------
00153 
00154 
00155 class EncoderProtocol;
00156 class ObjectTypeInfo;
00157 class PersistenceDb;
00158 
00159 class IEncoderProtocolListener
00160  {
00161     public:
00162         virtual void encoder_protocol_db_action( const std::string& db_name, EncoderProtocol* source ) = 0;
00163 
00164     public:
00165         virtual ~IEncoderProtocolListener() { }
00166 
00167 }; // class def
00168 //-------------------------------------------------------------------------------------------------------------------
00169 
00170 
00171 typedef XX_MapT<wosh::ObjectTypeInfo, wosh::EncoderBase*> tObjectTypeInfoEncoderBaseMap;
00172 
00173 #undef WOSH_ENCODERPROTOCOL
00174 #define WOSH_ENCODERPROTOCOL(CLASSNAME)                                                                                                             \
00175         WOSH_CLASSNAME(CLASSNAME)                                                                                                                   \
00176         public: inline const tObjectTypeInfoEncoderBaseMap& getEncodersBase() const { return getEncoders(); }                                       \
00177         public: static WRESULT SerializeTo( const ISerializable* object, StreamSink& output, const EncoderOptions& options = EncoderOptions() ) {   \
00178             return EncoderProtocol::s_serializeTo( getEncoders(), object, output, options );                                                        \
00179          }                                                                                                                                          \
00180         protected: inline tObjectTypeInfoEncoderBaseMap& getEncodersBase() { return getEncoders(); }                                                \
00181         protected: static inline wosh::EncoderBase* getEncoderFor_( const char* classname, double version = 0.0, bool recursive = true ) {          \
00182             return selectEncoder_(getEncoders(), classname, version, recursive );                                                                   \
00183          }                                                                                                                                          \
00184         protected: static tObjectTypeInfoEncoderBaseMap& getEncoders() {                                                                            \
00185             static tObjectTypeInfoEncoderBaseMap static_encoders;                                                                                   \
00186             return static_encoders;                                                                                                                 \
00187          }                                                                                                                                          \
00188         protected: friend class wosh::EncoderMapperT<CLASSNAME>;                                                                                    \
00189 // do not touch this line
00190 
00191 
00192  #define _EncoderProtocol_KEY_ProtocolPrefix        "ProtocolPrefix"
00193  #define _EncoderProtocol_KEY_ProtocolVersion       "ProtocolVersion"
00194  #define _EncoderProtocol_KEY_Priority              "Priority"
00195 
00196 class EncoderProtocol : public Object,
00197                         public virtual IPropertiesProviderListener,
00198                         public MethodRunner
00199  {
00200     WOSH_CLASSNAME(wosh::EncoderProtocol)
00201     WOSH_DF_MEYER_RW(wosh::EncoderProtocol, EncoderProtocol)
00202 
00203     public:
00204         EncoderProtocol();
00205         virtual ~EncoderProtocol();
00206 
00207 
00208     public:
00209         virtual const char* getProtocol() const = 0;
00210         virtual double getProtocolVersion() const = 0;
00211 
00212     public:
00213         virtual WRESULT loadObjects( std::vector<ISerializable*>& objects, const std::string& parameters ) = 0;
00214 
00215         virtual WRESULT saveObject( const ISerializable* object, const std::string& parameters, const EncoderOptions& options = EncoderOptions() ) = 0;
00216         virtual WRESULT saveObjects( const std::vector<ISerializable*>& objects, const std::string& parameters, const EncoderOptions& options = EncoderOptions() ) = 0;
00217 
00218     public:
00219         virtual bool isSupported( const char* classname, double version = 0.0 ) const;
00220 
00221         virtual WRESULT serializeTo( const ISerializable* object, StreamSink& output, const EncoderOptions& options = EncoderOptions() );
00222         virtual int64 deserializeFrom( ISerializable* object, const char* buffer, int64 buffer_size );
00223 
00224     protected:
00225         static WRESULT s_serializeTo( tObjectTypeInfoEncoderBaseMap& encoders, const ISerializable* object, StreamSink& output, const EncoderOptions& options = EncoderOptions() );
00226 
00227         virtual WRESULT init();
00228 
00229 /** @name Getters
00230  * @{
00231  ******************************************************************************/
00232     public:
00233         virtual const tObjectTypeInfoEncoderBaseMap& getEncodersBase() const = 0;
00234     protected:
00235         virtual tObjectTypeInfoEncoderBaseMap& getEncodersBase() = 0;
00236     public:
00237 
00238         const PropertiesProvider& getProperties() const { return Properties; }
00239         PropertiesProvider& getProperties()             { return Properties; }
00240 
00241         const MethodsProvider& getMethods() const       { return this->Methods; }
00242         MethodsProvider& getMethods()                   { return this->Methods; }
00243 
00244 //@}
00245 /** @name Setters
00246  * @{
00247  ******************************************************************************/
00248     public:
00249         virtual WRESULT setListener( IEncoderProtocolListener* enc_l );
00250 
00251 //@}
00252 /** @name IPropertiesProviderListener interface
00253  * @{
00254  ******************************************************************************/
00255     public:
00256         virtual bool readingProperty( Property*, const PropertiesProvider* ) { return true; }
00257         virtual bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );
00258 //@}
00259 /** @name Internal methods
00260  * @{
00261  ******************************************************************************/
00262     protected:
00263 //@}
00264 
00265         static EncoderBase* selectEncoder_( tObjectTypeInfoEncoderBaseMap& encoders, const char* classname, double version = 0.0, bool recursive = true );
00266 
00267 
00268     protected:
00269         mutable PropertiesProvider Properties;  ///< my configuration
00270         mutable MethodsProvider Methods;        ///< my methods
00271         mutable LogContext Log;                 ///< my logger
00272 
00273         bool initialized;
00274 
00275     private:
00276         IEncoderProtocolListener* listener;     ///< class listening for events
00277 
00278         friend class PersistenceManager;        ///< my big brother
00279 
00280 }; // class def
00281 //-------------------------------------------------------------------------------------------------------------------
00282 
00283 
00284 }; // namespace wosh
00285 
00286 #endif //__WOSH_Core_EncoderProtocol_H__

Generated on Tue Feb 8 2011 09:32:51 for WOSH system 0.8.888 [wolf] by Alessandro Polo, using DoxyGen 1.7.2 hosted by WOSH Framework