GiNaC 1.8.7
registrar.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2023 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 <string>
31#include <typeinfo>
32#include <vector>
33
34namespace GiNaC {
35
36class ex;
37class archive_node;
38
39template <template <class T, class = std::allocator<T>> class> class container;
40typedef container<std::list> lst;
41
44{
46 std::type_info const* tinfo;
49 unsigned rl;
50
53 inline bool operator<(const return_type_t& other) const
54 {
55 if (tinfo->before(*other.tinfo))
56 return true;
57 return rl < other.rl;
58 }
59 inline bool operator==(const return_type_t& other) const
60 {
61 if (*tinfo != *(other.tinfo))
62 return false;
63 return rl == other.rl;
64 }
65 inline bool operator!=(const return_type_t& other) const
66 {
67 return ! (operator==(other));
68 }
69};
70
71template<typename T> inline return_type_t make_return_type_t(const unsigned rl = 0)
72{
73 return_type_t ret;
74 ret.rl = rl;
75 ret.tinfo = &typeid(T);
76 return ret;
77}
78
81public:
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) { }
85
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; }
90
91 template <class Ctx, class T, class C>
92 registered_class_options & print_func(void f(const T &, const C & c, unsigned))
93 {
94 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
95 return *this;
96 }
97
98 template <class Ctx, class T, class C>
99 registered_class_options & print_func(void (T::*f)(const C &, unsigned))
100 {
101 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
102 return *this;
103 }
104
105 template <class Ctx>
107 {
108 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
109 return *this;
110 }
111
112 void set_print_func(unsigned id, const print_functor & f)
113 {
114 if (id >= print_dispatch_table.size())
115 print_dispatch_table.resize(id + 1);
116 print_dispatch_table[id] = f;
117 }
118
119private:
120 const char *name;
121 const char *parent_name;
122 std::type_info const* tinfo_key;
123 std::vector<print_functor> print_dispatch_table;
124};
125
127
129#define GINAC_DECLARE_REGISTERED_CLASS_COMMON(classname) \
130private: \
131 static GiNaC::registered_class_info reg_info; \
132public: \
133 static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
134 class visitor { \
135 public: \
136 virtual void visit(const classname &) = 0; \
137 virtual ~visitor() {}; \
138 };
139
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(); } \
147private:
148
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; \
157 classname(); \
158 classname * duplicate() const override { \
159 classname * bp = new classname(*this); \
160 bp->setflag(GiNaC::status_flags::dynallocated); \
161 return bp; \
162 } \
163 \
164 void accept(GiNaC::visitor & v) const override \
165 { \
166 if (visitor *p = dynamic_cast<visitor *>(&v)) \
167 p->visit(*this); \
168 else \
169 inherited::accept(v); \
170 } \
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(); } \
174protected: \
175 int compare_same_type(const GiNaC::basic & other) const override; \
176private:
177
178
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)));
182
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);
187
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);
192
193
195template <class Alg, class Ctx, class T, class C>
196extern void set_print_func(void f(const T &, const C & c, unsigned))
197{
198 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
199}
200
202template <class Alg, class Ctx, class T, class C>
203extern void set_print_func(void (T::*f)(const C &, unsigned))
204{
205 Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
206}
207
208
209} // namespace GiNaC
210
211#endif // ndef GINAC_REGISTRAR_H
This class represents a print method for a certain algebraic class and print_context type.
Definition: print.h:248
This class stores information about a registered GiNaC class.
Definition: registrar.h:80
const char * name
Class name.
Definition: registrar.h:120
const std::vector< print_functor > & get_print_dispatch_table() const
Definition: registrar.h:89
std::type_info const * tinfo_key
Type information key.
Definition: registrar.h:122
const char * get_parent_name() const
Definition: registrar.h:87
registered_class_options(const char *n, const char *p, const std::type_info &ti)
Definition: registrar.h:82
registered_class_options & print_func(void f(const T &, const C &c, unsigned))
Definition: registrar.h:92
const char * parent_name
Name of superclass.
Definition: registrar.h:121
registered_class_options & print_func(void(T::*f)(const C &, unsigned))
Definition: registrar.h:99
registered_class_options & print_func(const print_functor &f)
Definition: registrar.h:106
const char * get_name() const
Definition: registrar.h:86
void set_print_func(unsigned id, const print_functor &f)
Definition: registrar.h:112
std::vector< print_functor > print_dispatch_table
Method table for print() dispatch.
Definition: registrar.h:123
std::type_info const * get_id() const
Definition: registrar.h:88
Helper templates to provide per-class information for class hierarchies.
unsigned options
Definition: factor.cpp:2475
size_t n
Definition: factor.cpp:1432
size_t c
Definition: factor.cpp:757
Definition: add.cpp:38
container< std::list > lst
Definition: lst.h:32
class_info< registered_class_options > registered_class_info
Definition: registrar.h:126
return_type_t make_return_type_t(const unsigned rl=0)
Definition: registrar.h:71
void set_print_func(void f(const T &, const C &c, unsigned))
Add or replace a print method.
Definition: registrar.h:196
Definition of helper classes for expression output.
To distinguish between different kinds of non-commutative objects.
Definition: registrar.h:44
std::type_info const * tinfo
to distinguish between non-commutative objects of different type.
Definition: registrar.h:46
bool operator!=(const return_type_t &other) const
Definition: registrar.h:65
bool operator==(const return_type_t &other) const
Definition: registrar.h:59
unsigned rl
to distinguish between non-commutative objects of the same type.
Definition: registrar.h:49
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:53

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