EvolvingObjects
|
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_ARRAY_H__ 00023 # define __EOSERIAL_ARRAY_H__ 00024 00025 # include <vector> 00026 00027 # include "Entity.h" 00028 # include "Serializable.h" 00029 # include "Object.h" 00030 00031 namespace eoserial 00032 { 00033 00034 // Forward declaration for below declarations. 00035 class Array; 00036 00037 /* 00038 * Declarations of functions present in Utils.h 00039 * These are put here to avoid instead of including the file Utils.h, which would 00040 * cause a circular inclusion. 00041 */ 00042 00043 template< class T > 00044 void unpack( const Array & array, unsigned int index, T & value ); 00045 00046 void unpackObject( const Array & array, unsigned int index, Persistent & value ); 00047 00048 template< class Container, template<class> class UnpackAlgorithm > 00049 void unpackArray( const Array & array, unsigned int index, Container & container ); 00050 00058 class Array : public eoserial::Entity, public std::vector< eoserial::Entity* > 00059 { 00060 protected: 00061 typedef std::vector< eoserial::Entity* > ArrayChildren; 00062 00063 public: 00068 void push_back( const eoserial::Printable* obj ) 00069 { 00070 ArrayChildren::push_back( obj->pack() ); 00071 } 00072 00076 void push_back( eoserial::Entity* json ) 00077 { 00078 ArrayChildren::push_back( json ); 00079 } 00080 00085 virtual std::ostream& print( std::ostream& out ) const; 00086 00090 ~Array(); 00091 00092 /* 00093 * The following parts allows the user to automatically deserialize an eoserial::Array into a 00094 * standard container, by giving the algorithm which will be used to deserialize contained entities. 00095 */ 00096 00103 template<class Container> 00104 struct BaseAlgorithm 00105 { 00113 virtual void operator()( const eoserial::Array& array, unsigned int i, Container & container ) const = 0; 00114 }; 00115 00122 template<typename C> 00123 struct UnpackAlgorithm : public BaseAlgorithm<C> 00124 { 00125 void operator()( const eoserial::Array& array, unsigned int i, C & container ) const 00126 { 00127 typename C::value_type t; 00128 unpack( array, i, t ); 00129 container.push_back( t ); 00130 } 00131 }; 00132 00138 template<typename C> 00139 struct UnpackObjectAlgorithm : public BaseAlgorithm<C> 00140 { 00141 void operator()( const eoserial::Array& array, unsigned int i, C & container ) const 00142 { 00143 typename C::value_type t; 00144 unpackObject( array, i, t ); 00145 container.push_back( t ); 00146 } 00147 }; 00148 00154 template<class Container, template<class T> class UnpackAlgorithm> 00155 inline void deserialize( Container & array ) 00156 { 00157 UnpackAlgorithm< Container > algo; 00158 for( unsigned int i = 0, size = this->size(); 00159 i < size; 00160 ++i) 00161 { 00162 algo( *this, i, array ); 00163 } 00164 } 00165 }; 00166 00167 } // namespace eoserial 00168 00169 # endif // __EOSERIAL_ARRAY_H__ 00170