X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Farchive.cpp;h=ffd8b58a41a426cf08ffca449b493c78047dc516;hp=d462584ae3084e76b93a0d11c5c22f8eca906381;hb=85f46b3d72dbafb6fe1fbb9bca32ec612cf8b480;hpb=83a7ee99a947cbbf331018b803ad6be43a9ccd45 diff --git a/ginac/archive.cpp b/ginac/archive.cpp index d462584a..ffd8b58a 100644 --- a/ginac/archive.cpp +++ b/ginac/archive.cpp @@ -3,7 +3,7 @@ * Archiving of GiNaC expressions. */ /* - * GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,15 +20,18 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include - #include "archive.h" #include "registrar.h" #include "ex.h" #include "lst.h" +#ifdef HAVE_CONFIG_H #include "config.h" +#endif #include "tostring.h" +#include "version.h" + +#include +#include namespace GiNaC { @@ -218,7 +221,7 @@ std::ostream &operator<<(std::ostream &os, const archive &ar) os.put('A'); os.put('R'); os.put('C'); - write_unsigned(os, ARCHIVE_VERSION); + write_unsigned(os, GINACLIB_ARCHIVE_VERSION); // Write atoms unsigned num_atoms = ar.atoms.size(); @@ -265,9 +268,11 @@ std::istream &operator>>(std::istream &is, archive &ar) is.get(c1); is.get(c2); is.get(c3); is.get(c4); if (c1 != 'G' || c2 != 'A' || c3 != 'R' || c4 != 'C') throw (std::runtime_error("not a GiNaC archive (signature not found)")); + static const unsigned max_version = GINACLIB_ARCHIVE_VERSION; + static const unsigned min_version = GINACLIB_ARCHIVE_VERSION - GINACLIB_ARCHIVE_AGE; unsigned version = read_unsigned(is); - if (version > ARCHIVE_VERSION || version < ARCHIVE_VERSION - ARCHIVE_AGE) - throw (std::runtime_error("archive version " + ToString(version) + " cannot be read by this GiNaC library (which supports versions " + ToString(ARCHIVE_VERSION-ARCHIVE_AGE) + " thru " + ToString(ARCHIVE_VERSION))); + if ((version > max_version) || (version < min_version)) + throw (std::runtime_error("archive version " + ToString(version) + " cannot be read by this GiNaC library (which supports versions " + ToString(min_version) + " thru " + ToString(max_version))); // Read atoms unsigned num_atoms = read_unsigned(is); @@ -315,7 +320,7 @@ archive_atom archive::atomize(const std::string &s) const const std::string &archive::unatomize(archive_atom id) const { if (id >= atoms.size()) - throw (std::range_error("archive::unatomizee(): atom ID out of range")); + throw (std::range_error("archive::unatomize(): atom ID out of range")); return atoms[id]; } @@ -516,6 +521,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 +539,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() { @@ -626,13 +673,5 @@ void archive_node::printraw(std::ostream &os) const } } -/** Create a dummy archive. The intention is to fill archive_node's default - * ctor, which is currently a Cint-requirement. */ -archive* archive_node::dummy_ar_creator() -{ - static archive* some_ar = new archive; - return some_ar; -} - } // namespace GiNaC