S3FC project page S3FC home page

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

s3_streamable.tcc

Go to the documentation of this file.
00001 /*
00002  * Stone Three Foundation Class (s3fc) provides a number of utility classes.
00003  * Copyright (C) 2001 by Stone Three Signal Processing (Pty) Ltd.
00004  *
00005  * Authored by Stone Three Signal Processing (Pty) Ltd.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  * 
00021  * Please see the file 'COPYING' in the source root directory.
00022  */
00023 
00024 
00025 #ifndef S3_STREAMABLE_TCC 
00026 #define S3_STREAMABLE_TCC
00027 
00028 #include <s3fc/s3_streamable.h>
00029 #include <sstream>
00030 #include <typeinfo>
00031 
00032 //
00033 template<typename T>
00034 void s3_streamable_helper_pod<T>::pack(const T& t,
00035                    s3_pack_buffer& b)
00036 {
00037 #ifdef S3_VERBOSE_PACKING
00038    std::stringstream str;
00039    str << "POD " << typeid(T).name() << ":";
00040    s3_streamable::pack_string(str.str(), b);
00041 #endif
00042    b.copy_in(&t, sizeof(T));
00043 }
00044 
00045 //
00046 template<typename T>
00047 void s3_streamable_helper_pod<T>::unpack(T &t, 
00048                 const s3_pack_buffer& b)
00049 {
00050 #ifdef S3_VERBOSE_PACKING
00051    s3_streamable::unpack_string(b);
00052 #endif   
00053    b.copy_out(&t, sizeof(T));
00054 }
00055 
00056 //
00057 template<typename T>
00058 void s3_streamable_helper_pod<T>::pack_array(const T* const &t,
00059                     s3_pack_buffer& b,
00060                     const size_t& n)
00061 {
00062 #ifdef S3_VERBOSE_PACKING
00063    std::stringstream str;
00064    str << "POD " << typeid(T).name() << "[" << n << "]:";
00065    s3_streamable::pack_string(str.str(), b);
00066 #endif
00067    // write size
00068    b.copy_in(&n, sizeof(size_t));
00069    // write data
00070    b.copy_in(t, sizeof(T)*n);
00071 }
00072 
00073 //
00074 template<typename T>
00075 void s3_streamable_helper_pod<T>::unpack_array(T* &t, 
00076                       const s3_pack_buffer& b,
00077                       bool inplace)
00078 {
00079 #ifdef S3_VERBOSE_PACKING
00080    s3_streamable::unpack_string(b);
00081 #endif   
00082    // read size
00083    size_t n;
00084    b.copy_out(&n, sizeof(size_t));
00085    
00086    if ( ! inplace ) // alloc memory and swing pointer
00087    {
00088       // FIXME or CHECKME: Valgrind reports that this allocation is never deallocated
00089       t = new T[n];
00090    }
00091    // copy from packed data
00092    b.copy_out(t, sizeof(T)*n);
00093 }
00094 
00095 //
00096 template<typename T>
00097 void s3_streamable_helper_streamable<T>::pack(const T& t,
00098                      s3_pack_buffer &b)
00099 {
00100 #ifdef S3_VERBOSE_PACKING
00101    std::stringstream str;
00102    str << "s3_streamable (" << t.get_magic_string() << "):";
00103    s3_streamable::pack_string(str.str(), b);
00104 #endif
00105    t.pack(b);
00106 }
00107 
00108 //
00109 template<typename T>
00110 void s3_streamable_helper_streamable<T>::unpack(T& t,
00111                   const s3_pack_buffer &b)
00112 {
00113 #ifdef S3_VERBOSE_PACKING
00114    s3_streamable::unpack_string(b);
00115 #endif
00116    t.unpack(b);
00117 }
00118 
00119 // STL Container pack
00120 template<typename T>
00121 void s3_streamable_helper_stl<T>::pack(const T& t,
00122                    s3_pack_buffer& b)
00123 {
00124    // t.size(), t.begin() and t.end() should be valid for T
00125    typename T::size_type s = t.size();
00126    s3_streamable::pack(s, b);
00127    if ( s != 0 )
00128    {
00129       for (typename T::const_iterator ptr = t.begin();
00130       ptr != t.end();
00131       ptr++ )
00132       {
00133     // recurse
00134     s3_streamable::pack(*ptr, b);
00135       }
00136    }
00137 }
00138 
00139 // STL Container unpack
00140 template<typename T>
00141 void s3_streamable_helper_stl<T>::unpack(T& t,
00142                 const s3_pack_buffer& b)
00143 {
00144    // t.size(), t.begin(), t.end(), t.clear() should be valid for T
00145    t.clear();
00146    typename T::size_type s;
00147    s3_streamable::unpack(s, b);
00148    if ( s != 0 )
00149    {
00150       unsigned int ecnt = s;
00151       while (ecnt--) 
00152       {
00153     typename T::value_type v;
00154     // recurse
00155     s3_streamable::unpack(v, b);
00156     t.insert(t.end(), v);
00157       }
00158    }
00159 }
00160 
00161 // pack
00162 template<typename T>
00163 void s3_streamable::pack(T const &t, s3_pack_buffer &b)
00164 {
00165    s3_streamable_dispatcher<T>::pack(t, b);
00166 }
00167 
00168 // unpack
00169 template<typename T>
00170 void s3_streamable::unpack(T const &t, const s3_pack_buffer &b)
00171 {
00172    s3_streamable_dispatcher<T>::unpack(const_cast<T&>(t), b);
00173 }
00174 
00175 // POD T* pack_array
00176 template<typename T>
00177 void s3_streamable::pack_array(T* const &t, s3_pack_buffer& b, size_t n)
00178 {
00179    s3_streamable_dispatcher<T>::pack_array(t, b, n);
00180 }
00181 
00182 // POD T* unpack array
00183 template<typename T>
00184 void s3_streamable::unpack_array(T* const &t, 
00185              const s3_pack_buffer& b, 
00186              bool inplace)
00187 {
00188    s3_streamable_dispatcher<T>::unpack_array(const_cast<T*&>(t), b, inplace);
00189 }
00190 
00191 // POD const T* pack_array 
00192 template<typename T>
00193 void s3_streamable::pack_array(const T* const &t, s3_pack_buffer& b, size_t n)
00194 {
00195    s3_streamable_dispatcher<T>::pack_array(const_cast<T*>(t), b, n);
00196 }
00197 
00198 // POD const T* unpack array
00199 template<typename T>
00200 void s3_streamable::unpack_array(const T* const &t, 
00201              const s3_pack_buffer& b,
00202              bool inplace)
00203 {
00204    s3_streamable_dispatcher<T>::unpack_array(const_cast<T*&>(t), b, inplace);
00205 }
00206 
00207 #endif
00208 

Send comments to: s3fc@stonethree.com SourceForge Logo