EvolvingObjects
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 #include <assert.h>
00035 
00036 #include <eoFunctorStore.h>
00037 
00038 class eoObject;
00039 class eoPersistent;
00040 
00056 class eoState : public eoFunctorStore
00057 {
00058 public :
00059 
00060     eoState(std::string name="") :
00061       _tag_state_so(""),
00062       _tag_state_name(name),
00063       _tag_state_sc(""),
00064       _tag_section_so("\\section{"),
00065       _tag_section_sc("}\n"),
00066       _tag_content_s(""),
00067       _tag_content_e(""),
00068       _tag_section_sep(""),
00069       _tag_section_e("\n"),
00070       _tag_state_e("")
00071     {}
00072 
00073     ~eoState(void);
00074 
00075     void formatLatex(std::string name)
00076     {
00077         _tag_state_so = "";
00078         _tag_state_name = name;
00079         _tag_state_sc = "";
00080         _tag_section_so = "\\section{";
00081         _tag_section_sc = "}\n";
00082         _tag_content_s = "";
00083         _tag_content_e = "";
00084         _tag_section_sep = "";
00085         _tag_section_e = "\n";
00086         _tag_state_e = "";
00087     }
00088 
00089     void formatJSON(std::string name)
00090     {
00091         _tag_state_so = "{ \"";
00092         _tag_state_name = name;
00093         _tag_state_sc = "\":\n";
00094         _tag_section_so = "\t{ \"";
00095         _tag_section_sc = "\":\n";
00096         _tag_content_s = "\"";
00097         _tag_content_e = "\"";
00098         _tag_section_sep = ",\n";
00099         _tag_section_e = "\t}\n";
00100         _tag_state_e = "}\n";
00101     }
00102 
00103 
00107     void registerObject(eoPersistent& registrant);
00108 
00114     template <class T>
00115     T&   takeOwnership(const T& persistent)
00116     {
00117         // If the compiler budges here, T is not a subclass of eoPersistent
00118         ownedObjects.push_back(new T(persistent));
00119         return static_cast<T&>(*ownedObjects.back());
00120     }
00121 
00125     struct loading_error : public std::runtime_error
00126     {
00127         loading_error(std::string huh = "Error while loading") : std::runtime_error(huh) {}
00128     };
00129 
00130     std::string getCommentString(void) const { return "#"; }
00131 
00137     void load(const std::string& _filename);
00138 
00144     void load(std::istream& is);
00145 
00151     void save(const std::string& _filename) const;
00152 
00158     void save(std::ostream& os) const;
00159 
00160 private :
00161     std::string createObjectName(eoObject* obj);
00162 
00163     // first is Persistent, second is the raw data associated with it.
00164     typedef std::map<std::string, eoPersistent*> ObjectMap;
00165 
00166     ObjectMap objectMap;
00167 
00168     std::vector<ObjectMap::iterator> creationOrder;
00169     std::vector<eoPersistent*> ownedObjects;
00170 
00171     // private copy and assignment as eoState is supposed to be unique
00172     eoState(const eoState&);
00173     eoState& operator=(const eoState&);
00174 
00175     /* \@{
00176      * s=start, e=end
00177      * o=open, c=close
00178      *
00179      * { "my_state": 
00180      *     {
00181      *         "section_pop":"…",
00182      *         "section_rng":"…"
00183      *     }
00184      *  }
00185      *
00186      *                           // JSON         LATEX (default)
00187      */
00188     std::string _tag_state_so;   // { "          
00189     std::string _tag_state_name; // my_state     
00190     std::string _tag_state_sc;   // ":           
00191 
00192     std::string _tag_section_so; // { "          \\section{
00193     std::string _tag_section_sc; // ":           }\n
00194 
00195     std::string _tag_content_s; // "
00196     std::string _tag_content_e; // "
00197 
00198     std::string _tag_section_sep;// ,
00199 
00200     std::string _tag_section_e; // }           \n
00201 
00202     std::string _tag_state_e;   // }
00205     void removeComment( std::string& str, std::string comment);
00206 
00207     bool is_section(const std::string& str, std::string& name);
00208 
00209 protected:
00210     void saveSection( std::ostream& os, std::vector<ObjectMap::iterator>::const_iterator it) const;
00211 
00212 };
00216 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends