GiNaC  1.8.0
archive.h
Go to the documentation of this file.
1 
5 /*
6  * GiNaC Copyright (C) 1999-2020 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 
33 namespace GiNaC {
34 
35 class archive;
36 
37 
39 typedef unsigned archive_node_id;
40 
42 typedef 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 
53 public:
60  };
61 
65  struct property_info {
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 {
77  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 
152 private:
155 
157  std::vector<property> props;
158 
160  mutable bool has_expression;
161 
163  mutable ex e;
164 };
165 
166 typedef basic* (*synthesize_func)();
167 typedef std::map<std::string, synthesize_func> unarchive_map_t;
168 
170 {
171  static int usecount;
173 public:
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) \
220 class classname ## _unarchiver \
221 { \
222  static int usecount; \
223 public: \
224  static GiNaC::basic* create(); \
225  classname ## _unarchiver(); \
226  ~ classname ## _unarchiver(); \
227 }; \
228 static classname ## _unarchiver classname ## _unarchiver_instance
229 
230 #define GINAC_BIND_UNARCHIVER(classname) \
231 classname ## _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 } \
239 GiNaC::basic* classname ## _unarchiver::create() \
240 { \
241  return new classname(); \
242 } \
243 classname ## _unarchiver::~ classname ## _unarchiver() { } \
244 int classname ## _unarchiver::usecount = 0
245 
246 
254 class archive
255 {
256  friend std::ostream &operator<<(std::ostream &os, const archive &ar);
257  friend std::istream &operator>>(std::istream &is, archive &ar);
258 
259 public:
260  archive() {}
261  ~archive() {}
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 
307 private:
309  std::vector<archive_node> nodes;
310 
312  struct archived_ex {
315 
318  };
319 
321  std::vector<archived_ex> exprs;
322 
323 public:
324  archive_atom atomize(const std::string &s) const;
325  const std::string &unatomize(archive_atom id) const;
326 
327 private:
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 
341 std::ostream &operator<<(std::ostream &os, const archive &ar);
342 std::istream &operator>>(std::istream &is, archive &ar);
343 
344 } // namespace GiNaC
345 
346 #endif // ndef GINAC_ARCHIVE_H
GiNaC::archive_node::find_bool
bool find_bool(const std::string &name, bool &ret, unsigned index=0) const
Retrieve property of type "bool" from node.
Definition: archive.cpp:418
GiNaC::archive_node::property::type
property_type type
Data type of property.
Definition: archive.h:80
GiNaC::archive_node::archive_node
archive_node(archive &ar)
Definition: archive.h:89
GiNaC::archive::archive_ex
void archive_ex(const ex &e, const char *name)
Archive an expression.
Definition: archive.cpp:35
GiNaC::archive_node::property_info::type
property_type type
Data type of property.
Definition: archive.h:69
GiNaC::archive_node::find_unsigned
bool find_unsigned(const std::string &name, unsigned &ret, unsigned index=0) const
Retrieve property of type "unsigned" from node.
Definition: archive.cpp:436
GiNaC::archive_node::find_property_range
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
GiNaC::archive_node::find_first
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
GiNaC::archive_node::has_same_ex_as
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
GiNaC::archive_node::PTYPE_BOOL
@ PTYPE_BOOL
Definition: archive.h:56
GiNaC::archive_node::add_string
void add_string(const std::string &name, const std::string &value)
Add property of type "string" to node.
Definition: archive.cpp:405
GiNaC::archive::clear
void clear()
Clear all archived expressions.
Definition: archive.cpp:599
GiNaC::archive::get_top_node
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
GiNaC::archive_node::forget
void forget()
Delete cached unarchived expressions from node (for debugging).
Definition: archive.cpp:616
GiNaC::archive::archive
archive(const ex &e)
Construct archive from expression using the default name "ex".
Definition: archive.h:264
GiNaC::archive_node::property
Archived property (data type, name and associated data)
Definition: archive.h:76
GiNaC::unarchive_table_t::unarchive_table_t
unarchive_table_t()
Definition: archive.cpp:568
GiNaC::archive::atoms
std::vector< std::string > atoms
Vector of atomized strings (using a vector allows faster unarchiving).
Definition: archive.h:329
GiNaC::archive_node::property::property
property()
Definition: archive.h:77
GiNaC::archive_node::add_ex
void add_ex(const std::string &name, const ex &value)
Add property of type "ex" to node.
Definition: archive.cpp:410
GiNaC::unarchive_table_t::find
synthesize_func find(const std::string &classname) const
Definition: archive.cpp:575
GiNaC::archive_node
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition: archive.h:49
GiNaC::archive_node::unarchive
ex unarchive(lst &sym_lst) const
Convert archive node to GiNaC expression.
Definition: archive.cpp:544
GiNaC::archive_node::find_ex
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
GiNaC::synthesize_func
basic *(* synthesize_func)()
Definition: archive.h:166
GiNaC
Definition: add.cpp:38
GiNaC::unarch_table_instance
static unarchive_table_t unarch_table_instance
Definition: archive.h:179
GiNaC::archive::num_expressions
unsigned num_expressions() const
Return number of archived expressions.
Definition: archive.cpp:120
GiNaC::operator<<
std::ostream & operator<<(std::ostream &os, const archive_node &n)
Write archive_node to binary data stream.
Definition: archive.cpp:200
GiNaC::archive_node::find_ex_node
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
GiNaC::archive::archive
archive()
Definition: archive.h:260
GiNaC::unarchive_table_t::usecount
static int usecount
Definition: archive.h:171
GiNaC::archive_node::a
archive & a
Reference to the archive to which this node belongs.
Definition: archive.h:154
GiNaC::archive_node::e
ex e
The cached unarchived representation of this node (if any).
Definition: archive.h:163
GiNaC::archive_node::property::name
archive_atom name
Name of property.
Definition: archive.h:81
GiNaC::archive::unarchive_ex
ex unarchive_ex(const lst &sym_lst, const char *name) const
Retrieve expression from archive by name.
Definition: archive.cpp:78
GiNaC::archive_node::printraw
void printraw(std::ostream &os) const
Output archive_node to stream in ugly raw format (for debugging).
Definition: archive.cpp:664
GiNaC::archive_node::archive_node_cit_range::end
archive_node_cit end
Definition: archive.h:86
GiNaC::ex
Lightweight wrapper for GiNaC's symbolic objects.
Definition: ex.h:72
GiNaC::archive_node::propinfovector
std::vector< property_info > propinfovector
Definition: archive.h:73
GiNaC::archive_node::add_unsigned
void add_unsigned(const std::string &name, unsigned value)
Add property of type "unsigned int" to node.
Definition: archive.cpp:400
GiNaC::archive::atomize
archive_atom atomize(const std::string &s) const
Atomize a string (i.e.
Definition: archive.cpp:301
GiNaC::archive::archive
archive(const ex &e, const char *n)
Construct archive from expression using the specified name.
Definition: archive.h:267
GiNaC::archive_node::find_last
archive_node_cit find_last(const std::string &name) const
Definition: archive.cpp:367
GiNaC::archive::printraw
void printraw(std::ostream &os) const
Print archive to stream in ugly raw format (for debugging).
Definition: archive.cpp:624
GiNaC::archive::unatomize
const std::string & unatomize(archive_atom id) const
Unatomize a string (i.e.
Definition: archive.cpp:316
value
static const bool value
Definition: factor.cpp:231
GiNaC::archive_node::props
std::vector< property > props
Vector of stored properties.
Definition: archive.h:157
GiNaC::archive_node::property_info::count
unsigned count
Number of occurrences.
Definition: archive.h:71
ex.h
Interface to GiNaC's light-weight expression handles.
GiNaC::archive::archived_ex::archived_ex
archived_ex(archive_atom n, archive_node_id node)
Definition: archive.h:314
GiNaC::archive_node::has_expression
bool has_expression
Flag indicating whether a cached unarchived representation of this node exists.
Definition: archive.h:160
GiNaC::archive_node::get_properties
void get_properties(propinfovector &v) const
Return vector of properties stored in node.
Definition: archive.cpp:512
GiNaC::archive::exprs
std::vector< archived_ex > exprs
Vector of archived expression descriptors.
Definition: archive.h:321
GiNaC::archive_node::archive_node_cit
std::vector< property >::const_iterator archive_node_cit
Definition: archive.h:84
GiNaC::archive::archived_ex::name
archive_atom name
Name of expression.
Definition: archive.h:316
GiNaC::archive_node::property::property
property(archive_atom n, property_type t, unsigned v)
Definition: archive.h:78
GiNaC::archive_node::archive_node_cit_range::begin
archive_node_cit begin
Definition: archive.h:86
GiNaC::archive::inverse_atoms
std::map< std::string, archive_atom > inverse_atoms
Definition: archive.h:334
GiNaC::archive_atom
unsigned archive_atom
Numerical ID value to refer to a string.
Definition: archive.h:42
GiNaC::archive_node::archive_node_cit_range
Definition: archive.h:85
GiNaC::archive::~archive
~archive()
Definition: archive.h:261
GiNaC::archive::operator>>
friend std::istream & operator>>(std::istream &is, archive &ar)
Read archive from binary data stream.
Definition: archive.cpp:260
GiNaC::unarchive_map_t
std::map< std::string, synthesize_func > unarchive_map_t
Definition: archive.h:167
GiNaC::archive_node::operator=
const archive_node & operator=(const archive_node &other)
Assignment operator of archive_node.
Definition: archive.cpp:326
GiNaC::archive::operator<<
friend std::ostream & operator<<(std::ostream &os, const archive &ar)
Write archive to binary data stream.
Definition: archive.cpp:213
GiNaC::container
Wrapper template for making GiNaC classes out of STL containers.
Definition: container.h:73
GiNaC::operator>>
std::istream & operator>>(std::istream &is, archive_node &n)
Read archive_node from binary data stream.
Definition: archive.cpp:245
c
size_t c
Definition: factor.cpp:770
GiNaC::unarchive_table_t::insert
void insert(const std::string &classname, synthesize_func f)
Definition: archive.cpp:584
GiNaC::archive::archived_ex::archived_ex
archived_ex()
Definition: archive.h:313
n
size_t n
Definition: factor.cpp:1463
GiNaC::unarchive_table_t
Definition: archive.h:170
GiNaC::archive_node::property_info::property_info
property_info(property_type t, const std::string &n, unsigned c=1)
Definition: archive.h:67
GiNaC::archive_node::has_ex
bool has_ex() const
Definition: archive.h:146
GiNaC::archive
This class holds archived versions of GiNaC expressions (class ex).
Definition: archive.h:255
GiNaC::basic
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition: basic.h:105
GiNaC::archive::add_node
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
GiNaC::archive_node::PTYPE_UNSIGNED
@ PTYPE_UNSIGNED
Definition: archive.h:57
GiNaC::unarchive_table_t::~unarchive_table_t
~unarchive_table_t()
Definition: archive.cpp:592
GiNaC::archive_node::operator<<
friend std::ostream & operator<<(std::ostream &os, const archive_node &ar)
Write archive_node to binary data stream.
Definition: archive.cpp:200
GiNaC::archive_node::property_type
property_type
Property data types.
Definition: archive.h:55
GiNaC::archive::nodes
std::vector< archive_node > nodes
Vector of archived nodes.
Definition: archive.h:309
GiNaC::unarchive_table_t::unarch_map
static unarchive_map_t * unarch_map
Definition: archive.h:172
GiNaC::archive_node::property_info::name
std::string name
Name of property.
Definition: archive.h:70
GiNaC::archive_node::find_string
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
GiNaC::archive::archived_ex
Archived expression descriptor.
Definition: archive.h:312
GiNaC::archive::archived_ex::root
archive_node_id root
ID of root node.
Definition: archive.h:317
GiNaC::archive_node::find_ex_by_loc
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
GiNaC::archive_node::PTYPE_STRING
@ PTYPE_STRING
Definition: archive.h:58
GiNaC::archive_node::add_bool
void add_bool(const std::string &name, bool value)
Add property of type "bool" to node.
Definition: archive.cpp:395
GiNaC::archive_node::property_info
Information about a stored property.
Definition: archive.h:65
GiNaC::archive_node_id
unsigned archive_node_id
Numerical ID value to refer to an archive_node.
Definition: archive.h:35
GiNaC::archive::get_node
archive_node & get_node(archive_node_id id)
Retrieve archive_node by ID.
Definition: archive.cpp:69
GiNaC::archive_node::get_ex
ex get_ex() const
Definition: archive.h:147
GiNaC::archive_node::property::value
unsigned value
Stored value.
Definition: archive.h:82
GiNaC::archive_node::property_info::property_info
property_info()
Definition: archive.h:66
GiNaC::archive::forget
void forget()
Delete cached unarchived expressions in all archive_nodes (mainly for debugging).
Definition: archive.cpp:610
GiNaC::archive::inv_at_cit
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
GiNaC::archive::exprtable
std::map< ex, archive_node_id, ex_is_less > exprtable
Map of stored expressions to nodes for faster archiving.
Definition: archive.h:337
GiNaC::archive_node::operator>>
friend std::istream & operator>>(std::istream &is, archive_node &ar)
Read archive_node from binary data stream.
Definition: archive.cpp:245
GiNaC::archive_node::PTYPE_NODE
@ PTYPE_NODE
Definition: archive.h:59

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