GiNaC 1.8.7
archive.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2023 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef GINAC_ARCHIVE_H
24#define GINAC_ARCHIVE_H
25
26#include "ex.h"
27
28#include <iosfwd>
29#include <map>
30#include <string>
31#include <vector>
32
33namespace GiNaC {
34
35class archive;
36
37
39typedef unsigned archive_node_id;
40
42typedef unsigned archive_atom;
43
44
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
53public:
60 };
61
67 property_info(property_type t, const std::string &n, unsigned c = 1) : type(t), name(n), count(c) {}
68
70 std::string name;
71 unsigned count;
72 };
73 typedef std::vector<property_info> propinfovector;
74
76 struct property {
78 property(archive_atom n, property_type t, unsigned v) : type(t), name(n), value(v) {}
79
82 unsigned value;
83 };
84 typedef std::vector<property>::const_iterator archive_node_cit;
87 };
88
89 archive_node(archive &ar) : a(ar), has_expression(false) {}
90 archive_node(archive &ar, const ex &expr);
91
92 const archive_node &operator=(const archive_node &other);
93
95 void add_bool(const std::string &name, bool value);
96
98 void add_unsigned(const std::string &name, unsigned value);
99
101 void add_string(const std::string &name, const std::string &value);
102
104 void add_ex(const std::string &name, const ex &value);
105
108 bool find_bool(const std::string &name, bool &ret, unsigned index = 0) const;
109
112 bool find_unsigned(const std::string &name, unsigned &ret, unsigned index = 0) const;
113
116 bool find_string(const std::string &name, std::string &ret, unsigned index = 0) const;
117
120 archive_node_cit find_first(const std::string &name) const;
121 archive_node_cit find_last(const std::string &name) const;
122
126 archive_node_cit_range find_property_range(const std::string &name1, const std::string &name2) const;
127
130 bool find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index = 0) const;
131
135 void find_ex_by_loc(archive_node_cit loc, ex &ret, lst &sym_lst) const;
136
139 const archive_node &find_ex_node(const std::string &name, unsigned index = 0) const;
140
142 void get_properties(propinfovector &v) const;
143
144 ex unarchive(lst &sym_lst) const;
145 bool has_same_ex_as(const archive_node &other) const;
146 bool has_ex() const {return has_expression;}
147 ex get_ex() const {return e;}
148
149 void forget();
150 void printraw(std::ostream &os) const;
151
152private:
155
157 std::vector<property> props;
158
160 mutable bool has_expression;
161
163 mutable ex e;
164};
165
166typedef basic* (*synthesize_func)();
167typedef std::map<std::string, synthesize_func> unarchive_map_t;
168
170{
171 static int usecount;
173public:
176 synthesize_func find(const std::string& classname) const;
177 void insert(const std::string& classname, synthesize_func f);
178};
180
219#define GINAC_DECLARE_UNARCHIVER(classname) \
220class classname ## _unarchiver \
221{ \
222 static int usecount; \
223public: \
224 static GiNaC::basic* create(); \
225 classname ## _unarchiver(); \
226 ~ classname ## _unarchiver(); \
227}; \
228static classname ## _unarchiver classname ## _unarchiver_instance
229
230#define GINAC_BIND_UNARCHIVER(classname) \
231classname ## _unarchiver::classname ## _unarchiver() \
232{ \
233 static GiNaC::unarchive_table_t table; \
234 if (usecount++ == 0) { \
235 table.insert(std::string(#classname), \
236 &(classname ## _unarchiver::create)); \
237 } \
238} \
239GiNaC::basic* classname ## _unarchiver::create() \
240{ \
241 return new classname(); \
242} \
243classname ## _unarchiver::~ classname ## _unarchiver() { } \
244int classname ## _unarchiver::usecount = 0
245
246
255{
256 friend std::ostream &operator<<(std::ostream &os, const archive &ar);
257 friend std::istream &operator>>(std::istream &is, archive &ar);
258
259public:
262
264 archive(const ex &e) {archive_ex(e, "ex");}
265
267 archive(const ex &e, const char *n) {archive_ex(e, n);}
268
272 void archive_ex(const ex &e, const char *name);
273
277 ex unarchive_ex(const lst &sym_lst, const char *name) const;
278
283 ex unarchive_ex(const lst &sym_lst, unsigned index = 0) const;
284
290 ex unarchive_ex(const lst &sym_lst, std::string &name, unsigned index = 0) const;
291
293 unsigned num_expressions() const;
294
296 const archive_node &get_top_node(unsigned index = 0) const;
297
299 void clear();
300
303
304 void forget();
305 void printraw(std::ostream &os) const;
306
307private:
309 std::vector<archive_node> nodes;
310
312 struct archived_ex {
315
318 };
319
321 std::vector<archived_ex> exprs;
322
323public:
324 archive_atom atomize(const std::string &s) const;
325 const std::string &unatomize(archive_atom id) const;
326
327private:
329 mutable std::vector<std::string> atoms;
333 typedef std::map<std::string, archive_atom>::const_iterator inv_at_cit;
334 mutable std::map<std::string, archive_atom> inverse_atoms;
335
337 mutable std::map<ex, archive_node_id, ex_is_less> exprtable;
338};
339
340
341std::ostream &operator<<(std::ostream &os, const archive &ar);
342std::istream &operator>>(std::istream &is, archive &ar);
343
344} // namespace GiNaC
345
346#endif // ndef GINAC_ARCHIVE_H
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition: archive.h:49
void printraw(std::ostream &os) const
Output archive_node to stream in ugly raw format (for debugging).
Definition: archive.cpp:664
std::vector< property > props
Vector of stored properties.
Definition: archive.h:157
property_type
Property data types.
Definition: archive.h:55
void get_properties(propinfovector &v) const
Return vector of properties stored in node.
Definition: archive.cpp:512
bool has_expression
Flag indicating whether a cached unarchived representation of this node exists.
Definition: archive.h:160
ex get_ex() const
Definition: archive.h:147
bool has_ex() const
Definition: archive.h:146
bool find_unsigned(const std::string &name, unsigned &ret, unsigned index=0) const
Retrieve property of type "unsigned" from node.
Definition: archive.cpp:436
void add_unsigned(const std::string &name, unsigned value)
Add property of type "unsigned int" to node.
Definition: archive.cpp:400
bool find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index=0) const
Retrieve property of type "ex" from node.
Definition: archive.cpp:477
bool has_same_ex_as(const archive_node &other) const
Check if the archive_node stores the same expression as another archive_node.
Definition: archive.cpp:349
void find_ex_by_loc(archive_node_cit loc, ex &ret, lst &sym_lst) const
Retrieve property of type "ex" from the node if it is known that this node in fact contains such a pr...
Definition: archive.cpp:472
bool find_bool(const std::string &name, bool &ret, unsigned index=0) const
Retrieve property of type "bool" from node.
Definition: archive.cpp:418
void forget()
Delete cached unarchived expressions from node (for debugging).
Definition: archive.cpp:616
std::vector< property >::const_iterator archive_node_cit
Definition: archive.h:84
archive_node(archive &ar)
Definition: archive.h:89
const archive_node & find_ex_node(const std::string &name, unsigned index=0) const
Retrieve property of type "ex" from node, returning the node of the sub-expression.
Definition: archive.cpp:495
friend std::istream & operator>>(std::istream &is, archive_node &ar)
Read archive_node from binary data stream.
Definition: archive.cpp:245
std::vector< property_info > propinfovector
Definition: archive.h:73
bool find_string(const std::string &name, std::string &ret, unsigned index=0) const
Retrieve property of type "string" from node.
Definition: archive.cpp:454
void add_ex(const std::string &name, const ex &value)
Add property of type "ex" to node.
Definition: archive.cpp:410
void add_bool(const std::string &name, bool value)
Add property of type "bool" to node.
Definition: archive.cpp:395
archive_node_cit_range find_property_range(const std::string &name1, const std::string &name2) const
Find a range of locations in the vector of properties.
Definition: archive.cpp:379
friend std::ostream & operator<<(std::ostream &os, const archive_node &ar)
Write archive_node to binary data stream.
Definition: archive.cpp:200
ex e
The cached unarchived representation of this node (if any).
Definition: archive.h:163
archive_node_cit find_last(const std::string &name) const
Definition: archive.cpp:367
const archive_node & operator=(const archive_node &other)
Assignment operator of archive_node.
Definition: archive.cpp:326
archive_node_cit find_first(const std::string &name) const
Find the location in the vector of properties of the first/last property with a given name.
Definition: archive.cpp:357
archive & a
Reference to the archive to which this node belongs.
Definition: archive.h:154
ex unarchive(lst &sym_lst) const
Convert archive node to GiNaC expression.
Definition: archive.cpp:544
void add_string(const std::string &name, const std::string &value)
Add property of type "string" to node.
Definition: archive.cpp:405
This class holds archived versions of GiNaC expressions (class ex).
Definition: archive.h:255
friend std::ostream & operator<<(std::ostream &os, const archive &ar)
Write archive to binary data stream.
Definition: archive.cpp:213
archive_node_id add_node(const archive_node &n)
Add archive_node to archive if the corresponding expression is not already archived.
Definition: archive.cpp:50
archive_atom atomize(const std::string &s) const
Atomize a string (i.e.
Definition: archive.cpp:301
archive(const ex &e, const char *n)
Construct archive from expression using the specified name.
Definition: archive.h:267
void forget()
Delete cached unarchived expressions in all archive_nodes (mainly for debugging).
Definition: archive.cpp:610
ex unarchive_ex(const lst &sym_lst, const char *name) const
Retrieve expression from archive by name.
Definition: archive.cpp:78
archive(const ex &e)
Construct archive from expression using the default name "ex".
Definition: archive.h:264
void clear()
Clear all archived expressions.
Definition: archive.cpp:599
void archive_ex(const ex &e, const char *name)
Archive an expression.
Definition: archive.cpp:35
unsigned num_expressions() const
Return number of archived expressions.
Definition: archive.cpp:120
const std::string & unatomize(archive_atom id) const
Unatomize a string (i.e.
Definition: archive.cpp:316
std::map< std::string, archive_atom >::const_iterator inv_at_cit
The map of from strings to indices of the atoms vectors allows for faster archiving.
Definition: archive.h:333
std::map< ex, archive_node_id, ex_is_less > exprtable
Map of stored expressions to nodes for faster archiving.
Definition: archive.h:337
const archive_node & get_top_node(unsigned index=0) const
Return reference to top node of an expression specified by index.
Definition: archive.cpp:125
friend std::istream & operator>>(std::istream &is, archive &ar)
Read archive from binary data stream.
Definition: archive.cpp:260
std::vector< archive_node > nodes
Vector of archived nodes.
Definition: archive.h:309
archive_node & get_node(archive_node_id id)
Retrieve archive_node by ID.
Definition: archive.cpp:69
void printraw(std::ostream &os) const
Print archive to stream in ugly raw format (for debugging).
Definition: archive.cpp:624
std::vector< archived_ex > exprs
Vector of archived expression descriptors.
Definition: archive.h:321
std::vector< std::string > atoms
Vector of atomized strings (using a vector allows faster unarchiving).
Definition: archive.h:329
std::map< std::string, archive_atom > inverse_atoms
Definition: archive.h:334
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition: basic.h:105
Wrapper template for making GiNaC classes out of STL containers.
Definition: container.h:73
Lightweight wrapper for GiNaC's symbolic objects.
Definition: ex.h:72
void insert(const std::string &classname, synthesize_func f)
Definition: archive.cpp:584
synthesize_func find(const std::string &classname) const
Definition: archive.cpp:575
static unarchive_map_t * unarch_map
Definition: archive.h:172
Interface to GiNaC's light-weight expression handles.
static const bool value
Definition: factor.cpp:199
size_t n
Definition: factor.cpp:1432
size_t c
Definition: factor.cpp:757
Definition: add.cpp:38
unsigned archive_atom
Numerical ID value to refer to a string.
Definition: archive.h:42
std::ostream & operator<<(std::ostream &os, const archive_node &n)
Write archive_node to binary data stream.
Definition: archive.cpp:200
std::map< std::string, synthesize_func > unarchive_map_t
Definition: archive.h:167
basic *(* synthesize_func)()
Definition: archive.h:166
unsigned archive_node_id
Numerical ID value to refer to an archive_node.
Definition: archive.h:39
static unarchive_table_t unarch_table_instance
Definition: archive.h:179
std::istream & operator>>(std::istream &is, archive_node &n)
Read archive_node from binary data stream.
Definition: archive.cpp:245
Archived expression descriptor.
Definition: archive.h:312
archive_node_id root
ID of root node.
Definition: archive.h:317
archived_ex(archive_atom n, archive_node_id node)
Definition: archive.h:314
archive_atom name
Name of expression.
Definition: archive.h:316
Information about a stored property.
Definition: archive.h:65
property_info(property_type t, const std::string &n, unsigned c=1)
Definition: archive.h:67
unsigned count
Number of occurrences.
Definition: archive.h:71
std::string name
Name of property.
Definition: archive.h:70
property_type type
Data type of property.
Definition: archive.h:69
Archived property (data type, name and associated data)
Definition: archive.h:76
property_type type
Data type of property.
Definition: archive.h:80
property(archive_atom n, property_type t, unsigned v)
Definition: archive.h:78
unsigned value
Stored value.
Definition: archive.h:82
archive_atom name
Name of property.
Definition: archive.h:81

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.