GiNaC 1.8.10
registrar.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 Johannes Gutenberg University Mainz, Germany
7 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef GINAC_REGISTRAR_H
23#define GINAC_REGISTRAR_H
24
25#include "class_info.h"
26#include "print.h"
27
28#include <list>
29#include <typeinfo>
30#include <vector>
31
32namespace GiNaC {
33
34class ex;
35class archive_node;
36
37template <template <class T, class = std::allocator<T>> class> class container;
38typedef container<std::list> lst;
39
42{
44 std::type_info const* tinfo;
47 unsigned rl;
48
51 inline bool operator<(const return_type_t& other) const
52 {
53 if (tinfo->before(*other.tinfo))
54 return true;
55 return rl < other.rl;
56 }
57 inline bool operator==(const return_type_t& other) const
58 {
59 if (*tinfo != *(other.tinfo))
60 return false;
61 return rl == other.rl;
62 }
63 inline bool operator!=(const return_type_t& other) const
64 {
65 return ! (operator==(other));
66 }
67};
68
69template<typename T> inline return_type_t make_return_type_t(const unsigned rl = 0)
70{
71 return_type_t ret;
72 ret.rl = rl;
73 ret.tinfo = &typeid(T);
74 return ret;
75}
76
79public:
80 registered_class_options(const char *n, const char *p,
81 const std::type_info& ti)
82 : name(n), parent_name(p), tinfo_key(&ti) { }
83
84 const char *get_name() const { return name; }
85 const char *get_parent_name() const { return parent_name; }
86 std::type_info const* get_id() const { return tinfo_key; }
87 const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
88
89 template <class Ctx, class T, class C>
90 registered_class_options & print_func(void f(const T &, const C & c, unsigned))
91 {
92 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
93 return *this;
94 }
95
96 template <class Ctx, class T, class C>
98 {
99 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
100 return *this;
101 }
102
103 template <class Ctx>
105 {
106 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
107 return *this;
108 }
109
110 void set_print_func(unsigned id, const print_functor & f)
111 {
112 if (id >= print_dispatch_table.size())
113 print_dispatch_table.resize(id + 1);
114 print_dispatch_table[id] = f;
115 }
116
117private:
118 const char *name;
119 const char *parent_name;
120 std::type_info const* tinfo_key;
121 std::vector<print_functor> print_dispatch_table;
122};
123
125
127#define GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
128private: \
129 static GiNaC::registered_class_info reg_info; \
130public: \
131 static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
132 class visitor { \
133 public: \
134 virtual void visit(const classname &) = 0; \
135 virtual ~visitor() {}; \
136 };
137
139#define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
140 GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
141 typedef supername inherited; \
142 virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
143 virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
144 virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
145private:
146
151#define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
152 GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
153 template<class B, typename... Args> friend B & dynallocate(Args &&... args); \
154 typedef supername inherited; \
155 classname(); \
156 classname * duplicate() const override { \
157 classname * bp = new classname(*this); \
158 bp->setflag(GiNaC::status_flags::dynallocated); \
159 return bp; \
160 } \
161 \
162 void accept(GiNaC::visitor & v) const override \
163 { \
164 if (visitor *p = dynamic_cast<visitor *>(&v)) \
165 p->visit(*this); \
166 else \
167 inherited::accept(v); \
168 } \
169 const GiNaC::registered_class_info &get_class_info() const override { return classname::get_class_info_static(); } \
170 GiNaC::registered_class_info &get_class_info() override { return classname::get_class_info_static(); } \
171 const char *class_name() const override { return classname::get_class_info_static().options.get_name(); } \
172protected: \
173 int compare_same_type(const GiNaC::basic & other) const override; \
174private:
175
176
178#define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
179 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)));
180
183#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
184 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)).options);
185
188#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(classname, supername, options) \
189 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)).options);
190
191
193template <class Alg, class Ctx, class T, class C>
194extern void set_print_func(void f(const T &, const C & c, unsigned))
195{
196 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
197}
198
200template <class Alg, class Ctx, class T, class C>
201extern void set_print_func(void (T::*f)(const C &, unsigned))
202{
203 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
204}
205
206
207} // namespace GiNaC
208
209#endif // ndef GINAC_REGISTRAR_H
This class represents a print method for a certain algebraic class and print_context type.
Definition print.h:246
This class stores information about a registered GiNaC class.
Definition registrar.h:78
const char * name
Class name.
Definition registrar.h:118
const std::vector< print_functor > & get_print_dispatch_table() const
Definition registrar.h:87
std::type_info const * tinfo_key
Type information key.
Definition registrar.h:120
const char * get_parent_name() const
Definition registrar.h:85
registered_class_options(const char *n, const char *p, const std::type_info &ti)
Definition registrar.h:80
registered_class_options & print_func(void f(const T &, const C &c, unsigned))
Definition registrar.h:90
const char * parent_name
Name of superclass.
Definition registrar.h:119
registered_class_options & print_func(void(T::*f)(const C &, unsigned))
Definition registrar.h:97
registered_class_options & print_func(const print_functor &f)
Definition registrar.h:104
const char * get_name() const
Definition registrar.h:84
void set_print_func(unsigned id, const print_functor &f)
Definition registrar.h:110
std::vector< print_functor > print_dispatch_table
Method table for print() dispatch.
Definition registrar.h:121
std::type_info const * get_id() const
Definition registrar.h:86
Helper templates to provide per-class information for class hierarchies.
unsigned options
Definition factor.cpp:2473
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
Definition add.cpp:35
container< std::list > lst
Definition lst.h:31
class_info< registered_class_options > registered_class_info
Definition registrar.h:124
return_type_t make_return_type_t(const unsigned rl=0)
Definition registrar.h:69
void set_print_func(void f(const T &, const C &c, unsigned))
Add or replace a print method.
Definition registrar.h:194
Definition of helper classes for expression output.
To distinguish between different kinds of non-commutative objects.
Definition registrar.h:42
std::type_info const * tinfo
to distinguish between non-commutative objects of different type.
Definition registrar.h:44
bool operator!=(const return_type_t &other) const
Definition registrar.h:63
bool operator==(const return_type_t &other) const
Definition registrar.h:57
unsigned rl
to distinguish between non-commutative objects of the same type.
Definition registrar.h:47
bool operator<(const return_type_t &other) const
Strict weak ordering (so one can put return_type_t's into a STL container).
Definition registrar.h:51

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.