]> www.ginac.de Git - ginac.git/blob - ginac/archive.h
#pragmas for MAKECINT in combination with namespaces
[ginac.git] / ginac / archive.h
1 /** @file archive.h
2  *
3  *  Archiving of GiNaC expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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
31 class ostream;
32 class istream;
33
34 #ifndef NO_NAMESPACE_GINAC
35 namespace GiNaC {
36 #endif // ndef NO_NAMESPACE_GINAC
37
38 class lst;
39 class archive;
40
41
42 /** Numerical ID value to refer to an archive_node. */
43 typedef unsigned int archive_node_id;
44
45 /** Numerical ID value to refer to a string. */
46 typedef unsigned int archive_atom;
47
48
49 /** This class stores all properties needed to record/retrieve the state
50  *  of one object of class basic (or a derived class). Each property is
51  *  addressed by its name and data type. */
52 class archive_node
53 {
54         friend ostream &operator<<(ostream &os, const archive_node &ar);
55         friend istream &operator>>(istream &is, archive_node &ar);
56
57 public:
58         archive_node() : a(*dummy_ar_creator()), has_expression(false) {} // hack for cint which always requires a default constructor
59         archive_node(archive &ar) : a(ar), has_expression(false) {}
60         archive_node(archive &ar, const ex &expr);
61         ~archive_node() {}
62
63         archive_node(const archive_node &other);
64         const archive_node &operator=(const archive_node &other);
65
66         bool has_same_ex_as(const archive_node &other) const;
67
68         void add_bool(const string &name, bool value);
69         void add_unsigned(const string &name, unsigned int value);
70         void add_string(const string &name, const string &value);
71         void add_ex(const string &name, const ex &value);
72
73         bool find_bool(const string &name, bool &ret) const;
74         bool find_unsigned(const string &name, unsigned int &ret) const;
75         bool find_string(const string &name, string &ret) const;
76         bool find_ex(const string &name, ex &ret, const lst &sym_lst, unsigned int index = 0) const;
77
78         ex unarchive(const lst &sym_lst) const;
79
80         void forget(void);
81         void printraw(ostream &os) const;
82
83 private:
84         static archive* dummy_ar_creator(void);
85
86         /** Property data types */
87         enum property_type {
88                 PTYPE_BOOL,
89                 PTYPE_UNSIGNED,
90                 PTYPE_STRING,
91                 PTYPE_NODE
92         };
93
94         /** Archived property (data type, name and associated data) */
95         struct property {
96                 property() {}
97                 property(archive_atom n, property_type t, unsigned int v) : type(t), name(n), value(v) {}
98                 ~property() {}
99
100                 property(const property &other) : type(other.type), name(other.name), value(other.value) {}
101                 const property &operator=(const property &other);
102
103                 property_type type;     /**< Data type of property. */
104                 archive_atom name;      /**< Name of property. */
105                 unsigned int value;     /**< Stored value. */
106         };
107
108         /** Reference to the archive to which this node belongs. */
109         archive &a;
110
111         /** Vector of stored properties. */
112         vector<property> props;
113
114         /** Flag indicating whether a cached unarchived representation of this node exists. */
115         mutable bool has_expression;
116
117         /** The cached unarchived representation of this node (if any). */
118         mutable ex e;
119 };
120
121
122 /** This class holds archived versions of GiNaC expressions (class ex).
123  *  An archive can be constructed from an expression and then written to
124  *  a stream; or it can be read from a stream and then unarchived, yielding
125  *  back the expression. Archives can hold multiple expressions which can
126  *  be referred to by name or index number. The main component of the
127  *  archive class is a vector of archive_nodes which each store one object
128  *  of class basic (or a derived class). */
129 class archive
130 {
131         friend ostream &operator<<(ostream &os, const archive &ar);
132         friend istream &operator>>(istream &is, archive &ar);
133
134 public:
135         archive() {}
136         ~archive() {}
137
138         /** Construct archive from expression using the default name "ex". */
139         archive(const ex &e) {archive_ex(e, "ex");}
140
141         /** Construct archive from expression using the specified name. */
142         archive(const ex &e, const char *n) {archive_ex(e, n);}
143
144         archive_node_id add_node(const archive_node &n);
145         archive_node &get_node(archive_node_id id);
146
147         void archive_ex(const ex &e, const char *name);
148         ex unarchive_ex(const lst &sym_lst, const char *name) const;
149         ex unarchive_ex(const lst &sym_lst, unsigned int index = 0) const;
150         ex unarchive_ex(const lst &sym_lst, string &name, unsigned int index = 0) const;
151         unsigned int num_expressions(void) const;
152
153         void clear(void);
154
155         void forget(void);
156         void printraw(ostream &os) const;
157
158 private:
159         /** Vector of archived nodes. */
160         vector<archive_node> nodes;
161
162         /** Archived expression descriptor. */
163         struct archived_ex {
164                 archived_ex() {}
165                 archived_ex(archive_atom n, archive_node_id node) : name(n), root(node) {}
166
167                 archive_atom name;              /**< Name of expression. */
168                 archive_node_id root;   /**< ID of root node. */
169         };
170
171         /** Vector of archived expression descriptors. */
172         vector<archived_ex> exprs;
173
174 public:
175         archive_atom atomize(const string &s) const;
176         const string &unatomize(archive_atom id) const;
177
178 private:
179         /** Vector of atomized strings (using a vector allows faster unarchiving). */
180         mutable vector<string> atoms;
181 };
182
183
184 ostream &operator<<(ostream &os, const archive &ar);
185 istream &operator>>(istream &is, archive &ar);
186
187
188 #ifndef NO_NAMESPACE_GINAC
189 } // namespace GiNaC
190 #endif // ndef NO_NAMESPACE_GINAC
191
192 #endif // ndef __GINAC_ARCHIVE_H__