git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
210 lines
11 KiB
HTML
210 lines
11 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
|
|
<title>EO: eoState.cpp Source File</title>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css">
|
|
</head><body>
|
|
<!-- Generated by Doxygen 1.3.9.1 -->
|
|
<div class="qindex"> <form class="search" action="search.php" method="get">
|
|
<a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="pages.html">Related Pages</a> | <span class="search"><u>S</u>earch for <input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
|
|
<div class="nav">
|
|
<a class="el" href="dir_000000.html">src</a> / <a class="el" href="dir_000011.html">utils</a></div>
|
|
<h1>eoState.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="preprocessor">#ifdef _MSC_VER</span>
|
|
00002 <span class="preprocessor"></span><span class="preprocessor">#pragma warning(disable:4786)</span>
|
|
00003 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
|
|
00004 <span class="preprocessor"></span>
|
|
00005 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
|
|
00006 <span class="preprocessor"></span><span class="preprocessor">#include <config.h></span>
|
|
00007 <span class="preprocessor">#endif</span>
|
|
00008 <span class="preprocessor"></span>
|
|
00009 <span class="preprocessor">#include <algorithm></span>
|
|
00010 <span class="preprocessor">#include <fstream></span>
|
|
00011 <span class="preprocessor">#include <sstream></span>
|
|
00012
|
|
00013 <span class="preprocessor">#include "eoState.h"</span>
|
|
00014 <span class="preprocessor">#include "eoObject.h"</span>
|
|
00015 <span class="preprocessor">#include "eoPersistent.h"</span>
|
|
00016
|
|
00017 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
|
00018
|
|
00019
|
|
00020
|
|
00021 <span class="keywordtype">void</span> removeComment(string& str, string comment)
|
|
00022 {
|
|
00023 string::size_type pos = str.find(comment);
|
|
00024
|
|
00025 <span class="keywordflow">if</span> (pos != string::npos)
|
|
00026 {
|
|
00027 str.erase(pos, str.size());
|
|
00028 }
|
|
00029 }
|
|
00030
|
|
00031 <span class="keywordtype">bool</span> is_section(<span class="keyword">const</span> string& str, string& name)
|
|
00032 {
|
|
00033 string::size_type pos = str.find(<span class="stringliteral">"\\section{"</span>);
|
|
00034
|
|
00035 <span class="keywordflow">if</span> (pos == string::npos)
|
|
00036 <span class="keywordflow">return</span> <span class="keyword">false</span>;
|
|
00037 <span class="comment">//else</span>
|
|
00038
|
|
00039 string::size_type end = str.find(<span class="stringliteral">"}"</span>);
|
|
00040
|
|
00041 <span class="keywordflow">if</span> (end == string::npos)
|
|
00042 <span class="keywordflow">return</span> <span class="keyword">false</span>;
|
|
00043 <span class="comment">// else</span>
|
|
00044
|
|
00045 name = str.substr(pos + 9, end-9);
|
|
00046
|
|
00047 <span class="keywordflow">return</span> <span class="keyword">true</span>;
|
|
00048 }
|
|
00049
|
|
00050 eoState::~eoState(<span class="keywordtype">void</span>)
|
|
00051 {
|
|
00052 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < ownedObjects.size(); ++i)
|
|
00053 {
|
|
00054 <span class="keyword">delete</span> ownedObjects[i];
|
|
00055 }
|
|
00056 }
|
|
00057
|
|
<a name="l00058"></a><a class="code" href="classeo_state.html#a2">00058</a> <span class="keywordtype">void</span> <a class="code" href="classeo_state.html#a2">eoState::registerObject</a>(<a class="code" href="classeo_persistent.html">eoPersistent</a>& registrant)
|
|
00059 {
|
|
00060 string name = createObjectName(dynamic_cast<eoObject*>(&registrant));
|
|
00061
|
|
00062 pair<ObjectMap::iterator,bool> res = objectMap.insert(make_pair(name, &registrant));
|
|
00063
|
|
00064 <span class="keywordflow">if</span> (res.second == <span class="keyword">true</span>)
|
|
00065 {
|
|
00066 creationOrder.push_back(res.first);
|
|
00067 }
|
|
00068 <span class="keywordflow">else</span>
|
|
00069 {
|
|
00070 <span class="keywordflow">throw</span> logic_error(<span class="stringliteral">"Interval error: object already present in the state"</span>);
|
|
00071 }
|
|
00072 }
|
|
00073
|
|
<a name="l00074"></a><a class="code" href="classeo_state.html#a5">00074</a> <span class="keywordtype">void</span> <a class="code" href="classeo_state.html#a5">eoState::load</a>(<span class="keyword">const</span> string& _filename)
|
|
00075 {
|
|
00076 ifstream is (_filename.c_str());
|
|
00077
|
|
00078 <span class="keywordflow">if</span> (!is)
|
|
00079 {
|
|
00080 string str = <span class="stringliteral">"Could not open file "</span> + _filename;
|
|
00081 <span class="keywordflow">throw</span> runtime_error(str);
|
|
00082 }
|
|
00083
|
|
00084 <a class="code" href="classeo_state.html#a5">load</a>(is);
|
|
00085 }
|
|
00086
|
|
<a name="l00087"></a><a class="code" href="classeo_state.html#a6">00087</a> <span class="keywordtype">void</span> <a class="code" href="classeo_state.html#a5">eoState::load</a>(std::istream& is)
|
|
00088 {
|
|
00089 string str;
|
|
00090 string name;
|
|
00091
|
|
00092 getline(is, str);
|
|
00093
|
|
00094 <span class="keywordflow">if</span> (is.fail())
|
|
00095 {
|
|
00096 string str = <span class="stringliteral">"Error while reading stream"</span>;
|
|
00097 <span class="keywordflow">throw</span> runtime_error(str);
|
|
00098 }
|
|
00099
|
|
00100 <span class="keywordflow">while</span>(! is.eof())
|
|
00101 { <span class="comment">// parse section header</span>
|
|
00102 <span class="keywordflow">if</span> (is_section(str, name))
|
|
00103 {
|
|
00104 string fullString;
|
|
00105 ObjectMap::iterator it = objectMap.find(name);
|
|
00106
|
|
00107 <span class="keywordflow">if</span> (it == objectMap.end())
|
|
00108 { <span class="comment">// ignore</span>
|
|
00109 <span class="keywordflow">while</span> (getline(is, str))
|
|
00110 {
|
|
00111 <span class="keywordflow">if</span> (is_section(str, name))
|
|
00112 <span class="keywordflow">break</span>;
|
|
00113 }
|
|
00114 }
|
|
00115 <span class="keywordflow">else</span>
|
|
00116 {
|
|
00117
|
|
00118 <a class="code" href="classeo_persistent.html">eoPersistent</a>* object = it->second;
|
|
00119
|
|
00120 <span class="comment">// now we have the object, get lines, remove comments etc.</span>
|
|
00121
|
|
00122 string fullstring;
|
|
00123
|
|
00124 <span class="keywordflow">while</span> (getline(is, str))
|
|
00125 {
|
|
00126 <span class="keywordflow">if</span> (is.eof())
|
|
00127 <span class="keywordflow">throw</span> runtime_error(<span class="stringliteral">"No section in load file"</span>);
|
|
00128 <span class="keywordflow">if</span> (is_section(str, name))
|
|
00129 <span class="keywordflow">break</span>;
|
|
00130
|
|
00131 removeComment(str, getCommentString());
|
|
00132 fullstring += str + <span class="stringliteral">"\n"</span>;
|
|
00133 }
|
|
00134 istringstream the_stream(fullstring);
|
|
00135 object-><a class="code" href="classeo_persistent.html#a1">readFrom</a>(the_stream);
|
|
00136 }
|
|
00137 }
|
|
00138 <span class="keywordflow">else</span> <span class="comment">// if (is_section(str, name)) - what if file empty</span>
|
|
00139 {
|
|
00140 getline(is, str); <span class="comment">// try next line!</span>
|
|
00141 <span class="comment">// if (is.eof())</span>
|
|
00142 <span class="comment">// throw runtime_error("No section in load file");</span>
|
|
00143 }
|
|
00144 }
|
|
00145
|
|
00146 }
|
|
00147
|
|
<a name="l00148"></a><a class="code" href="classeo_state.html#a7">00148</a> <span class="keywordtype">void</span> <a class="code" href="classeo_state.html#a7">eoState::save</a>(<span class="keyword">const</span> string& filename)<span class="keyword"> const</span>
|
|
00149 <span class="keyword"></span>{ <span class="comment">// saves in order of insertion</span>
|
|
00150 ofstream os(filename.c_str());
|
|
00151
|
|
00152 <span class="keywordflow">if</span> (!os)
|
|
00153 {
|
|
00154 string msg = <span class="stringliteral">"Could not open file: "</span> + filename + <span class="stringliteral">" for writing!"</span>;
|
|
00155 <span class="keywordflow">throw</span> runtime_error(msg);
|
|
00156 }
|
|
00157
|
|
00158 <a class="code" href="classeo_state.html#a7">save</a>(os);
|
|
00159 }
|
|
00160
|
|
<a name="l00161"></a><a class="code" href="classeo_state.html#a8">00161</a> <span class="keywordtype">void</span> <a class="code" href="classeo_state.html#a7">eoState::save</a>(std::ostream& os)<span class="keyword"> const</span>
|
|
00162 <span class="keyword"></span>{ <span class="comment">// saves in order of insertion</span>
|
|
00163 <span class="keywordflow">for</span> (vector<ObjectMap::iterator>::const_iterator it = creationOrder.begin(); it != creationOrder.end(); ++it)
|
|
00164 {
|
|
00165 os << <span class="stringliteral">"\\section{"</span> << (*it)->first << <span class="stringliteral">"}\n"</span>;
|
|
00166 (*it)->second-><a class="code" href="classeo_printable.html#a1">printOn</a>(os);
|
|
00167 os << <span class="charliteral">'\n'</span>;
|
|
00168 }
|
|
00169 }
|
|
00170
|
|
00171 string eoState::createObjectName(<a class="code" href="classeo_object.html">eoObject</a>* obj)
|
|
00172 {
|
|
00173 <span class="keywordflow">if</span> (obj == 0)
|
|
00174 {
|
|
00175 ostringstream os;
|
|
00176 os << objectMap.size();
|
|
00177 <span class="keywordflow">return</span> os.str();
|
|
00178 }
|
|
00179 <span class="comment">// else</span>
|
|
00180
|
|
00181 string name = obj-><a class="code" href="classeo_object.html#a1">className</a>();
|
|
00182 ObjectMap::const_iterator it = objectMap.find(name);
|
|
00183
|
|
00184 <span class="keywordtype">unsigned</span> count = 1;
|
|
00185 <span class="keywordflow">while</span> (it != objectMap.end())
|
|
00186 {
|
|
00187 ostringstream os;
|
|
00188 os << obj-><a class="code" href="classeo_object.html#a1">className</a>().c_str() << count++;
|
|
00189 name = os.str();
|
|
00190 it = objectMap.find(name);
|
|
00191 }
|
|
00192
|
|
00193 <span class="keywordflow">return</span> name;
|
|
00194 }
|
|
00195
|
|
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:38 2006 for EO by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.3.9.1 </small></address>
|
|
</body>
|
|
</html>
|