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