3 * GiNaC's class registrar (for class basic and all classes derived from it). */
6 * GiNaC Copyright (C) 1999-2008 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 /** To distinguish between different kinds of non-commutative objects */
44 /// to distinguish between non-commutative objects of different type.
45 std::type_info const* tinfo;
46 /// to distinguish between non-commutative objects of the same type.
47 /// Think of gamma matrices with different represenation labels.
50 /// Strict weak ordering (so one can put return_type_t's into
52 inline bool operator<(const return_type_t& other) const
54 if (tinfo->before(*other.tinfo))
58 inline bool operator==(const return_type_t& other) const
60 if (*tinfo != *(other.tinfo))
62 return rl == other.rl;
64 inline bool operator!=(const return_type_t& other) const
66 return ! (operator==(other));
70 template<typename T> inline return_type_t make_return_type_t(const unsigned rl = 0)
74 ret.tinfo = &typeid(T);
78 /** Definitions for the tinfo mechanism. */
79 typedef const void * tinfo_t;
80 struct tinfo_static_t {};
82 /** Unarchiving function (static member function of every GiNaC class). */
83 typedef ex (*unarch_func)(const archive_node &n, lst &sym_lst);
86 /** This class stores information about a registered GiNaC class. */
87 class registered_class_options {
89 registered_class_options(const char *n, const char *p, tinfo_t ti, unarch_func f)
90 : name(n), parent_name(p), tinfo_key(ti), unarchive(f) {}
92 const char *get_name() const { return name; }
93 const char *get_parent_name() const { return parent_name; }
94 tinfo_t get_id() const { return tinfo_key; }
95 unarch_func get_unarch_func() const { return unarchive; }
96 const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
98 template <class Ctx, class T, class C>
99 registered_class_options & print_func(void f(const T &, const C & c, unsigned))
101 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
105 template <class Ctx, class T, class C>
106 registered_class_options & print_func(void (T::*f)(const C &, unsigned))
108 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
113 registered_class_options & print_func(const print_functor & f)
115 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
119 void set_print_func(unsigned id, const print_functor & f)
121 if (id >= print_dispatch_table.size())
122 print_dispatch_table.resize(id + 1);
123 print_dispatch_table[id] = f;
127 const char *name; /**< Class name. */
128 const char *parent_name; /**< Name of superclass. */
129 tinfo_t tinfo_key; /**< Type information key. */
130 unarch_func unarchive; /**< Pointer to unarchiving function. */
131 std::vector<print_functor> print_dispatch_table; /**< Method table for print() dispatch */
134 typedef class_info<registered_class_options> registered_class_info;
137 /** Primary macro for inclusion in the declaration of each registered class. */
138 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
140 typedef supername inherited; \
141 static const GiNaC::tinfo_static_t tinfo_static; \
143 static GiNaC::registered_class_info reg_info; \
145 static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
146 virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
147 virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
148 virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
150 classname(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
151 virtual void archive(GiNaC::archive_node &n) const; \
152 static GiNaC::ex unarchive(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
156 virtual void visit(const classname &) = 0; \
157 virtual ~visitor() {}; \
160 /** Macro for inclusion in the declaration of each registered class.
161 * It declares some functions that are common to all classes derived
162 * from 'basic' as well as all required stuff for the GiNaC class
163 * registry (mainly needed for archiving). */
164 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
165 GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
168 virtual classname * duplicate() const { return new classname(*this); } \
170 virtual void accept(GiNaC::visitor & v) const \
172 if (visitor *p = dynamic_cast<visitor *>(&v)) \
175 inherited::accept(v); \
178 virtual int compare_same_type(const GiNaC::basic & other) const; \
182 /** Macro for inclusion in the implementation of each registered class. */
183 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
184 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive)); \
185 const GiNaC::tinfo_static_t classname::tinfo_static = {};
187 /** Macro for inclusion in the implementation of each registered class.
188 * Additional options can be specified. */
189 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
190 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive).options); \
191 const GiNaC::tinfo_static_t classname::tinfo_static = {};
193 /** Macro for inclusion in the implementation of each registered class.
194 * Additional options can be specified. */
195 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(classname, supername, options) \
196 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive).options); \
197 template<> const GiNaC::tinfo_static_t classname::tinfo_static = {};
200 /** Find type information key by class name. */
201 extern tinfo_t find_tinfo_key(const std::string &class_name);
203 /** Find unarchiving function by class name. */
204 extern unarch_func find_unarch_func(const std::string &class_name);
207 /** Add or replace a print method. */
208 template <class Alg, class Ctx, class T, class C>
209 extern void set_print_func(void f(const T &, const C & c, unsigned))
211 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
214 /** Add or replace a print method. */
215 template <class Alg, class Ctx, class T, class C>
216 extern void set_print_func(void (T::*f)(const C &, unsigned))
218 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
224 #endif // ndef __GINAC_REGISTRAR_H__