]> www.ginac.de Git - ginac.git/blobdiff - ginac/archive.cpp
Update copyright statements.
[ginac.git] / ginac / archive.cpp
index d462584ae3084e76b93a0d11c5c22f8eca906381..d25c804a49045e2f8fd737c4d2065f4df59d83f0 100644 (file)
@@ -3,7 +3,7 @@
  *  Archiving of GiNaC expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2008 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
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <iostream>
-#include <stdexcept>
-
 #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 <iostream>
+#include <stdexcept>
 
 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);
@@ -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<basic> 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()
 {