3 * GiNaC's class registrar (for class basic and all classes derived from it). */
6 * GiNaC Copyright (C) 1999-2020 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
26 #include "class_info.h"
39 template <template <class T, class = std::allocator<T>> class> class container;
40 typedef container<std::list> lst;
42 /** To distinguish between different kinds of non-commutative objects */
45 /// to distinguish between non-commutative objects of different type.
46 std::type_info const* tinfo;
47 /// to distinguish between non-commutative objects of the same type.
48 /// Think of gamma matrices with different representation labels.
51 /// Strict weak ordering (so one can put return_type_t's into
53 inline bool operator<(const return_type_t& other) const
55 if (tinfo->before(*other.tinfo))
59 inline bool operator==(const return_type_t& other) const
61 if (*tinfo != *(other.tinfo))
63 return rl == other.rl;
65 inline bool operator!=(const return_type_t& other) const
67 return ! (operator==(other));
71 template<typename T> inline return_type_t make_return_type_t(const unsigned rl = 0)
75 ret.tinfo = &typeid(T);
79 /** This class stores information about a registered GiNaC class. */
80 class registered_class_options {
82 registered_class_options(const char *n, const char *p,
83 const std::type_info& ti)
84 : name(n), parent_name(p), tinfo_key(&ti) { }
86 const char *get_name() const { return name; }
87 const char *get_parent_name() const { return parent_name; }
88 std::type_info const* get_id() const { return tinfo_key; }
89 const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
91 template <class Ctx, class T, class C>
92 registered_class_options & print_func(void f(const T &, const C & c, unsigned))
94 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
98 template <class Ctx, class T, class C>
99 registered_class_options & print_func(void (T::*f)(const C &, unsigned))
101 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
106 registered_class_options & print_func(const print_functor & f)
108 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
112 void set_print_func(unsigned id, const print_functor & f)
114 if (id >= print_dispatch_table.size())
115 print_dispatch_table.resize(id + 1);
116 print_dispatch_table[id] = f;
120 const char *name; /**< Class name. */
121 const char *parent_name; /**< Name of superclass. */
122 std::type_info const* tinfo_key; /**< Type information key. */
123 std::vector<print_functor> print_dispatch_table; /**< Method table for print() dispatch */
126 typedef class_info<registered_class_options> registered_class_info;
128 /** Common part of GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS and GINAC_DECLARE_REGISTERED_CLASS. */
129 #define GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
131 static GiNaC::registered_class_info reg_info; \
133 static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
136 virtual void visit(const classname &) = 0; \
137 virtual ~visitor() {}; \
140 /** Primary macro for inclusion in the declaration of each registered class. */
141 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
142 GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
143 typedef supername inherited; \
144 virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
145 virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
146 virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
149 /** Macro for inclusion in the declaration of each registered class.
150 * It declares some functions that are common to all classes derived
151 * from 'basic' as well as all required stuff for the GiNaC class
152 * registry (mainly needed for archiving). */
153 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
154 GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
155 template<class B, typename... Args> friend B & dynallocate(Args &&... args); \
156 typedef supername inherited; \
158 classname * duplicate() const override { \
159 classname * bp = new classname(*this); \
160 bp->setflag(GiNaC::status_flags::dynallocated); \
164 void accept(GiNaC::visitor & v) const override \
166 if (visitor *p = dynamic_cast<visitor *>(&v)) \
169 inherited::accept(v); \
171 const GiNaC::registered_class_info &get_class_info() const override { return classname::get_class_info_static(); } \
172 GiNaC::registered_class_info &get_class_info() override { return classname::get_class_info_static(); } \
173 const char *class_name() const override { return classname::get_class_info_static().options.get_name(); } \
175 int compare_same_type(const GiNaC::basic & other) const override; \
179 /** Macro for inclusion in the implementation of each registered class. */
180 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
181 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)));
183 /** Macro for inclusion in the implementation of each registered class.
184 * Additional options can be specified. */
185 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
186 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)).options);
188 /** Macro for inclusion in the implementation of each registered class.
189 * Additional options can be specified. */
190 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(classname, supername, options) \
191 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)).options);
194 /** Add or replace a print method. */
195 template <class Alg, class Ctx, class T, class C>
196 extern void set_print_func(void f(const T &, const C & c, unsigned))
198 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
201 /** Add or replace a print method. */
202 template <class Alg, class Ctx, class T, class C>
203 extern void set_print_func(void (T::*f)(const C &, unsigned))
205 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
211 #endif // ndef GINAC_REGISTRAR_H