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

iarchive.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 __i_archive_hpp
00021 #define __i_archive_hpp
00022 
00023 #include "archive.hpp"
00024 
00025 #include <mynahsa/spimpl.hpp>
00026 
00027 
00028 #include <string>
00029 
00030 #include <map>
00031 
00032 #include <stdexcept>
00033 
00034 
00035 #include <iostream>
00036 
00037 namespace MynahSA {
00038   
00043   template<class T>
00044   class IArchive : public Archive { 
00045   public:
00046   
00047 
00048   
00050     IArchive(T& s) : _stream(s) {
00051     }
00052       
00054     IArchive(const IArchive<T>& i) : Archive(i), 
00055                                      _stream(i._stream) {
00056     }
00057   
00058   
00060     ~IArchive() {
00061     }
00062   
00063   
00064 
00065   
00066     //  Implementation of pure base follows
00067   
00068 
00069     
00070     // must treat int / bool / float / double / char
00071   
00073     virtual Archive& operator&(long long& j) { 
00074       _stream >> j;
00075       return *this;
00076     }
00077     
00079     virtual Archive& operator&(unsigned long long& j) { 
00080       _stream >> j;
00081       return *this;
00082     }
00083   
00085     virtual Archive& operator&(unsigned int& i) {
00086       _stream >> i;
00087       return *this;
00088     }
00089   
00091     virtual Archive& operator&(int& i) { 
00092       _stream >> i;
00093       return *this;
00094     }
00095     
00097     virtual Archive& operator&(short& s) { 
00098       _stream >> s;
00099       return *this;
00100     }
00101     
00103     virtual Archive& operator&(unsigned short& s) { 
00104       _stream >> s;
00105       return *this;
00106     }
00107     
00109     virtual Archive& operator&(char& c) { 
00110       _stream >> c;
00111       return *this;
00112     }
00113     
00115     virtual Archive& operator&(unsigned char& c) {
00116       _stream >> c;
00117       return *this;
00118     }
00119     
00121     virtual Archive& operator&(bool& b) { 
00122       _stream >> b;
00123       return *this;
00124     }
00125     
00127     virtual Archive& operator&(float& f) {
00128       _stream >> f;
00129       return *this;
00130     }
00131     
00133     virtual Archive& operator&(double& d) {
00134       _stream >> d;
00135       return *this;
00136     }
00137   
00138   
00139   
00141     virtual Archive& operator&(std::string& s) { 
00142       // note: stream operator defines behavior for string streaming.  If the std library streams
00143       // are used the string s must note contain a space, since the std streams split strings by space
00144       _stream >> s;
00145       return *this;
00146     }
00147   
00148     template <class Type>
00149     Archive& operator>>(SHARED_PTR<Type>& instance) { 
00150     std::string name; // get the class name
00151     (*this) & instance;
00152     return *this;
00153     }
00154     
00155     template <class Type>
00156     Archive& operator>>(Type& instance) { 
00157       instance.serialize(*this);
00158       return *this;
00159     }
00160   
00161     
00162 
00163   
00165     using Archive::operator&;
00166   
00167   
00168     // IArchive must provide STL wrappers for operator>> - we can simply use Archive::operator& to do the work, but we must provide the function
00169     // override s.t. the compiler will have a method for implementing the unidirectional stream operator
00170     template<class Type>
00171     Archive& operator>>(std::list<Type>& l) { 
00172       (*this) & l;
00173       return *this;
00174     }
00175     
00176     template<class Type>
00177     Archive& operator>>(std::vector<Type>& v) { 
00178       (*this) & v;
00179       return *this;
00180     }
00181     
00182     template<class A, class B>
00183     Archive& operator>>(std::pair<A,B>& p) { 
00184       (*this) & p;
00185       return *this;
00186     }
00187     
00188     template<class index_type, class value_type>
00189     Archive& operator>>(std::map<index_type, value_type>& m) { 
00190       (*this) & m;
00191       return *this;
00192     }
00193     
00194     template<class index_type, class value_type>
00195     Archive& operator>>(std::multimap<index_type, value_type>& m) { 
00196       (*this) & m;
00197       return *this;
00198     }
00199   
00200     template<class value_type>
00201     Archive& operator>>(std::set<value_type>& s) { 
00202       (*this) & s;
00203       return *this;
00204     }
00205   
00206   
00207   
00208   
00209   
00210   
00211   
00212   
00213   protected:
00215     ArchiveMode getArchiveMode() const { 
00216       return Archive::ARCHIVE_READ;
00217     }
00218   
00219   
00220   private:
00221 
00222   
00223     T& _stream;
00224   
00225   };
00226 };
00227 
00228 
00229 #endif