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