]> www.ginac.de Git - ginac.git/blob - ginac/registrar.h
Replaced class UniPoly by cl_UP_MI.
[ginac.git] / ginac / registrar.h
1 /** @file registrar.h
2  *
3  *  GiNaC's class registrar (for class basic and all classes derived from it). */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 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 <string>
27 #include <list>
28 #include <vector>
29
30 #include "class_info.h"
31 #include "print.h"
32
33 namespace GiNaC {
34
35 class ex;
36 class archive_node;
37
38 template <template <class T, class = std::allocator<T> > class> class container;
39 typedef container<std::list> lst;
40
41 /** Definitions for the tinfo mechanism. */
42 typedef const void * tinfo_t;
43 struct tinfo_static_t {};
44
45 /** Unarchiving function (static member function of every GiNaC class). */
46 typedef ex (*unarch_func)(const archive_node &n, lst &sym_lst);
47
48
49 /** This class stores information about a registered GiNaC class. */
50 class registered_class_options {
51 public:
52         registered_class_options(const char *n, const char *p, tinfo_t ti, unarch_func f)
53          : name(n), parent_name(p), tinfo_key(ti), unarchive(f) {}
54
55         const char *get_name() const { return name; }
56         const char *get_parent_name() const { return parent_name; }
57         tinfo_t get_id() const { return tinfo_key; }
58         unarch_func get_unarch_func() const { return unarchive; }
59         const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
60
61         template <class Ctx, class T, class C>
62         registered_class_options & print_func(void f(const T &, const C & c, unsigned))
63         {
64                 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
65                 return *this;
66         }
67
68         template <class Ctx, class T, class C>
69         registered_class_options & print_func(void (T::*f)(const C &, unsigned))
70         {
71                 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
72                 return *this;
73         }
74
75         template <class Ctx>
76         registered_class_options & print_func(const print_functor & f)
77         {
78                 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
79                 return *this;
80         }
81
82         void set_print_func(unsigned id, const print_functor & f)
83         {
84                 if (id >= print_dispatch_table.size())
85                         print_dispatch_table.resize(id + 1);
86                 print_dispatch_table[id] = f;
87         }
88
89 private:
90         const char *name;         /**< Class name. */
91         const char *parent_name;  /**< Name of superclass. */
92         tinfo_t tinfo_key;        /**< Type information key. */
93         unarch_func unarchive;    /**< Pointer to unarchiving function. */
94         std::vector<print_functor> print_dispatch_table; /**< Method table for print() dispatch */
95 };
96
97 typedef class_info<registered_class_options> registered_class_info;
98
99
100 /** Primary macro for inclusion in the declaration of each registered class. */
101 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
102 public: \
103         typedef supername inherited; \
104     static const GiNaC::tinfo_static_t tinfo_static; \
105 private: \
106         static GiNaC::registered_class_info reg_info; \
107 public: \
108         static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
109         virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
110         virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
111         virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
112         \
113         classname(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
114         virtual void archive(GiNaC::archive_node &n) const; \
115         static GiNaC::ex unarchive(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
116         \
117         class visitor { \
118         public: \
119                 virtual void visit(const classname &) = 0; \
120                 virtual ~visitor() {}; \
121         };
122
123 /** Macro for inclusion in the declaration of each registered class.
124  *  It declares some functions that are common to all classes derived
125  *  from 'basic' as well as all required stuff for the GiNaC class
126  *  registry (mainly needed for archiving). */
127 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
128         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
129 public: \
130         classname(); \
131         virtual classname * duplicate() const { return new classname(*this); } \
132         \
133         virtual void accept(GiNaC::visitor & v) const \
134         { \
135                 if (visitor *p = dynamic_cast<visitor *>(&v)) \
136                         p->visit(*this); \
137                 else \
138                         inherited::accept(v); \
139         } \
140 protected: \
141         virtual int compare_same_type(const GiNaC::basic & other) const; \
142 private:
143
144
145 /** Macro for inclusion in the implementation of each registered class. */
146 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
147         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive)); \
148         const GiNaC::tinfo_static_t classname::tinfo_static = {};
149
150 /** Macro for inclusion in the implementation of each registered class.
151  *  Additional options can be specified. */
152 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
153         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive).options); \
154         const GiNaC::tinfo_static_t classname::tinfo_static = {};
155
156 /** Macro for inclusion in the implementation of each registered class.
157  *  Additional options can be specified. */
158 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(classname, supername, options) \
159         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, &classname::tinfo_static, &classname::unarchive).options); \
160         template<> const GiNaC::tinfo_static_t classname::tinfo_static = {};
161
162
163 /** Find type information key by class name. */
164 extern tinfo_t find_tinfo_key(const std::string &class_name);
165
166 /** Find unarchiving function by class name. */
167 extern unarch_func find_unarch_func(const std::string &class_name);
168
169
170 /** Add or replace a print method. */
171 template <class Alg, class Ctx, class T, class C>
172 extern void set_print_func(void f(const T &, const C & c, unsigned))
173 {
174         Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
175 }
176
177 /** Add or replace a print method. */
178 template <class Alg, class Ctx, class T, class C>
179 extern void set_print_func(void (T::*f)(const C &, unsigned))
180 {
181         Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
182 }
183
184
185 } // namespace GiNaC
186
187 #endif // ndef __GINAC_REGISTRAR_H__