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