3 * GiNaC's class registrar (for class basic and all classes derived from it). */
6 * GiNaC Copyright (C) 1999-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef __GINAC_REGISTRAR_H__
24 #define __GINAC_REGISTRAR_H__
30 #include "class_info.h"
38 template <template <class T, class = std::allocator<T> > class> class container;
39 typedef container<std::list> lst;
41 /** Definitions for the tinfo mechanism. */
42 typedef const void * tinfo_t;
43 struct tinfo_static_t {};
45 /** Unarchiving function (static member function of every GiNaC class). */
46 typedef ex (*unarch_func)(const archive_node &n, lst &sym_lst);
49 /** This class stores information about a registered GiNaC class. */
50 class registered_class_options {
52 registered_class_options(const char *n, const char *p, tinfo_t ti, unarch_func f)
53 : name(n), parent_name(p), tinfo_key(ti), unarchive(f) {}
55 const char *get_name() const { return name; }
56 const char *get_parent_name() const { return parent_name; }
57 tinfo_t get_id() const { return tinfo_key; }
58 unarch_func get_unarch_func() const { return unarchive; }
59 const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
61 template <class Ctx, class T, class C>
62 registered_class_options & print_func(void f(const T &, const C & c, unsigned))
64 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
68 template <class Ctx, class T, class C>
69 registered_class_options & print_func(void (T::*f)(const C &, unsigned))
71 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
76 registered_class_options & print_func(const print_functor & f)
78 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
82 void set_print_func(unsigned id, const print_functor & f)
84 if (id >= print_dispatch_table.size())
85 print_dispatch_table.resize(id + 1);
86 print_dispatch_table[id] = f;
90 const char *name; /**< Class name. */
91 const char *parent_name; /**< Name of superclass. */
92 tinfo_t tinfo_key; /**< Type information key. */
93 unarch_func unarchive; /**< Pointer to unarchiving function. */
94 std::vector<print_functor> print_dispatch_table; /**< Method table for print() dispatch */
97 typedef class_info<registered_class_options> registered_class_info;
100 /** Primary macro for inclusion in the declaration of each registered class. */
101 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
103 typedef supername inherited; \
104 static const GiNaC::tinfo_static_t tinfo_static; \
106 static GiNaC::registered_class_info reg_info; \
108 static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
109 virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
110 virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
111 virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
113 classname(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
114 virtual void archive(GiNaC::archive_node &n) const; \
115 static GiNaC::ex unarchive(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
119 virtual void visit(const classname &) = 0; \
120 virtual ~visitor() {}; \
123 /** Macro for inclusion in the declaration of each registered class.
124 * It declares some functions that are common to all classes derived
125 * from 'basic' as well as all required stuff for the GiNaC class
126 * registry (mainly needed for archiving). */
127 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
128 GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
131 virtual classname * duplicate() const { return new classname(*this); } \
133 virtual void accept(GiNaC::visitor & v) const \
135 if (visitor *p = dynamic_cast<visitor *>(&v)) \
138 inherited::accept(v); \
141 virtual int compare_same_type(const GiNaC::basic & other) const; \
145 /** Macro for inclusion in the implementation of each registered class. */
146 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
147 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive)); \
148 const GiNaC::tinfo_static_t classname::tinfo_static = {};
150 /** Macro for inclusion in the implementation of each registered class.
151 * Additional options can be specified. */
152 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
153 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive).options); \
154 const GiNaC::tinfo_static_t classname::tinfo_static = {};
156 /** Macro for inclusion in the implementation of each registered class.
157 * Additional options can be specified. */
158 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(classname, supername, options) \
159 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive).options); \
160 template<> const GiNaC::tinfo_static_t classname::tinfo_static = {};
163 /** Find type information key by class name. */
164 extern tinfo_t find_tinfo_key(const std::string &class_name);
166 /** Find unarchiving function by class name. */
167 extern unarch_func find_unarch_func(const std::string &class_name);
170 /** Add or replace a print method. */
171 template <class Alg, class Ctx, class T, class C>
172 extern void set_print_func(void f(const T &, const C & c, unsigned))
174 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
177 /** Add or replace a print method. */
178 template <class Alg, class Ctx, class T, class C>
179 extern void set_print_func(void (T::*f)(const C &, unsigned))
181 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
187 #endif // ndef __GINAC_REGISTRAR_H__