X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Farchive.cpp;h=d25c804a49045e2f8fd737c4d2065f4df59d83f0;hp=8280e429f105cf4ef05c922e0776f57f49a6eae2;hb=cca88b51436e4b654d16a4d60cd0d1c66fcf5dd6;hpb=619d77d2676f7f1a562fb9fefc0ba6754fe2d750 diff --git a/ginac/archive.cpp b/ginac/archive.cpp index 8280e429..d25c804a 100644 --- a/ginac/archive.cpp +++ b/ginac/archive.cpp @@ -3,7 +3,7 @@ * Archiving of GiNaC expressions. */ /* - * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2014 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,15 +268,19 @@ 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); ar.atoms.resize(num_atoms); - for (unsigned i=0; i>(std::istream &is, archive &ar) * represents the string). */ archive_atom archive::atomize(const std::string &s) const { - // Search for string in atoms vector - std::vector::const_iterator i = atoms.begin(), iend = atoms.end(); - archive_atom id = 0; - while (i != iend) { - if (*i == s) - return id; - i++; id++; - } + // Search for string in inverse_atoms map. + inv_at_cit i = inverse_atoms.find(s); + if (i!=inverse_atoms.end()) + return i->second; // Not found, add to atoms vector + archive_atom id = atoms.size(); atoms.push_back(s); + inverse_atoms[s] = id; return 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,18 +539,55 @@ 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() { atoms.clear(); + inverse_atoms.clear(); exprs.clear(); nodes.clear(); exprtable.clear();