EvolvingObjects
Utils.h
00001 /*
00002 (c) Thales group, 2012
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Lesser General Public
00006     License as published by the Free Software Foundation;
00007     version 2 of the License.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this library; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 Contact: http://eodev.sourceforge.net
00018 
00019 Authors:
00020     Benjamin Bouvier <benjamin.bouvier@gmail.com>
00021 */
00022 # ifndef __EOSERIAL_UTILS_H__
00023 # define __EOSERIAL_UTILS_H__
00024 
00025 # include "Array.h"
00026 # include "Object.h"
00027 # include "String.h"
00028 
00029 namespace eoserial
00030 {
00031     /* ***************************
00032      * DESERIALIZATION FUNCTIONS *
00033      *****************************
00034     These functions are useful for casting eoserial::objects into simple, primitive
00035     variables or into class instance which implement eoserial::Persistent.
00036 
00037     The model is always quite the same : 
00038     - the first argument is the containing object (which is a eoserial::Entity, 
00039     an object or an array)
00040     - the second argument is the key or index,
00041     - the last argument is the value in which we're writing.
00042     */
00043 
00044     template< class T >
00045     inline void unpack( const Object & obj, const std::string & key, T & value )
00046     {
00047         static_cast<String*>( obj.find( key )->second )->deserialize( value );
00048     }
00049 
00050     inline void unpackObject( const Object & obj, const std::string & key, Persistent & value )
00051     {
00052         static_cast<Object*>( obj.find( key )->second )->deserialize( value );
00053     }
00054 
00055     template< class Container, template<class> class UnpackAlgorithm >
00056     inline void unpackArray( const Object & obj, const std::string & key, Container & array )
00057     {
00058         static_cast<Array*>( obj.find( key )->second )->deserialize< Container, UnpackAlgorithm >( array );
00059     }
00060 
00061     template< class T >
00062     inline void unpack( const Array & array, unsigned int index, T & value )
00063     {
00064         static_cast<String*>( array[ index ] )->deserialize( value );
00065     }
00066 
00067     inline void unpackObject( const Array & array, unsigned int index, Persistent & value )
00068     {
00069         static_cast<Object*>( array[ index ] )->deserialize( value );
00070     }
00071 
00072     template< class Container, template<class> class UnpackAlgorithm >
00073     inline void unpackArray( const Array & array, unsigned int index, Container & container )
00074     {
00075         static_cast<Array*>( array[ index ] )->deserialize< Container, UnpackAlgorithm >( container );
00076     }
00077 
00078     /* *****************************
00079      *** SERIALIZATION FUNCTIONS ***
00080      *******************************
00081      These functions are useful for casting classic objects and 
00082      eoserial::Persistent objects into eoserial entities which 
00083      can be manipulated by the framework.
00084     */
00085 
00096     template <typename T>
00097     String* make( const T & value )
00098     {
00099         std::stringstream ss;
00100         ss.precision(std::numeric_limits<double>::digits10 + 1);
00101         ss << value;
00102         return new String( ss.str() );
00103     }
00104 
00109     template<>
00110     inline String* make( const std::string & value )
00111     {
00112         return new String( value );
00113     }
00114 
00115     /*
00116      * These functions are useful for automatically serializing STL containers into
00117      * eoserial arrays which could be used by the framework.
00118      **/
00119 
00123     template< class T >
00124     struct PushAlgorithm
00125     {
00132         virtual void operator()( Array & array, const T & value ) = 0;
00133     };
00134 
00141     template< class T >
00142     struct MakeAlgorithm : public PushAlgorithm<T>
00143     {
00144         void operator()( Array & array, const T & value )
00145         {
00146             array.push_back( make( value ) );
00147         }
00148     };
00149 
00153     template< class T >
00154     struct SerializablePushAlgorithm : public PushAlgorithm<T>
00155     {
00156         void operator()( Array & array, const T & obj )
00157         {
00158             // obj address is not saved into array.push_back.
00159             array.push_back( &obj );
00160         }
00161     };
00162 
00171     template< class Container, template<class> class PushAlgorithm >
00172     Array* makeArray( const Container & array )
00173     {
00174         Array* returned_array = new Array;
00175         typedef typename Container::const_iterator iterator;
00176         typedef typename Container::value_type Type;
00177         PushAlgorithm< Type > algo;
00178         for (
00179                 iterator it = array.begin(), end = array.end();
00180                 it != end;
00181                 ++it)
00182         {
00183             algo( *returned_array, *it );
00184         }
00185         return returned_array;
00186     }
00187 } // namespace eoserial
00188 
00189 # endif //__EOSERIAL_UTILS_H__
 All Classes Namespaces Files Functions Variables Typedefs Friends