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