3 * GiNaC's class registrar (for class basic and all classes derived from it). */
6 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __GINAC_REGISTRAR_H__
24 #define __GINAC_REGISTRAR_H__
30 #include "class_info.h"
38 template <template <class> class> class container;
39 typedef container<std::list> lst;
42 /** Unarchiving function (static member function of every GiNaC class). */
43 typedef ex (*unarch_func)(const archive_node &n, lst &sym_lst);
46 /** This class stores information about a registered GiNaC class. */
47 class registered_class_options {
49 registered_class_options(const char *n, const char *p, unsigned ti, unarch_func f)
50 : name(n), parent_name(p), tinfo_key(ti), unarchive(f) {}
52 const char *get_name() const { return name; }
53 const char *get_parent_name() const { return parent_name; }
54 unsigned get_id() const { return tinfo_key; }
55 unarch_func get_unarch_func() const { return unarchive; }
56 const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
58 template <class Ctx, class T, class C>
59 registered_class_options & print_func(void f(const T &, const C & c, unsigned))
61 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
65 template <class Ctx, class T, class C>
66 registered_class_options & print_func(void (T::*f)(const C &, unsigned))
68 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
73 registered_class_options & print_func(const print_functor & f)
75 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
79 void set_print_func(unsigned id, const print_functor & f)
81 if (id >= print_dispatch_table.size())
82 print_dispatch_table.resize(id + 1);
83 print_dispatch_table[id] = f;
87 const char *name; /**< Class name. */
88 const char *parent_name; /**< Name of superclass. */
89 unsigned tinfo_key; /**< TINFO_* key. */
90 unarch_func unarchive; /**< Pointer to unarchiving function. */
91 std::vector<print_functor> print_dispatch_table; /**< Method table for print() dispatch */
94 typedef class_info<registered_class_options> registered_class_info;
97 /** Primary macro for inclusion in the declaration of each registered class. */
98 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
100 typedef supername inherited; \
102 static GiNaC::registered_class_info reg_info; \
104 static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
105 virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
106 virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
107 virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
109 classname(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
110 virtual void archive(GiNaC::archive_node &n) const; \
111 static GiNaC::ex unarchive(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
115 virtual void visit(const classname &) = 0; \
118 /** Macro for inclusion in the declaration of each registered class.
119 * It declares some functions that are common to all classes derived
120 * from 'basic' as well as all required stuff for the GiNaC class
121 * registry (mainly needed for archiving). */
122 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
123 GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
126 virtual classname * duplicate() const { return new classname(*this); } \
128 virtual void accept(GiNaC::visitor & v) const \
130 if (visitor *p = dynamic_cast<visitor *>(&v)) \
133 inherited::accept(v); \
136 virtual int compare_same_type(const GiNaC::basic & other) const; \
139 /** Macro for inclusion in the implementation of each registered class. */
140 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
141 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, TINFO_##classname, &classname::unarchive));
143 /** Macro for inclusion in the implementation of each registered class.
144 * Additional options can be specified. */
145 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
146 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, TINFO_##classname, &classname::unarchive).options);
149 /** Find TINFO_* key by class name. */
150 extern unsigned find_tinfo_key(const std::string &class_name);
152 /** Find unarchiving function by class name. */
153 extern unarch_func find_unarch_func(const std::string &class_name);
156 /** Add or replace a print method. */
157 template <class Alg, class Ctx, class T, class C>
158 extern void set_print_func(void f(const T &, const C & c, unsigned))
160 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
163 /** Add or replace a print method. */
164 template <class Alg, class Ctx, class T, class C>
165 extern void set_print_func(void (T::*f)(const C &, unsigned))
167 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
173 #endif // ndef __GINAC_REGISTRAR_H__