]> www.ginac.de Git - ginac.git/blobdiff - ginac/archive.cpp
More evaluation rules: abs(x^n) => abs(x)^n (x > 0, n is real).
[ginac.git] / ginac / archive.cpp
index 8280e429f105cf4ef05c922e0776f57f49a6eae2..6466834140a7e6e3a08b2ad40b5585b0646e20b4 100644 (file)
@@ -3,7 +3,7 @@
  *  Archiving of GiNaC expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2011 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,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<num_atoms; i++)
+       for (unsigned i=0; i<num_atoms; i++) {
                getline(is, ar.atoms[i], '\0');
+               ar.inverse_atoms[ar.atoms[i]] = i;
+       }
 
        // Read expressions
        unsigned num_exprs = read_unsigned(is);
@@ -297,17 +304,15 @@ std::istream &operator>>(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<std::string>::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<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()
 {
        atoms.clear();
+       inverse_atoms.clear();
        exprs.clear();
        nodes.clear();
        exprtable.clear();