3 * Archiving of GiNaC expressions. */
6 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __GINAC_ARCHIVE_H__
24 #define __GINAC_ARCHIVE_H__
38 /** Numerical ID value to refer to an archive_node. */
39 typedef unsigned archive_node_id;
41 /** Numerical ID value to refer to a string. */
42 typedef unsigned archive_atom;
45 /** This class stores all properties needed to record/retrieve the state
46 * of one object of class basic (or a derived class). Each property is
47 * addressed by its name and data type. */
50 friend std::ostream &operator<<(std::ostream &os, const archive_node &ar);
51 friend std::istream &operator>>(std::istream &is, archive_node &ar);
54 /** Property data types */
62 /** Information about a stored property. A vector of these structures
63 * is returned by get_properties().
64 * @see get_properties */
65 struct property_info {
67 property_info(property_type t, const std::string &n, unsigned c = 1) : type(t), name(n), count(c) {}
69 property_type type; /**< Data type of property. */
70 std::string name; /**< Name of property. */
71 unsigned count; /**< Number of occurrences. */
73 typedef std::vector<property_info> propinfovector;
75 archive_node() : a(*dummy_ar_creator()), has_expression(false) {} // hack for cint which always requires a default constructor
76 archive_node(archive &ar) : a(ar), has_expression(false) {}
77 archive_node(archive &ar, const ex &expr);
79 const archive_node &operator=(const archive_node &other);
81 /** Add property of type "bool" to node. */
82 void add_bool(const std::string &name, bool value);
84 /** Add property of type "unsigned int" to node. */
85 void add_unsigned(const std::string &name, unsigned value);
87 /** Add property of type "string" to node. */
88 void add_string(const std::string &name, const std::string &value);
90 /** Add property of type "ex" to node. */
91 void add_ex(const std::string &name, const ex &value);
93 /** Retrieve property of type "bool" from node.
94 * @return "true" if property was found, "false" otherwise */
95 bool find_bool(const std::string &name, bool &ret, unsigned index = 0) const;
97 /** Retrieve property of type "unsigned" from node.
98 * @return "true" if property was found, "false" otherwise */
99 bool find_unsigned(const std::string &name, unsigned &ret, unsigned index = 0) const;
101 /** Retrieve property of type "string" from node.
102 * @return "true" if property was found, "false" otherwise */
103 bool find_string(const std::string &name, std::string &ret, unsigned index = 0) const;
105 /** Retrieve property of type "ex" from node.
106 * @return "true" if property was found, "false" otherwise */
107 bool find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index = 0) const;
109 /** Retrieve property of type "ex" from node, returning the node of
110 * the sub-expression. */
111 const archive_node &find_ex_node(const std::string &name, unsigned index = 0) const;
113 /** Return vector of properties stored in node. */
114 void get_properties(propinfovector &v) const;
116 ex unarchive(lst &sym_lst) const;
117 bool has_same_ex_as(const archive_node &other) const;
118 bool has_ex() const {return has_expression;}
119 ex get_ex() const {return e;}
122 void printraw(std::ostream &os) const;
125 static archive* dummy_ar_creator();
127 /** Archived property (data type, name and associated data) */
130 property(archive_atom n, property_type t, unsigned v) : type(t), name(n), value(v) {}
132 property_type type; /**< Data type of property. */
133 archive_atom name; /**< Name of property. */
134 unsigned value; /**< Stored value. */
137 /** Reference to the archive to which this node belongs. */
140 /** Vector of stored properties. */
141 std::vector<property> props;
143 /** Flag indicating whether a cached unarchived representation of this node exists. */
144 mutable bool has_expression;
146 /** The cached unarchived representation of this node (if any). */
151 /** This class holds archived versions of GiNaC expressions (class ex).
152 * An archive can be constructed from an expression and then written to
153 * a stream; or it can be read from a stream and then unarchived, yielding
154 * back the expression. Archives can hold multiple expressions which can
155 * be referred to by name or index number. The main component of the
156 * archive class is a vector of archive_nodes which each store one object
157 * of class basic (or a derived class). */
160 friend std::ostream &operator<<(std::ostream &os, const archive &ar);
161 friend std::istream &operator>>(std::istream &is, archive &ar);
167 /** Construct archive from expression using the default name "ex". */
168 archive(const ex &e) {archive_ex(e, "ex");}
170 /** Construct archive from expression using the specified name. */
171 archive(const ex &e, const char *n) {archive_ex(e, n);}
173 /** Archive an expression.
174 * @param e the expression to be archived
175 * @param name name under which the expression is stored */
176 void archive_ex(const ex &e, const char *name);
178 /** Retrieve expression from archive by name.
179 * @param sym_lst list of pre-defined symbols
180 * @param name name of expression */
181 ex unarchive_ex(const lst &sym_lst, const char *name) const;
183 /** Retrieve expression from archive by index.
184 * @param sym_lst list of pre-defined symbols
185 * @param index index of expression
186 * @see count_expressions */
187 ex unarchive_ex(const lst &sym_lst, unsigned index = 0) const;
189 /** Retrieve expression and its name from archive by index.
190 * @param sym_lst list of pre-defined symbols
191 * @param name receives the name of the expression
192 * @param index index of expression
193 * @see count_expressions */
194 ex unarchive_ex(const lst &sym_lst, std::string &name, unsigned index = 0) const;
196 /** Return number of archived expressions. */
197 unsigned num_expressions() const;
199 /** Return reference to top node of an expression specified by index. */
200 const archive_node &get_top_node(unsigned index = 0) const;
202 /** Clear all archived expressions. */
205 archive_node_id add_node(const archive_node &n);
206 archive_node &get_node(archive_node_id id);
209 void printraw(std::ostream &os) const;
212 /** Vector of archived nodes. */
213 std::vector<archive_node> nodes;
215 /** Archived expression descriptor. */
218 archived_ex(archive_atom n, archive_node_id node) : name(n), root(node) {}
220 archive_atom name; /**< Name of expression. */
221 archive_node_id root; /**< ID of root node. */
224 /** Vector of archived expression descriptors. */
225 std::vector<archived_ex> exprs;
228 archive_atom atomize(const std::string &s) const;
229 const std::string &unatomize(archive_atom id) const;
232 /** Vector of atomized strings (using a vector allows faster unarchiving). */
233 mutable std::vector<std::string> atoms;
235 /** Map of stored expressions to nodes for faster archiving */
236 typedef std::map<ex, archive_node_id, ex_is_less>::iterator mapit;
237 mutable std::map<ex, archive_node_id, ex_is_less> exprtable;
241 std::ostream &operator<<(std::ostream &os, const archive &ar);
242 std::istream &operator>>(std::istream &is, archive &ar);
247 #endif // ndef __GINAC_ARCHIVE_H__