Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# eoState.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // eoState.h
    00005 // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
    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 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     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00022              Marc.Schoenauer@polytechnique.fr
    00023              mkeijzer@dhi.dk
    00024  */
    00025 //-----------------------------------------------------------------------------
    00026
    00027 #ifndef eoState_h
    00028 #define eoState_h
    00029
    00030 #include <stdexcept>
    00031 #include <string>
    00032 #include <map>
    00033 #include <vector>
    00034
    00035 #include <eoFunctorStore.h>
    00036
    00037 class eoObject;
    00038 class eoPersistent;
    00039
    00054 class eoState : public eoFunctorStore
    00055 {
    00056 public :
    00057
    00058     eoState(void) {}
    00059
    00060     ~eoState(void);
    00061
    00065     void registerObject(eoPersistent& registrant);
    00066
    00072     template <class T>
    00073     T&   takeOwnership(const T& persistent)
    00074     {
    00075         // If the compiler budges here, T is not a subclass of eoPersistent
    00076         ownedObjects.push_back(new T(persistent));
    00077         return static_cast<T&>(*ownedObjects.back());
    00078     }
    00079
    00083     struct loading_error : public std::runtime_error
    00084     {
    00085         loading_error(std::string huh = "Error while loading") : std::runtime_error(huh) {}
    00086     };
    00087
    00088     std::string getCommentString(void) const { return "#"; }
    00089
    00095     void load(const std::string& _filename);
    00096
    00102     void load(std::istream& is);
    00103
    00109     void save(const std::string& _filename) const;
    00110
    00116     void save(std::ostream& os) const;
    00117
    00118 private :
    00119     std::string createObjectName(eoObject* obj);
    00120
    00121     // first is Persistent, second is the raw data associated with it.
    00122     typedef std::map<std::string, eoPersistent*> ObjectMap;
    00123
    00124     ObjectMap objectMap;
    00125
    00126     std::vector<ObjectMap::iterator> creationOrder;
    00127     std::vector<eoPersistent*> ownedObjects;
    00128
    00129     // private copy and assignment as eoState is supposed to be unique
    00130     eoState(const eoState&);
    00131     eoState& operator=(const eoState&);
    00132
    00133 };
    00134
    00135 #endif