GiNaC 1.8.8
registrar.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2025 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, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef GINAC_REGISTRAR_H
24#define GINAC_REGISTRAR_H
25
26#include "class_info.h"
27#include "print.h"
28
29#include <list>
30#include <typeinfo>
31#include <vector>
32
33namespace GiNaC {
34
35class ex;
36class archive_node;
37
38template <template <class T, class = std::allocator<T>> class> class container;
39typedef container<std::list> lst;
40
43{
45 std::type_info const* tinfo;
48 unsigned rl;
49
52 inline bool operator<(const return_type_t& other) const
53 {
54 if (tinfo->before(*other.tinfo))
55 return true;
56 return rl < other.rl;
57 }
58 inline bool operator==(const return_type_t& other) const
59 {
60 if (*tinfo != *(other.tinfo))
61 return false;
62 return rl == other.rl;
63 }
64 inline bool operator!=(const return_type_t& other) const
65 {
66 return ! (operator==(other));
67 }
68};
69
70template<typename T> inline return_type_t make_return_type_t(const unsigned rl = 0)
71{
72 return_type_t ret;
73 ret.rl = rl;
74 ret.tinfo = &typeid(T);
75 return ret;
76}
77
80public:
81 registered_class_options(const char *n, const char *p,
82 const std::type_info& ti)
83 : name(n), parent_name(p), tinfo_key(&ti) { }
84
85 const char *get_name() const { return name; }
86 const char *get_parent_name() const { return parent_name; }
87 std::type_info const* get_id() const { return tinfo_key; }
88 const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
89
90 template <class Ctx, class T, class C>
91 registered_class_options & print_func(void f(const T &, const C & c, unsigned))
92 {
93 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
94 return *this;
95 }
96
97 template <class Ctx, class T, class C>
99 {
100 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
101 return *this;
102 }
103
104 template <class Ctx>
106 {
107 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
108 return *this;
109 }
110
111 void set_print_func(unsigned id, const print_functor & f)
112 {
113 if (id >= print_dispatch_table.size())
114 print_dispatch_table.resize(id + 1);
115 print_dispatch_table[id] = f;
116 }
117
118private:
119 const char *name;
120 const char *parent_name;
121 std::type_info const* tinfo_key;
122 std::vector<print_functor> print_dispatch_table;
123};
124
126
128#define GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
129private: \
130 static GiNaC::registered_class_info reg_info; \
131public: \
132 static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
133 class visitor { \
134 public: \
135 virtual void visit(const classname &) = 0; \
136 virtual ~visitor() {}; \
137 };
138
140#define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
141 GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
142 typedef supername inherited; \
143 virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
144 virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
145 virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
146private:
147
152#define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
153 GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
154 template<class B, typename... Args> friend B & dynallocate(Args &&... args); \
155 typedef supername inherited; \
156 classname(); \
157 classname * duplicate() const override { \
158 classname * bp = new classname(*this); \
159 bp->setflag(GiNaC::status_flags::dynallocated); \
160 return bp; \
161 } \
162 \
163 void accept(GiNaC::visitor & v) const override \
164 { \
165 if (visitor *p = dynamic_cast<visitor *>(&v)) \
166 p->visit(*this); \
167 else \
168 inherited::accept(v); \
169 } \
170 const GiNaC::registered_class_info &get_class_info() const override { return classname::get_class_info_static(); } \
171 GiNaC::registered_class_info &get_class_info() override { return classname::get_class_info_static(); } \
172 const char *class_name() const override { return classname::get_class_info_static().options.get_name(); } \
173protected: \
174 int compare_same_type(const GiNaC::basic & other) const override; \
175private:
176
177
179#define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
180 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)));
181
184#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
185 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)).options);
186
189#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(classname, supername, options) \
190 GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname)).options);
191
192
194template <class Alg, class Ctx, class T, class C>
195extern void set_print_func(void f(const T &, const C & c, unsigned))
196{
197 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
198}
199
201template <class Alg, class Ctx, class T, class C>
202extern void set_print_func(void (T::*f)(const C &, unsigned))
203{
204 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
205}
206
207
208} // namespace GiNaC
209
210#endif // ndef GINAC_REGISTRAR_H
This class represents a print method for a certain algebraic class and print_context type.
Definition print.h:247
This class stores information about a registered GiNaC class.
Definition registrar.h:79
const char * name
Class name.
Definition registrar.h:119
const std::vector< print_functor > & get_print_dispatch_table() const
Definition registrar.h:88
std::type_info const * tinfo_key
Type information key.
Definition registrar.h:121
const char * get_parent_name() const
Definition registrar.h:86
registered_class_options(const char *n, const char *p, const std::type_info &ti)
Definition registrar.h:81
registered_class_options & print_func(void f(const T &, const C &c, unsigned))
Definition registrar.h:91
const char * parent_name
Name of superclass.
Definition registrar.h:120
registered_class_options & print_func(void(T::*f)(const C &, unsigned))
Definition registrar.h:98
registered_class_options & print_func(const print_functor &f)
Definition registrar.h:105
const char * get_name() const
Definition registrar.h:85
void set_print_func(unsigned id, const print_functor &f)
Definition registrar.h:111
std::vector< print_functor > print_dispatch_table
Method table for print() dispatch.
Definition registrar.h:122
std::type_info const * get_id() const
Definition registrar.h:87
Helper templates to provide per-class information for class hierarchies.
unsigned options
Definition factor.cpp:2474
size_t n
Definition factor.cpp:1432
size_t c
Definition factor.cpp:757
Definition add.cpp:36
container< std::list > lst
Definition lst.h:32
class_info< registered_class_options > registered_class_info
Definition registrar.h:125
return_type_t make_return_type_t(const unsigned rl=0)
Definition registrar.h:70
void set_print_func(void f(const T &, const C &c, unsigned))
Add or replace a print method.
Definition registrar.h:195
Definition of helper classes for expression output.
To distinguish between different kinds of non-commutative objects.
Definition registrar.h:43
std::type_info const * tinfo
to distinguish between non-commutative objects of different type.
Definition registrar.h:45
bool operator!=(const return_type_t &other) const
Definition registrar.h:64
bool operator==(const return_type_t &other) const
Definition registrar.h:58
unsigned rl
to distinguish between non-commutative objects of the same type.
Definition registrar.h:48
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:52

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