]> www.ginac.de Git - ginac.git/blob - ginac/registrar.h
8cab2031a010cf57e9918fb4e73f74eab13c30b0
[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-2001 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
28 namespace GiNaC {
29
30 class registered_class_info;
31 class ex;
32 class archive_node;
33 class lst;
34
35
36 /** Unarchiving function (static member function of every GiNaC class). */
37 typedef ex (*unarch_func)(const archive_node &n, const lst &sym_lst);
38
39
40 /** Head of list of all registered_class_info structures. */
41 extern registered_class_info *first_registered_class;
42
43
44 /** This structure stores information about a registered GiNaC class. */
45 struct registered_class_info {
46         registered_class_info(const char *n, const char *s, unsigned int k, unarch_func f)
47                 : name(n), super(s), tinfo_key(k), unarchive(f)
48         {
49                 // Add structure to list
50                 next = first_registered_class;
51                 first_registered_class = this;
52         }
53
54         registered_class_info *next;  /**< Pointer to next registered_class_info in list. */
55         const char *name;             /**< Class name. */
56         const char *super;            /**< Name of superclass. */
57         unsigned int tinfo_key;       /**< TINFO_* key. */
58         unarch_func unarchive;        /**< Pointer to unarchiving function. */
59 };
60
61
62 /** Primary macro for inclusion in the implementation of each registered class. */
63 #define GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
64 public: \
65         typedef supername inherited; \
66         static registered_class_info reg_info; \
67         virtual const char *class_name(void) const; \
68         classname(const archive_node &n, const lst &sym_lst); \
69         virtual void archive(archive_node &n) const; \
70         static ex unarchive(const archive_node &n, const lst &sym_lst);
71
72 /** Macro for inclusion in the declaration of each registered class.
73  *  It declares some functions that are common to all classes derived
74  *  from 'basic' as well as all required stuff for the GiNaC class
75  *  registry (mainly needed for archiving). */
76 #define GINAC_DECLARE_REGISTERED_CLASS(classname, supername) \
77         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(classname, supername) \
78 public: \
79         classname(); \
80         ~classname() { /*debugmsg(#classname " dtor", LOGLEVEL_DESTRUCT);*/ destroy(false); } \
81         classname(const classname & other); \
82         const classname & operator=(const classname & other); \
83         basic * duplicate() const; \
84         unsigned get_precedence(void) const {return precedence;} \
85 protected: \
86         void copy(const classname & other); \
87         void destroy(bool call_parent); \
88         int compare_same_type(const basic & other) const; \
89 private:
90
91 /** Primary macro for inclusion in the implementation of each registered class. */
92 #define GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(classname, supername) \
93         registered_class_info classname::reg_info(#classname, #supername, TINFO_##classname, &classname::unarchive); \
94         const char *classname::class_name(void) const {return reg_info.name;}
95
96 /** Macro for inclusion in the implementation of each registered class.
97  *  It implements some functions that are the same in all classes derived
98  *  from 'basic' (such as the assignment operator). */
99 #define GINAC_IMPLEMENT_REGISTERED_CLASS(classname, supername) \
100         GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(classname, supername) \
101 classname::classname(const classname & other) \
102 { \
103         /*debugmsg(#classname " copy ctor", LOGLEVEL_CONSTRUCT);*/ \
104         copy(other); \
105 } \
106 const classname & classname::operator=(const classname & other) \
107 { \
108         /*debugmsg(#classname " operator=", LOGLEVEL_ASSIGNMENT);*/ \
109         if (this != &other) { \
110                 destroy(true); \
111                 copy(other); \
112         } \
113         return *this; \
114 } \
115 basic * classname::duplicate() const { \
116         /*debugmsg(#classname " duplicate", LOGLEVEL_DUPLICATE);*/ \
117         return new classname(*this); \
118 }
119
120
121 /** Find TINFO_* key by class name. */
122 extern unsigned int find_tinfo_key(const std::string &class_name);
123
124 /** Find unarchiving function by class name. */
125 extern unarch_func find_unarch_func(const std::string &class_name);
126
127
128 } // namespace GiNaC
129
130 #endif // ndef __GINAC_REGISTRAR_H__