Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

oarchive.hpp

00001 /************************************************************************************
00002  *    This file is part of the MynahSA streaming and archiving toolkit              *
00003  *    Copyright (C) 2006 Mynah-Software Ltd. All Rights Reserved.                   *
00004  *                                                                                  *
00005  *    This program is free software; you can redistribute it and/or modify          *
00006  *    it under the terms of the GNU General Public License, version 2               *
00007  *    as published by the Free Software Foundation.                                 *
00008  *                                                                                  *
00009  *    This program 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                 *
00012  *    GNU General Public License for more details.                                  *
00013  *                                                                                  *
00014  *    You should have received a copy of the GNU General Public License along       *
00015  *    with this program; if not, write to the Free Software Foundation, Inc.,       *
00016  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                   *
00017  *                                                                                  *
00018  ************************************************************************************/
00019 
00020 #ifndef __o_archive_hpp
00021 #define __o_archive_hpp
00022 
00023 #include <mynahsa/spimpl.hpp>
00024 
00025 #include <string>
00026 
00027 #include "archive.hpp"
00028 #include "iobase.hpp"
00029 
00030 namespace MynahSA { 
00031 
00032   template<class T>
00033   class OArchive : public Archive { 
00034   public:
00036     OArchive(T& s) : _stream(s) { 
00037     }
00038   
00039     OArchive(const OArchive<T>& i) : Archive(i), 
00040                                      _stream(i._stream) {
00041     }
00042   
00044     ~OArchive() { 
00045     }
00046  
00047   
00048   
00049     // must treat int / bool / float / double / char
00050     
00052     virtual Archive& operator&(long long& j) { 
00053       _stream << j; 
00054       return *this;
00055     }
00056     
00058     virtual Archive& operator&(unsigned long long& j) { 
00059       _stream << j;
00060       return *this;
00061     }
00062   
00063   
00065     virtual Archive& operator&(unsigned int& i) {
00066       _stream << i;
00067       return *this;
00068     }
00069   
00071     virtual Archive& operator&(int& i) { 
00072       _stream << i;
00073       return *this;
00074     }
00075   
00077     virtual Archive& operator&(short& s) { 
00078       _stream << s;
00079       return *this;
00080     }
00081     
00083     virtual Archive& operator&(unsigned short& s) { 
00084       _stream << s;
00085       return *this;
00086     }
00087   
00088   
00090     virtual Archive& operator&(bool& b) {
00091       _stream << b;
00092       return *this;
00093     }
00094     
00096     virtual Archive& operator&(float& f) { 
00097       _stream << f;
00098       return *this;
00099     }
00100     
00102     virtual Archive& operator&(double& d) {
00103       _stream << d;
00104       return *this;
00105     }
00106   
00108     virtual Archive& operator&(char& c) { 
00109       _stream << c;
00110       return *this;
00111     }
00112   
00114     virtual Archive& operator&(unsigned char& c) { 
00115       _stream << c;
00116       return *this;
00117     }
00118   
00119   
00121     virtual Archive& operator&(std::string& s) { 
00122       _stream << s;
00123       return *this;
00124     }
00125   
00126     template <class Type>
00127     Archive& operator<<(SHARED_PTR<Type>& instance) { 
00128       (*this) & instance;
00129       return *this;
00130     }
00131     
00132     template <class Type>
00133     Archive& operator<<(Type& instance) { 
00134       instance.serialize(*this);
00135       return *this;
00136     }
00137     
00138     void flush() { 
00139       _stream.flush();
00140     }
00141   
00143     using Archive::operator&;
00144   
00145   
00146     // OArchive must provide STL wrappers for operator<< - we can simply use Archive::operator& to do the work, but we must provide the function
00147     // override s.t. the compiler will have a method for implementing the unidirectional stream operator
00148     template<class Type>
00149     Archive& operator<<(std::list<Type>& l) { 
00150       (*this) & l;
00151       return *this;
00152     }
00153     
00154     template<class Type>
00155     Archive& operator<<(std::vector<Type>& v) { 
00156       (*this) & v;
00157       return *this;
00158     }
00159     
00160     template<class A, class B>
00161     Archive& operator<<(std::pair<A,B>& p) { 
00162       (*this) & p;
00163       return *this;
00164     }
00165     
00166     template<class index_type, class value_type>
00167     Archive& operator<<(std::map<index_type, value_type>& m) { 
00168       (*this) & m;
00169       return *this;
00170     }
00171     
00172     template<class index_type, class value_type>
00173     Archive& operator<<(std::multimap<index_type, value_type>& m) { 
00174       (*this) & m;
00175       return *this;
00176     }  
00177 
00178     template<class value_type>
00179     Archive& operator<<(std::set<value_type>& s) { 
00180       (*this) & s;
00181       return *this;
00182     }
00183 
00184   protected:
00186     ArchiveMode getArchiveMode() const { 
00187       return Archive::ARCHIVE_WRITE;
00188     }
00189   
00190   
00191   private:
00192     T& _stream;
00193   
00194   };
00195 };
00196 
00197 #endif