]> www.ginac.de Git - ginac.git/blob - ginac/archive.h
- added get_representation_label()
[ginac.git] / ginac / archive.h
1 /** @file archive.h
2  *
3  *  Archiving of GiNaC expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #ifndef __GINAC_ARCHIVE_H__
24 #define __GINAC_ARCHIVE_H__
25
26 #include "ex.h"
27
28 #include <string>
29 #include <vector>
30 #include <iostream>
31
32 namespace GiNaC {
33
34 class lst;
35 class archive;
36
37
38 /** Numerical ID value to refer to an archive_node. */
39 typedef unsigned int archive_node_id;
40
41 /** Numerical ID value to refer to a string. */
42 typedef unsigned int archive_atom;
43
44
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. */
48 class archive_node
49 {
50         friend std::ostream &operator<<(std::ostream &os, const archive_node &ar);
51         friend std::istream &operator>>(std::istream &is, archive_node &ar);
52
53 public:
54         /** Property data types */
55         enum property_type {
56                 PTYPE_BOOL,
57                 PTYPE_UNSIGNED,
58                 PTYPE_STRING,
59                 PTYPE_NODE
60         };
61
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 {
66                 property_info() {}
67                 property_info(property_type t, const std::string &n, unsigned c = 1) : type(t), name(n), count(c) {}
68                 ~property_info() {}
69
70                 property_info(const property_info &other) : type(other.type), name(other.name), count(other.count) {}
71                 const property_info &operator=(const property_info &other);
72
73                 property_type type;     /**< Data type of property. */
74                 std::string name;   /**< Name of property. */
75                 unsigned count;     /**< Number of occurrences. */
76         };
77         // Cint doesn't like vector<..,default_alloc> but malloc_alloc is
78         // unstandardized and not supported by newer GCCs.
79 #if defined(__GNUC__) && ((__GNUC__ == 2) && (__GNUC_MINOR__ < 97))
80         typedef std::vector<property_info,malloc_alloc> propinfovector;
81 #else
82         typedef std::vector<property_info> propinfovector;
83 #endif
84
85         archive_node() : a(*dummy_ar_creator()), has_expression(false) {} // hack for cint which always requires a default constructor
86         archive_node(archive &ar) : a(ar), has_expression(false) {}
87         archive_node(archive &ar, const ex &expr);
88         ~archive_node() {}
89
90         archive_node(const archive_node &other);
91         const archive_node &operator=(const archive_node &other);
92
93         /** Add property of type "bool" to node. */
94         void add_bool(const std::string &name, bool value);
95
96         /** Add property of type "unsigned int" to node. */
97         void add_unsigned(const std::string &name, unsigned int value);
98
99         /** Add property of type "string" to node. */
100         void add_string(const std::string &name, const std::string &value);
101
102         /** Add property of type "ex" to node. */
103         void add_ex(const std::string &name, const ex &value);
104
105         /** Retrieve property of type "bool" from node.
106          *  @return "true" if property was found, "false" otherwise */
107         bool find_bool(const std::string &name, bool &ret) const;
108
109         /** Retrieve property of type "unsigned" from node.
110          *  @return "true" if property was found, "false" otherwise */
111         bool find_unsigned(const std::string &name, unsigned int &ret) const;
112
113         /** Retrieve property of type "string" from node.
114          *  @return "true" if property was found, "false" otherwise */
115         bool find_string(const std::string &name, std::string &ret) const;
116
117         /** Retrieve property of type "ex" from node.
118          *  @return "true" if property was found, "false" otherwise */
119         bool find_ex(const std::string &name, ex &ret, const lst &sym_lst, unsigned int index = 0) const;
120
121         /** Retrieve property of type "ex" from node, returning the node of
122          *  the sub-expression. */
123         const archive_node &find_ex_node(const std::string &name, unsigned int index = 0) const;
124
125         /** Return vector of properties stored in node. */
126         void get_properties(propinfovector &v) const;
127
128         ex unarchive(const lst &sym_lst) const;
129         bool has_same_ex_as(const archive_node &other) const;
130
131         void forget(void);
132         void printraw(std::ostream &os) const;
133
134 private:
135         static archive* dummy_ar_creator(void);
136
137         /** Archived property (data type, name and associated data) */
138         struct property {
139                 property() {}
140                 property(archive_atom n, property_type t, unsigned int v) : type(t), name(n), value(v) {}
141                 ~property() {}
142
143                 property(const property &other) : type(other.type), name(other.name), value(other.value) {}
144                 const property &operator=(const property &other);
145
146                 property_type type; /**< Data type of property. */
147                 archive_atom name;  /**< Name of property. */
148                 unsigned int value; /**< Stored value. */
149         };
150
151         /** Reference to the archive to which this node belongs. */
152         archive &a;
153
154         /** Vector of stored properties. */
155         std::vector<property> props;
156
157         /** Flag indicating whether a cached unarchived representation of this node exists. */
158         mutable bool has_expression;
159
160         /** The cached unarchived representation of this node (if any). */
161         mutable ex e;
162 };
163
164
165 /** This class holds archived versions of GiNaC expressions (class ex).
166  *  An archive can be constructed from an expression and then written to
167  *  a stream; or it can be read from a stream and then unarchived, yielding
168  *  back the expression. Archives can hold multiple expressions which can
169  *  be referred to by name or index number. The main component of the
170  *  archive class is a vector of archive_nodes which each store one object
171  *  of class basic (or a derived class). */
172 class archive
173 {
174         friend std::ostream &operator<<(std::ostream &os, const archive &ar);
175         friend std::istream &operator>>(std::istream &is, archive &ar);
176
177 public:
178         archive() {}
179         ~archive() {}
180
181         /** Construct archive from expression using the default name "ex". */
182         archive(const ex &e) {archive_ex(e, "ex");}
183
184         /** Construct archive from expression using the specified name. */
185         archive(const ex &e, const char *n) {archive_ex(e, n);}
186
187         /** Archive an expression.
188          *  @param e the expression to be archived
189          *  @param name name under which the expression is stored */
190         void archive_ex(const ex &e, const char *name);
191
192         /** Retrieve expression from archive by name.
193          *  @param sym_lst list of pre-defined symbols */
194         ex unarchive_ex(const lst &sym_lst, const char *name) const;
195
196         /** Retrieve expression from archive by index.
197          *  @param sym_lst list of pre-defined symbols
198      *  @see count_expressions */
199         ex unarchive_ex(const lst &sym_lst, unsigned int index = 0) const;
200
201         /** Retrieve expression and its name from archive by index.
202          *  @param sym_lst list of pre-defined symbols
203          *  @param name receives the name of the expression
204      *  @see count_expressions */
205         ex unarchive_ex(const lst &sym_lst, std::string &name, unsigned int index = 0) const;
206
207         /** Return number of archived expressions. */
208         unsigned int num_expressions(void) const;
209
210         /** Return reference to top node of an expression specified by index. */
211         const archive_node &get_top_node(unsigned int index = 0) const;
212
213         /** Clear all archived expressions. */
214         void clear(void);
215
216         archive_node_id add_node(const archive_node &n);
217         archive_node &get_node(archive_node_id id);
218
219         void forget(void);
220         void printraw(std::ostream &os) const;
221
222 private:
223         /** Vector of archived nodes. */
224         std::vector<archive_node> nodes;
225
226         /** Archived expression descriptor. */
227         struct archived_ex {
228                 archived_ex() {}
229                 archived_ex(archive_atom n, archive_node_id node) : name(n), root(node) {}
230
231                 archive_atom name;              /**< Name of expression. */
232                 archive_node_id root;   /**< ID of root node. */
233         };
234
235         /** Vector of archived expression descriptors. */
236         std::vector<archived_ex> exprs;
237
238 public:
239         archive_atom atomize(const std::string &s) const;
240         const std::string &unatomize(archive_atom id) const;
241
242 private:
243         /** Vector of atomized strings (using a vector allows faster unarchiving). */
244         mutable std::vector<std::string> atoms;
245 };
246
247
248 std::ostream &operator<<(std::ostream &os, const archive &ar);
249 std::istream &operator>>(std::istream &is, archive &ar);
250
251
252 } // namespace GiNaC
253
254 #endif // ndef __GINAC_ARCHIVE_H__