From d327f3f00c66a79d42855939866047b3e8caa630 Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Sun, 19 Oct 2008 19:39:30 +0400 Subject: [PATCH] Don't force every algebraic class to implement archiving/unarchiving. So people who don't use (un)archiving don't need to bother with it. --- doc/examples/mystring.cpp | 3 +- ginac/add.cpp | 2 +- ginac/add.h | 1 + ginac/archive.cpp | 46 +++++++++++++++++++++- ginac/archive.h | 80 +++++++++++++++++++++++++++++++++++++++ ginac/basic.cpp | 12 +----- ginac/basic.h | 17 +++++++++ ginac/clifford.cpp | 16 ++++---- ginac/clifford.h | 8 ++++ ginac/color.cpp | 13 ++++--- ginac/color.h | 7 ++++ ginac/constant.cpp | 11 +++--- ginac/constant.h | 7 ++-- ginac/container.h | 63 +++++++++++++----------------- ginac/ex.h | 1 + ginac/expairseq.cpp | 7 +--- ginac/expairseq.h | 3 ++ ginac/fail.cpp | 3 +- ginac/fail.h | 2 + ginac/fderivative.cpp | 5 ++- ginac/fderivative.h | 3 ++ ginac/function.pl | 14 +++---- ginac/idx.cpp | 16 ++++---- ginac/idx.h | 12 ++++-- ginac/indexed.cpp | 6 +-- ginac/indexed.h | 5 +++ ginac/integral.cpp | 6 +-- ginac/integral.h | 6 +++ ginac/lst.cpp | 3 ++ ginac/lst.h | 1 + ginac/matrix.cpp | 11 +++--- ginac/matrix.h | 6 +++ ginac/mul.cpp | 4 +- ginac/mul.h | 1 + ginac/ncmul.cpp | 5 ++- ginac/ncmul.h | 2 + ginac/numeric.cpp | 6 +-- ginac/numeric.h | 6 +++ ginac/power.cpp | 7 ++-- ginac/power.h | 6 +++ ginac/pseries.cpp | 6 ++- ginac/pseries.h | 5 +++ ginac/registrar.cpp | 10 ----- ginac/registrar.h | 29 +++----------- ginac/relational.cpp | 6 +-- ginac/relational.h | 6 +++ ginac/structure.h | 21 +--------- ginac/symbol.cpp | 39 +++++++++---------- ginac/symbol.h | 9 ++++- ginac/symmetry.cpp | 6 +-- ginac/symmetry.h | 6 +++ ginac/tensor.cpp | 19 +++++----- ginac/tensor.h | 15 +++++++- ginac/utils.cpp | 2 + ginac/utils.h | 14 ------- ginac/wildcard.cpp | 6 +-- ginac/wildcard.h | 6 +++ 57 files changed, 409 insertions(+), 229 deletions(-) diff --git a/doc/examples/mystring.cpp b/doc/examples/mystring.cpp index eb0e452c..ac9903f0 100644 --- a/doc/examples/mystring.cpp +++ b/doc/examples/mystring.cpp @@ -47,8 +47,9 @@ int mystring::compare_same_type(const basic &other) const } // archiving/unarchiving -mystring::mystring(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) +void mystring::read_archive(const archive_node &n, lst &sym_lst) { + inherited::read_archive(n, sym_lst); n.find_string("string", str); } diff --git a/ginac/add.cpp b/ginac/add.cpp index 7f721544..06ada496 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -97,7 +97,7 @@ add::add(std::auto_ptr vp, const ex & oc) // archiving ////////// -DEFAULT_ARCHIVING(add) +GINAC_BIND_UNARCHIVER(add); ////////// // functions overriding virtual functions from base classes diff --git a/ginac/add.h b/ginac/add.h index d96e1263..0dfd6034 100644 --- a/ginac/add.h +++ b/ginac/add.h @@ -84,6 +84,7 @@ protected: void do_print_csrc(const print_csrc & c, unsigned level) const; void do_print_python_repr(const print_python_repr & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(add); } // namespace GiNaC diff --git a/ginac/archive.cpp b/ginac/archive.cpp index d462584a..1d65c7a9 100644 --- a/ginac/archive.cpp +++ b/ginac/archive.cpp @@ -516,6 +516,12 @@ void archive_node::get_properties(propinfovector &v) const } } +static synthesize_func find_factory_fcn(const std::string& name) +{ + static unarchive_table_t the_table; + synthesize_func ret = the_table.find(name); + return ret; +} /** Convert archive node to GiNaC expression. */ ex archive_node::unarchive(lst &sym_lst) const @@ -528,14 +534,50 @@ ex archive_node::unarchive(lst &sym_lst) const std::string class_name; if (!find_string("class", class_name)) throw (std::runtime_error("archive node contains no class name")); - unarch_func f = find_unarch_func(class_name); // Call instantiation function - e = f(*this, sym_lst); + synthesize_func factory_fcn = find_factory_fcn(class_name); + ptr obj(factory_fcn()); + obj->setflag(status_flags::dynallocated); + obj->read_archive(*this, sym_lst); + e = ex(*obj); has_expression = true; return e; } +int unarchive_table_t::usecount = 0; +unarchive_map_t* unarchive_table_t::unarch_map = 0; + +unarchive_table_t::unarchive_table_t() +{ + if (usecount == 0) + unarch_map = new unarchive_map_t(); + ++usecount; +} + +synthesize_func unarchive_table_t::find(const std::string& classname) const +{ + unarchive_map_t::const_iterator i = unarch_map->find(classname); + if (i != unarch_map->end()) + return i->second; + throw std::runtime_error(std::string("no unarchiving function for \"") + + classname + "\" class"); +} + +void unarchive_table_t::insert(const std::string& classname, synthesize_func f) +{ + if (unarch_map->find(classname) != unarch_map->end()) + throw std::runtime_error(std::string("Class \"" + classname + + "\" is already registered")); + unarch_map->operator[](classname) = f; +} + +unarchive_table_t::~unarchive_table_t() +{ + if (--usecount == 0) + delete unarch_map; +} + void archive::clear() { diff --git a/ginac/archive.h b/ginac/archive.h index 0c2a1af6..5969b00c 100644 --- a/ginac/archive.h +++ b/ginac/archive.h @@ -158,6 +158,86 @@ private: mutable ex e; }; +typedef basic* (*synthesize_func)(); +typedef std::map unarchive_map_t; + +class unarchive_table_t +{ + static int usecount; + static unarchive_map_t* unarch_map; +public: + unarchive_table_t(); + ~unarchive_table_t(); + synthesize_func find(const std::string& classname) const; + void insert(const std::string& classname, synthesize_func f); +}; +static unarchive_table_t unarch_table_instance; + +/** Helper macros to register a class with (un)archiving (a.k.a. + * (de)serialization). + * + * Usage: put + * + * GINAC_DECLARE_UNARCHIVER(myclass); + * + * into the header file (in the global or namespace scope), and + * + * GINAC_BIND_UNARCHIVER(myclass); + * + * into the source file. + * + * Effect: the `myclass' (being a class derived directly or indirectly + * from GiNaC::basic) can be archived and unarchived. + * + * Note: you need to use GINAC_{DECLARE,BIND}_UNARCHIVER incantations + * in order to make your class (un)archivable _even if your class does + * not overload `read_archive' method_. Sorry for inconvenience. + * + * How it works: + * + * The `basic' class has a `read_archive' virtual method which reads an + * expression from archive. Derived classes can overload that method. + * There's a small problem, though. On unarchiving all we have is a set + * of named byte streams. In C++ the class name (as written in the source + * code) has nothing to do with its actual type. Thus, we need establish + * a correspondence ourselves. To do so we maintain a `class_name' => + * `function_pointer' table (see the unarchive_table_t class above). + * Every function in this table is supposed to create a new object of + * the `class_name' type. The `archive_node' class uses that table to + * construct an object of correct type. Next it invokes read_archive + * virtual method of newly created object, which does the actual job. + * + * Note: this approach is very simple-minded (it does not handle classes + * with same names from different namespaces, multiple inheritance, etc), + * but it happens to work surprisingly well. + */ +#define GINAC_DECLARE_UNARCHIVER(classname) \ +class classname ## _unarchiver \ +{ \ + static int usecount; \ +public: \ + static GiNaC::basic* create(); \ + classname ## _unarchiver(); \ + ~ classname ## _unarchiver(); \ +}; \ +static classname ## _unarchiver classname ## _unarchiver_instance + +#define GINAC_BIND_UNARCHIVER(classname) \ +classname ## _unarchiver::classname ## _unarchiver() \ +{ \ + static GiNaC::unarchive_table_t table; \ + if (usecount++ == 0) { \ + table.insert(std::string(#classname), \ + &(classname ## _unarchiver::create)); \ + } \ +} \ +GiNaC::basic* classname ## _unarchiver::create() \ +{ \ + return new classname(); \ +} \ +classname ## _unarchiver::~ classname ## _unarchiver() { } \ +int classname ## _unarchiver::usecount = 0 + /** This class holds archived versions of GiNaC expressions (class ex). * An archive can be constructed from an expression and then written to diff --git a/ginac/basic.cpp b/ginac/basic.cpp index 237c63df..04f87df1 100644 --- a/ginac/basic.cpp +++ b/ginac/basic.cpp @@ -92,16 +92,8 @@ const basic & basic::operator=(const basic & other) ////////// /** Construct object from archive_node. */ -basic::basic(const archive_node &n, lst &sym_lst) : flags(0) -{ - // Reconstruct tinfo_key from class name - std::string class_name; - if (!n.find_string("class", class_name)) - throw (std::runtime_error("archive node contains no class name")); -} - -/** Unarchive the object. */ -DEFAULT_UNARCHIVE(basic) +void basic::read_archive(const archive_node& n, lst& syms) +{ } /** Archive the object. */ void basic::archive(archive_node &n) const diff --git a/ginac/basic.h b/ginac/basic.h index 534a5915..51e0e909 100644 --- a/ginac/basic.h +++ b/ginac/basic.h @@ -241,6 +241,23 @@ public: void print_dispatch(const registered_class_info & ri, const print_context & c, unsigned level) const; + /** Save (serialize) the object into archive node. + * + * Losely speaking, this method turns an expression into a byte + * stream (which can be saved and restored later on, or sent via + * network, etc.) + */ + virtual void archive(archive_node& n) const; + /** Load (deserialize) the object from an archive node. + * + * @note This method is essentially a constructor. However, + * constructors can't be virtual. So, if unarchiving routines + * are implemented as constructors one would need to define such + * a constructor in every class, even if all it does is simply + * calling constructor of a superclass. + */ + virtual void read_archive(const archive_node& n, lst& syms); // no const + ex subs_one_level(const exmap & m, unsigned options) const; ex diff(const symbol & s, unsigned nth = 1) const; int compare(const basic & other) const; diff --git a/ginac/clifford.cpp b/ginac/clifford.cpp index d3667aac..07da6935 100644 --- a/ginac/clifford.cpp +++ b/ginac/clifford.cpp @@ -122,8 +122,9 @@ return_type_t clifford::return_type_tinfo() const // archiving ////////// -clifford::clifford(const archive_node & n, lst & sym_lst) : inherited(n, sym_lst) +void clifford::read_archive(const archive_node& n, lst& sym_lst) { + inherited::read_archive(n, sym_lst); unsigned rl; n.find_unsigned("label", rl); representation_label = rl; @@ -140,13 +141,12 @@ void clifford::archive(archive_node & n) const n.add_unsigned("commutator_sign+1", commutator_sign+1); } -DEFAULT_UNARCHIVE(clifford) -DEFAULT_ARCHIVING(diracone) -DEFAULT_ARCHIVING(cliffordunit) -DEFAULT_ARCHIVING(diracgamma) -DEFAULT_ARCHIVING(diracgamma5) -DEFAULT_ARCHIVING(diracgammaL) -DEFAULT_ARCHIVING(diracgammaR) +GINAC_BIND_UNARCHIVER(clifford); +GINAC_BIND_UNARCHIVER(diracone); +GINAC_BIND_UNARCHIVER(diracgamma); +GINAC_BIND_UNARCHIVER(diracgamma5); +GINAC_BIND_UNARCHIVER(diracgammaL); +GINAC_BIND_UNARCHIVER(diracgammaR); ex clifford::get_metric(const ex & i, const ex & j, bool symmetrised) const diff --git a/ginac/clifford.h b/ginac/clifford.h index 6b74a468..efb9dd66 100644 --- a/ginac/clifford.h +++ b/ginac/clifford.h @@ -53,6 +53,8 @@ public: // functions overriding virtual functions from base classes public: unsigned precedence() const { return 65; } + void archive(archive_node& n) const; + void read_archive(const archive_node& n, lst& sym_lst); protected: ex eval_ncmul(const exvector & v) const; bool match_same_type(const basic & other) const; @@ -83,6 +85,7 @@ protected: ex metric; /**< Metric of the space, all constructors make it an indexed object */ int commutator_sign; /**< It is the sign in the definition e~i e~j +/- e~j e~i = B(i, j) + B(j, i)*/ }; +GINAC_DECLARE_UNARCHIVER(clifford); /** This class represents the Clifford algebra unity element. */ class diracone : public tensor @@ -94,6 +97,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(diracone); /** This class represents the Clifford algebra generators (units). */ @@ -126,6 +130,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(diracgamma); /** This class represents the Dirac gamma5 object which anticommutates with @@ -142,6 +147,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(diracgamma5); /** This class represents the Dirac gammaL object which behaves like @@ -158,6 +164,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(diracgammaL); /** This class represents the Dirac gammaL object which behaves like @@ -174,6 +181,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(diracgammaR); // global functions diff --git a/ginac/color.cpp b/ginac/color.cpp index 30c38d9f..b1ca9050 100644 --- a/ginac/color.cpp +++ b/ginac/color.cpp @@ -103,8 +103,9 @@ return_type_t color::return_type_tinfo() const // archiving ////////// -color::color(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) +void color::read_archive(const archive_node& n, lst& sym_lst) { + inherited::read_archive(n, sym_lst); unsigned rl; n.find_unsigned("label", rl); representation_label = rl; @@ -116,11 +117,11 @@ void color::archive(archive_node &n) const n.add_unsigned("label", representation_label); } -DEFAULT_UNARCHIVE(color) -DEFAULT_ARCHIVING(su3one) -DEFAULT_ARCHIVING(su3t) -DEFAULT_ARCHIVING(su3f) -DEFAULT_ARCHIVING(su3d) +GINAC_BIND_UNARCHIVER(color); +GINAC_BIND_UNARCHIVER(su3one); +GINAC_BIND_UNARCHIVER(su3t); +GINAC_BIND_UNARCHIVER(su3f); +GINAC_BIND_UNARCHIVER(su3d); ////////// // functions overriding virtual functions from base classes diff --git a/ginac/color.h b/ginac/color.h index 360223e2..d595cb3e 100644 --- a/ginac/color.h +++ b/ginac/color.h @@ -49,6 +49,8 @@ public: // internal constructors color(unsigned char rl, const exvector & v, bool discardable = false); color(unsigned char rl, std::auto_ptr vp); + void archive(archive_node& n) const; + void read_archive(const archive_node& n, lst& sym_lst); // functions overriding virtual functions from base classes protected: @@ -67,6 +69,7 @@ public: private: unsigned char representation_label; /**< Representation label to distinguish independent color matrices coming from separated fermion lines */ }; +GINAC_DECLARE_UNARCHIVER(color); /** This class represents the su(3) unity element. */ @@ -79,6 +82,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(su3one); /** This class represents an su(3) generator. */ class su3t : public tensor @@ -94,6 +98,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(su3t); /** This class represents the tensor of antisymmetric su(3) structure * constants. */ @@ -112,6 +117,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(su3f); /** This class represents the tensor of symmetric su(3) structure constants. */ class su3d : public tensor @@ -129,6 +135,7 @@ protected: void do_print(const print_context & c, unsigned level) const; void do_print_latex(const print_latex & c, unsigned level) const; }; +GINAC_DECLARE_UNARCHIVER(su3d); // global functions diff --git a/ginac/constant.cpp b/ginac/constant.cpp index 9247ac20..10972b07 100644 --- a/ginac/constant.cpp +++ b/ginac/constant.cpp @@ -80,25 +80,24 @@ constant::constant(const std::string & initname, const numeric & initnumber, con // archiving ////////// -constant::constant(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {} - -ex constant::unarchive(const archive_node &n, lst &sym_lst) +void constant::read_archive(const archive_node &n, lst &sym_lst) { // Find constant by name (!! this is bad: 'twould be better if there // was a list of all global constants that we could search) std::string s; if (n.find_string("name", s)) { if (s == Pi.name) - return Pi; + *this = Pi; else if (s == Catalan.name) - return Catalan; + *this = Catalan; else if (s == Euler.name) - return Euler; + *this = Euler; else throw (std::runtime_error("unknown constant '" + s + "' in archive")); } else throw (std::runtime_error("unnamed constant in archive")); } +GINAC_BIND_UNARCHIVER(constant); void constant::archive(archive_node &n) const { diff --git a/ginac/constant.h b/ginac/constant.h index c18f6fc9..efbe39e9 100644 --- a/ginac/constant.h +++ b/ginac/constant.h @@ -26,6 +26,7 @@ #include #include "basic.h" #include "ex.h" +#include "archive.h" namespace GiNaC { @@ -38,9 +39,6 @@ typedef ex (*evalffunctype)(); class constant : public basic { GINAC_DECLARE_REGISTERED_CLASS(constant, basic) - -// member functions - // other constructors public: constant(const std::string & initname, evalffunctype efun = 0, const std::string & texname = std::string(), unsigned domain = domain::complex); @@ -54,6 +52,8 @@ public: ex conjugate() const; ex real_part() const; ex imag_part() const; + void archive(archive_node& n) const; + void read_archive(const archive_node& n, lst& syms); protected: ex derivative(const symbol & s) const; bool is_equal_same_type(const basic & other) const; @@ -76,6 +76,7 @@ private: static unsigned next_serial; unsigned domain; ///< numerical value this constant evalf()s to }; +GINAC_DECLARE_UNARCHIVER(constant); extern const constant Pi; extern const constant Catalan; diff --git a/ginac/container.h b/ginac/container.h index 7092e3b0..c1345949 100644 --- a/ginac/container.h +++ b/ginac/container.h @@ -128,7 +128,6 @@ private: template