]> www.ginac.de Git - ginac.git/blob - ginac/registrar.h
e6c083f32f98f72e60824ed3f20b892ab10c6374
[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-2003 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
29 #include "class_info.h"
30
31 namespace GiNaC {
32
33 class ex;
34 class archive_node;
35
36 template <template <class> class> class container;
37 typedef container<std::list> lst;
38
39
40 /** Unarchiving function (static member function of every GiNaC class). */
41 typedef ex (*unarch_func)(const archive_node &n, lst &sym_lst);
42
43
44 /** This structure stores information about a registered GiNaC class. */
45 class registered_class_options {
46 public:
47         registered_class_options(const char *n, const char *p, unsigned ti, unarch_func f)
48          : name(n), parent_name(p), tinfo_key(ti), unarchive(f) {}
49
50         const char *get_name() const { return name; }
51         const char *get_parent_name() const { return parent_name; }
52         unsigned get_id() const { return tinfo_key; }
53         unarch_func get_unarch_func() const { return unarchive; }
54
55 private:
56         const char *name;         /**< Class name. */
57         const char *parent_name;  /**< Name of superclass. */
58         unsigned tinfo_key;       /**< TINFO_* key. */
59         unarch_func unarchive;    /**< Pointer to unarchiving function. */
60 };
61
62 typedef class_info<registered_class_options> registered_class_info;
63
64
65 /** Primary macro for inclusion in the declaration of each registered class. */
66 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
67 public: \
68         typedef supername inherited; \
69         template <class isexaclass> friend bool is_exactly_a(const GiNaC::basic &obj); \
70 private: \
71         static GiNaC::registered_class_info reg_info; \
72 public: \
73         virtual const GiNaC::registered_class_info &get_class_info() const { return reg_info; } \
74         virtual const char *class_name() const { return reg_info.options.get_name(); } \
75         \
76         classname(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
77         virtual void archive(GiNaC::archive_node &n) const; \
78         static GiNaC::ex unarchive(const GiNaC::archive_node &n, GiNaC::lst &sym_lst); \
79         \
80         class visitor { \
81         public: \
82                 virtual void visit(const classname &) = 0; \
83         };
84
85 /** Macro for inclusion in the declaration of each registered class.
86  *  It declares some functions that are common to all classes derived
87  *  from 'basic' as well as all required stuff for the GiNaC class
88  *  registry (mainly needed for archiving). */
89 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
90         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
91 public: \
92         classname(); \
93         classname * duplicate() const { return new classname(*this); } \
94         \
95         void accept(GiNaC::visitor & v) const \
96         { \
97                 if (visitor *p = dynamic_cast<visitor *>(&v)) \
98                         p->visit(*this); \
99                 else \
100                         inherited::accept(v); \
101         } \
102 protected: \
103         int compare_same_type(const GiNaC::basic & other) const; \
104 private:
105
106 /** Macro for inclusion in the implementation of each registered class. */
107 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
108         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, TINFO_##classname, &classname::unarchive));
109
110 /** Macro for inclusion in the implementation of each registered class.
111  *  Additional options can be specified. */
112 #define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options) \
113         GiNaC::registered_class_info classname::reg_info = GiNaC::registered_class_info(GiNaC::registered_class_options(#classname, #supername, TINFO_##classname, &classname::unarchive).options);
114
115
116 /** Find TINFO_* key by class name. */
117 extern unsigned find_tinfo_key(const std::string &class_name);
118
119 /** Find unarchiving function by class name. */
120 extern unarch_func find_unarch_func(const std::string &class_name);
121
122
123 } // namespace GiNaC
124
125 #endif // ndef __GINAC_REGISTRAR_H__