]> www.ginac.de Git - ginac.git/commitdiff
- split registered_class_info into the actual per-class data and the
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Mon, 21 Jul 2003 22:09:41 +0000 (22:09 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Mon, 21 Jul 2003 22:09:41 +0000 (22:09 +0000)
  "infrastructure"
- use registered_class_info::dump_hierarchy() to display the class hierarchy
  tree as seen by GiNaC

ginac/Makefile.am
ginac/basic.h
ginac/class_info.h [new file with mode: 0644]
ginac/registrar.cpp
ginac/registrar.h
ginac/structure.h

index 2b961fc1ff755fa8c8a74bcb498ed82060bb5277..89e385f95ec9d9ac4305a03ecff29bc0eb0aa307 100644 (file)
@@ -13,12 +13,12 @@ libginac_la_SOURCES = add.cpp archive.cpp basic.cpp clifford.cpp color.cpp \
 libginac_la_LDFLAGS = -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \
   -release $(LT_RELEASE)
 ginacincludedir = $(includedir)/ginac
-ginacinclude_HEADERS = ginac.h add.h archive.h assertion.h basic.h clifford.h \
-  color.h constant.h container.h ex.h expair.h expairseq.h exprseq.h fail.h \
-  fderivative.h flags.h function.h idx.h indexed.h inifcns.h lst.h matrix.h \
-  mul.h ncmul.h normal.h numeric.h operators.h power.h print.h pseries.h ptr.h \
-  registrar.h relational.h structure.h symbol.h symmetry.h tensor.h tinfos.h \
-  version.h wildcard.h
+ginacinclude_HEADERS = ginac.h add.h archive.h assertion.h basic.h class_info.h \
+  clifford.h color.h constant.h container.h ex.h expair.h expairseq.h exprseq.h \
+  fail.h fderivative.h flags.h function.h idx.h indexed.h inifcns.h lst.h \
+  matrix.h mul.h ncmul.h normal.h numeric.h operators.h power.h print.h \
+  pseries.h ptr.h registrar.h relational.h structure.h symbol.h symmetry.h \
+  tensor.h tinfos.h version.h wildcard.h
 LFLAGS = -Pginac_yy -olex.yy.c
 YFLAGS = -p ginac_yy -d
 EXTRA_DIST = function.pl input_parser.h version.h.in
index 1d81c6e19bc2f6ced70962e468fa7bac5ad824ab..90e3faffe2cff79ce3d838d85493bc039a683f94 100644 (file)
@@ -237,9 +237,9 @@ inline bool is_a(const basic &obj)
  *  inefficient default.  It should in all time-critical cases be overridden
  *  by template specializations that use the TINFO_* constants directly. */
 template <class T>
-inline bool is_exactly_a(const class basic &obj)
+inline bool is_exactly_a(const basic &obj)
 {
-       return obj.tinfo() == T::reg_info.tinfo_key;
+       return obj.tinfo() == T::reg_info.options.get_id();
 }
 
 } // namespace GiNaC
diff --git a/ginac/class_info.h b/ginac/class_info.h
new file mode 100644 (file)
index 0000000..808a987
--- /dev/null
@@ -0,0 +1,187 @@
+/** @file class_info.h
+ *
+ *  Helper templates to provide per-class information for class hierarchies. */
+
+/*
+ *  GiNaC Copyright (C) 1999-2003 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
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __GINAC_CLASS_INFO_H__
+#define __GINAC_CLASS_INFO_H__
+
+#include <cstddef> // for size_t
+#include <vector>
+#include <map>
+#include <string>
+#include <iostream>
+#include <iomanip>
+
+namespace GiNaC {
+
+// OPT is the class that stores the actual per-class data. It must provide
+// get_name(), get_parent_name() and get_id() members.
+
+template <class OPT>
+class class_info {
+public:
+       class_info(const OPT & o) : options(o), next(first), parent(NULL)
+       {
+               first = this;
+               parents_identified = false;
+       }
+
+       /** Get pointer to class_info of parent class (or NULL). */
+       class_info *get_parent() const
+       {
+               identify_parents();
+               return parent;
+       }
+
+       /** Find class_info by name. */
+       static const class_info *find(const std::string &class_name)
+       {
+               // Use a map for faster lookup. The registered_class_info list doesn't
+               // change at run-time, so it's sufficient to construct the map once
+               // on the first trip through this function.
+               typedef std::map<std::string, const class_info *> name_map_type;
+               static name_map_type name_map;
+               static bool name_map_initialized = false;
+
+               if (!name_map_initialized) {
+                       // Construct map
+                       const class_info *p = first;
+                       while (p) {
+                               name_map[p->options.get_name()] = p;
+                               p = p->next;
+                       }
+                       name_map_initialized = true;
+               }
+
+               typename name_map_type::const_iterator it = name_map.find(class_name);
+               if (it == name_map.end())
+                       throw (std::runtime_error("class '" + class_name + "' not registered"));
+               else
+                       return it->second;
+       }
+
+private:
+       struct tree_node {
+               tree_node(class_info *i) : info(i) {}
+               void add_child(tree_node *n) { children.push_back(n); }
+
+               std::vector<tree_node *> children;
+               class_info *info;
+       };
+
+       static void dump_tree(tree_node *n, const std::string & prefix, bool verbose)
+       {
+               std::string name = n->info->options.get_name();
+               std::cout << name;
+               if (verbose)
+                       std::cout << " [ID 0x" << std::hex << std::setw(8) << std::setfill('0') << n->info->options.get_id() << std::dec << "]" << std::endl;
+
+               size_t num_children = n->children.size();
+               if (num_children) {
+                       for (size_t i = 0; i < num_children; ++i) {
+                               if (verbose) {
+                                       std::cout << prefix << " +- ";
+                                       if (i == num_children - 1)
+                                               dump_tree(n->children[i], prefix + "    ", verbose);
+                                       else
+                                               dump_tree(n->children[i], prefix + " |  ", verbose);
+                               } else {
+                                       std::string spaces(name.size(), ' ');
+                                       if (i > 0)
+                                               std::cout << prefix << spaces;
+                                       if (num_children == 1)
+                                               std::cout << " --- ";
+                                       else if (i > 0)
+                                               std::cout << "  +- ";
+                                       else
+                                               std::cout << " -+- ";
+                                       if (i == num_children - 1)
+                                               dump_tree(n->children[i], prefix + spaces + "     ", verbose);
+                                       else
+                                               dump_tree(n->children[i], prefix + spaces + "  |  ", verbose);
+                               }
+                       }
+               } else if (!verbose)
+                       std::cout << std::endl;
+       }
+
+public:
+       /** Dump class hierarchy to std::cout. */
+       static void dump_hierarchy(bool verbose = false)
+       {
+               identify_parents();
+
+               // Create tree nodes for all class_infos
+               std::vector<tree_node> tree;
+               for (class_info *p = first; p; p = p->next)
+                       tree.push_back(tree_node(p));
+
+               // Identify children for all nodes and find the root
+               tree_node *root = NULL;
+               for (typename std::vector<tree_node>::iterator i = tree.begin(); i != tree.end(); ++i) {
+                       class_info *p = i->info->get_parent();
+                       if (p) {
+                               for (typename std::vector<tree_node>::iterator j = tree.begin(); j != tree.end(); ++j) {
+                                       if (j->info == p) {
+                                               j->add_child(&*i);
+                                               break;
+                                       }
+                               }
+                       } else
+                               root = &*i;
+               }
+
+               // Print hierarchy tree starting at the root
+               dump_tree(root, "", verbose);
+       }
+
+       OPT options;
+
+private:
+       static void identify_parents()
+       {
+               if (!parents_identified) {
+                       for (class_info *p = first; p; p = p->next) {
+                               const char *parent_name = p->options.get_parent_name();
+                               for (class_info *q = first; q; q = q->next) {
+                                       if (strcmp(q->options.get_name(), parent_name) == 0) {
+                                               p->parent = q;
+                                               break;
+                                       }
+                               }
+                       }
+                       parents_identified = true;
+               }
+       }
+
+       static class_info *first;
+       class_info *next;
+       mutable class_info *parent;
+
+       static bool parents_identified;
+};
+
+template <class OPT> class_info<OPT> *class_info<OPT>::first = NULL;
+template <class OPT> bool class_info<OPT>::parents_identified = false;
+
+} // namespace GiNaC
+
+#endif // ndef __GINAC_CLASS_INFO_H__
index 7aee59464f4e4851c88811a9a76a5cb615c7b909..c20b0448caf5aae1aabf5d4a769668c79eda75b1 100644 (file)
 
 namespace GiNaC {
 
-registered_class_info *first_registered_class = NULL;
-
-/** Find registered_class_info strucure by class name. */
-static const registered_class_info *find_registered_class_info(const std::string &class_name)
+unsigned find_tinfo_key(const std::string &class_name)
 {
-       // Use a map for faster lookup. The registered_class_info list doesn't
-       // change at run-time, so it's sufficient to construct the map once
-       // on the first trip through this function.
-       typedef std::map<std::string, const registered_class_info *> name_map_type;
-       static name_map_type name_map;
-       static bool name_map_initialized = false;
-
-       if (!name_map_initialized) {
-               // Construct map
-               const registered_class_info *p = first_registered_class;
-               while (p) {
-                       name_map[p->name] = p;
-                       p = p->next;
-               }
-               name_map_initialized = true;
-       }
-
-       name_map_type::const_iterator it = name_map.find(class_name);
-       if (it == name_map.end())
-               throw (std::runtime_error("class '" + class_name + "' not registered"));
-       else
-               return it->second;
-}
-
-unsigned int find_tinfo_key(const std::string &class_name)
-{
-       const registered_class_info *p = find_registered_class_info(class_name);
-       return p->tinfo_key;
+       return registered_class_info::find(class_name)->options.get_id();
 }
 
 unarch_func find_unarch_func(const std::string &class_name)
 {
-       const registered_class_info *p = find_registered_class_info(class_name);
-       return p->unarchive;
+       return registered_class_info::find(class_name)->options.get_unarch_func();
 }
 
-
 } // namespace GiNaC
index 6969f0c23bb66cd5b27ccc7167125000edb0bdc3..e6c083f32f98f72e60824ed3f20b892ab10c6374 100644 (file)
 #include <string>
 #include <list>
 
+#include "class_info.h"
+
 namespace GiNaC {
 
-class registered_class_info;
 class ex;
 class archive_node;
 
@@ -40,35 +41,37 @@ typedef container<std::list> lst;
 typedef ex (*unarch_func)(const archive_node &n, lst &sym_lst);
 
 
-/** Head of list of all registered_class_info structures. */
-extern registered_class_info *first_registered_class;
+/** This structure stores information about a registered GiNaC class. */
+class registered_class_options {
+public:
+       registered_class_options(const char *n, const char *p, unsigned ti, unarch_func f)
+        : name(n), parent_name(p), tinfo_key(ti), unarchive(f) {}
 
+       const char *get_name() const { return name; }
+       const char *get_parent_name() const { return parent_name; }
+       unsigned get_id() const { return tinfo_key; }
+       unarch_func get_unarch_func() const { return unarchive; }
 
-/** This structure stores information about a registered GiNaC class. */
-struct registered_class_info {
-       registered_class_info(const char *n, const char *s, unsigned int k, unarch_func f)
-               : name(n), super(s), tinfo_key(k), unarchive(f)
-       {
-               // Add structure to list
-               next = first_registered_class;
-               first_registered_class = this;
-       }
-
-       registered_class_info *next;  /**< Pointer to next registered_class_info in list. */
-       const char *name;             /**< Class name. */
-       const char *super;            /**< Name of superclass. */
-       unsigned int tinfo_key;       /**< TINFO_* key. */
-       unarch_func unarchive;        /**< Pointer to unarchiving function. */
+private:
+       const char *name;         /**< Class name. */
+       const char *parent_name;  /**< Name of superclass. */
+       unsigned tinfo_key;       /**< TINFO_* key. */
+       unarch_func unarchive;    /**< Pointer to unarchiving function. */
 };
 
+typedef class_info<registered_class_options> registered_class_info;
+
 
 /** Primary macro for inclusion in the declaration of each registered class. */
 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
 public: \
        typedef supername inherited; \
-       \
+       template <class isexaclass> friend bool is_exactly_a(const GiNaC::basic &obj); \
+private: \
        static GiNaC::registered_class_info reg_info; \
-       virtual const char *class_name() const; \
+public: \
+       virtual const GiNaC::registered_class_info &get_class_info() const { return reg_info; } \
+       virtual const char *class_name() const { return reg_info.options.get_name(); } \
        \
        classname(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
        virtual void archive(GiNaC::archive_node &n) const; \
@@ -100,16 +103,18 @@ protected: \
        int compare_same_type(const GiNaC::basic & other) const; \
 private:
 
-/** Macro for inclusion in the implementation of each registered class.
- *  It defines some members that are the same in all classes derived from
- *  'basic'. */
+/** Macro for inclusion in the implementation of each registered class. */
 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
-       GiNaC::registered_class_info classname::reg_info(#classname, #supername, TINFO_##classname, &classname::unarchive); \
-       const char *classname::class_name() const {return reg_info.name;}
+       GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, TINFO_##classname, &classname::unarchive));
+
+/** Macro for inclusion in the implementation of each registered class.
+ *  Additional options can be specified. */
+#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
+       GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, TINFO_##classname, &classname::unarchive).options);
 
 
 /** Find TINFO_* key by class name. */
-extern unsigned int find_tinfo_key(const std::string &class_name);
+extern unsigned find_tinfo_key(const std::string &class_name);
 
 /** Find unarchiving function by class name. */
 extern unarch_func find_unarch_func(const std::string &class_name);
index de45c2aaaeca364e3099486002609011342eca91..e38d457757dc2bd879eeab0bc6ed58a2b3f8a386 100644 (file)
@@ -116,7 +116,7 @@ class structure : public basic, public ComparisonPolicy<T> {
        GINAC_DECLARE_REGISTERED_CLASS(structure, basic)
 
        // helpers
-       static unsigned get_tinfo() { return reg_info.tinfo_key; }
+       static unsigned get_tinfo() { return reg_info.options.tinfo_key; }
        static const char *get_class_name() { return "structure"; }
 
        // constructors
@@ -256,10 +256,7 @@ int structure<T, CP>::compare_same_type(const basic & other) const
 }
 
 template <class T, template <class> class CP>
-registered_class_info structure<T, CP>::reg_info(structure::get_class_name(), "basic", next_structure_tinfo_key++, &structure::unarchive);
-
-template <class T, template <class> class CP>
-const char *structure<T, CP>::class_name() const { return reg_info.name; }
+registered_class_info structure<T, CP>::reg_info = registered_class_info(registered_class_options(structure::get_class_name(), "basic", next_structure_tinfo_key++, &structure::unarchive));
 
 
 } // namespace GiNaC