]> www.ginac.de Git - ginac.git/blob - ginac/registrar.h
registered_class_info: use typeid() instead of tinfo_static.
[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 #include <typeinfo>
30
31 #include "class_info.h"
32 #include "print.h"
33
34 namespace GiNaC {
35
36 class ex;
37 class archive_node;
38
39 template <template <class T, class = std::allocator<T> > class> class container;
40 typedef container<std::list> lst;
41
42 /** To distinguish between different kinds of non-commutative objects */
43 struct return_type_t
44 {
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 represenation labels.
49         unsigned rl;
50
51         /// Strict weak ordering (so one can put return_type_t's into
52         /// a STL container).
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
71 template<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
79 /** Definitions for the tinfo mechanism. */
80 typedef const void * tinfo_t;
81 struct tinfo_static_t {};
82
83 /** Unarchiving function (static member function of every GiNaC class). */
84 typedef ex (*unarch_func)(const archive_node &n, lst &sym_lst);
85
86
87 /** This class stores information about a registered GiNaC class. */
88 class registered_class_options {
89 public:
90         registered_class_options(const char *n, const char *p, 
91                                  const std::type_info& ti,
92                                  unarch_func f)
93          : name(n), parent_name(p), tinfo_key(&ti), unarchive(f) {}
94
95         const char *get_name() const { return name; }
96         const char *get_parent_name() const { return parent_name; }
97         std::type_info const* get_id() const { return tinfo_key; }
98         unarch_func get_unarch_func() const { return unarchive; }
99         const std::vector<print_functor> &get_print_dispatch_table() const { return print_dispatch_table; }
100
101         template <class Ctx, class T, class C>
102         registered_class_options & print_func(void f(const T &, const C & c, unsigned))
103         {
104                 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
105                 return *this;
106         }
107
108         template <class Ctx, class T, class C>
109         registered_class_options & print_func(void (T::*f)(const C &, unsigned))
110         {
111                 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
112                 return *this;
113         }
114
115         template <class Ctx>
116         registered_class_options & print_func(const print_functor & f)
117         {
118                 set_print_func(Ctx::get_class_info_static().options.get_id(), f);
119                 return *this;
120         }
121
122         void set_print_func(unsigned id, const print_functor & f)
123         {
124                 if (id >= print_dispatch_table.size())
125                         print_dispatch_table.resize(id + 1);
126                 print_dispatch_table[id] = f;
127         }
128
129 private:
130         const char *name;         /**< Class name. */
131         const char *parent_name;  /**< Name of superclass. */
132         std::type_info const* tinfo_key;        /**< Type information key. */
133         unarch_func unarchive;    /**< Pointer to unarchiving function. */
134         std::vector<print_functor> print_dispatch_table; /**< Method table for print() dispatch */
135 };
136
137 typedef class_info<registered_class_options> registered_class_info;
138
139
140 /** Primary macro for inclusion in the declaration of each registered class. */
141 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
142 public: \
143         typedef supername inherited; \
144     static const GiNaC::tinfo_static_t tinfo_static; \
145 private: \
146         static GiNaC::registered_class_info reg_info; \
147 public: \
148         static GiNaC::registered_class_info &get_class_info_static() { return reg_info; } \
149         virtual const GiNaC::registered_class_info &get_class_info() const { return classname::get_class_info_static(); } \
150         virtual GiNaC::registered_class_info &get_class_info() { return classname::get_class_info_static(); } \
151         virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
152         \
153         classname(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
154         virtual void archive(GiNaC::archive_node &n) const; \
155         static GiNaC::ex unarchive(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
156         \
157         class visitor { \
158         public: \
159                 virtual void visit(const classname &) = 0; \
160                 virtual ~visitor() {}; \
161         };
162
163 /** Macro for inclusion in the declaration of each registered class.
164  *  It declares some functions that are common to all classes derived
165  *  from 'basic' as well as all required stuff for the GiNaC class
166  *  registry (mainly needed for archiving). */
167 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
168         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
169 public: \
170         classname(); \
171         virtual classname * duplicate() const { return new classname(*this); } \
172         \
173         virtual void accept(GiNaC::visitor & v) const \
174         { \
175                 if (visitor *p = dynamic_cast<visitor *>(&v)) \
176                         p->visit(*this); \
177                 else \
178                         inherited::accept(v); \
179         } \
180 protected: \
181         virtual int compare_same_type(const GiNaC::basic & other) const; \
182 private:
183
184
185 /** Macro for inclusion in the implementation of each registered class. */
186 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
187         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname), &classname::unarchive)); \
188         const GiNaC::tinfo_static_t classname::tinfo_static = {};
189
190 /** Macro for inclusion in the implementation of each registered class.
191  *  Additional options can be specified. */
192 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
193         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname), &classname::unarchive).options); \
194         const GiNaC::tinfo_static_t classname::tinfo_static = {};
195
196 /** Macro for inclusion in the implementation of each registered class.
197  *  Additional options can be specified. */
198 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(classname, supername, options) \
199         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, typeid(classname), &classname::unarchive).options); \
200         template<> const GiNaC::tinfo_static_t classname::tinfo_static = {};
201
202
203 /** Find type information key by class name. */
204 extern std::type_info const* find_tinfo_key(const std::string &class_name);
205
206 /** Find unarchiving function by class name. */
207 extern unarch_func find_unarch_func(const std::string &class_name);
208
209
210 /** Add or replace a print method. */
211 template <class Alg, class Ctx, class T, class C>
212 extern void set_print_func(void f(const T &, const C & c, unsigned))
213 {
214         Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
215 }
216
217 /** Add or replace a print method. */
218 template <class Alg, class Ctx, class T, class C>
219 extern void set_print_func(void (T::*f)(const C &, unsigned))
220 {
221         Alg::get_class_info_static().options.set_print_func(Ctx::get_class_info_static().options.get_id(), f);
222 }
223
224
225 } // namespace GiNaC
226
227 #endif // ndef __GINAC_REGISTRAR_H__