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