]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
43fc49580c505b796c01ddb9e4a33b88ae6358b8
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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_BASIC_H__
24 #define __GINAC_BASIC_H__
25
26 #include <iostream>
27 #include <typeinfo>
28 #include <vector>
29
30 // CINT needs <algorithm> to work properly with <vector>
31 #include <algorithm>
32
33 #include "flags.h"
34 #include "tinfos.h"
35 #include "assertion.h"
36 #include "registrar.h"
37
38 #ifndef NO_NAMESPACE_GINAC
39 namespace GiNaC {
40 #endif // ndef NO_NAMESPACE_GINAC
41
42 class basic;
43 class ex;
44 class symbol;
45 class lst;
46 class numeric;
47 class relational;
48 class archive_node;
49
50 // typedef std::vector<ex> exvector;
51 typedef std::vector<ex,malloc_alloc> exvector; // CINT does not like vector<...,default_alloc>
52
53 #define INLINE_BASIC_CONSTRUCTORS
54
55 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
56  *  It is responsible for the reference counting. */
57 class basic
58 {
59     GINAC_DECLARE_REGISTERED_CLASS(basic, void)
60
61     friend class ex;
62
63 // member functions
64
65     // default constructor, destructor, copy constructor assignment operator and helpers
66 public:
67     basic()
68 #ifdef INLINE_BASIC_CONSTRUCTORS
69     : tinfo_key(TINFO_basic), flags(0), refcount(0)
70     {
71     }
72 #else
73 ;
74 #endif // def INLINE_BASIC_CONSTRUCTORS
75
76     virtual ~basic()
77 #ifdef INLINE_BASIC_CONSTRUCTORS
78     {
79         destroy(0);
80         GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
81     }
82 #else
83 ;
84 #endif // def INLINE_BASIC_CONSTRUCTORS
85
86     basic(const basic & other)
87 #ifdef INLINE_BASIC_CONSTRUCTORS
88     {
89         copy(other);
90     }
91 #else
92 ;
93 #endif // def INLINE_BASIC_CONSTRUCTORS
94
95     virtual const basic & operator=(const basic & other);
96     
97 protected:
98     void copy(const basic & other)
99     {
100         flags = other.flags & ~status_flags::dynallocated;
101         hashvalue = other.hashvalue;
102         tinfo_key = other.tinfo_key;
103     }
104     void destroy(bool call_parent) {}
105
106     // other constructors
107     basic(unsigned ti)
108 #ifdef INLINE_BASIC_CONSTRUCTORS
109     : tinfo_key(ti), flags(0), refcount(0)
110     {
111     }
112 #else
113 ;
114 #endif // def INLINE_BASIC_CONSTRUCTORS
115
116     // functions overriding virtual functions from bases classes
117     // none
118     
119     // new virtual functions which can be overridden by derived classes
120 public: // only const functions please (may break reference counting)
121     virtual basic * duplicate() const;
122     virtual void print(std::ostream & os,unsigned upper_precedence = 0) const;
123     virtual void printraw(std::ostream & os) const;
124     virtual void printtree(std::ostream & os, unsigned indent) const;
125     virtual void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence = 0) const;
126     virtual void dbgprint(void) const;
127     virtual void dbgprinttree(void) const;
128     virtual bool info(unsigned inf) const;
129     virtual unsigned nops() const;
130     virtual ex op(int i) const;
131     virtual ex & let_op(int i);
132     virtual ex operator[](const ex & index) const;
133     virtual ex operator[](int i) const;
134     virtual bool has(const ex & other) const;
135     virtual int degree(const symbol & s) const;
136     virtual int ldegree(const symbol & s) const;
137     virtual ex coeff(const symbol & s, int n = 1) const;
138     virtual ex collect(const symbol & s) const;
139     virtual ex eval(int level = 0) const;
140     virtual ex evalf(int level = 0) const;
141     virtual ex series(const relational & r, int order, unsigned options = 0) const;
142     virtual ex subs(const lst & ls, const lst & lr) const;
143     virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
144     virtual ex to_rational(lst &repl_lst) const;
145     virtual numeric integer_content(void) const;
146     virtual ex smod(const numeric &xi) const;
147     virtual numeric max_coefficient(void) const;
148     virtual exvector get_indices(void) const;
149     virtual ex simplify_ncmul(const exvector & v) const;
150 protected: // non-const functions should be called from class ex only
151     virtual ex derivative(const symbol & s) const;
152     virtual int compare_same_type(const basic & other) const;
153     virtual bool is_equal_same_type(const basic & other) const;
154     virtual unsigned return_type(void) const;
155     virtual unsigned return_type_tinfo(void) const;
156     virtual unsigned calchash(void) const;
157     virtual ex expand(unsigned options=0) const;
158
159     // non-virtual functions in this class
160 public:
161     ex subs(const ex & e) const;
162     ex diff(const symbol & s, unsigned nth=1) const;
163     int compare(const basic & other) const;
164     bool is_equal(const basic & other) const;
165     const basic & hold(void) const;
166     unsigned gethash(void) const {if (flags & status_flags::hash_calculated) return hashvalue; else return calchash();}
167     unsigned tinfo(void) const {return tinfo_key;}
168     const basic & setflag(unsigned f) const {flags |= f; return *this;}
169     const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
170 protected:
171     void ensure_if_modifiable(void) const;
172
173 // member variables
174     
175 protected:
176     unsigned tinfo_key;
177     mutable unsigned flags;
178     mutable unsigned hashvalue;
179     static unsigned precedence;
180     static unsigned delta_indent;
181 private:
182     unsigned refcount;
183 };
184
185 // global constants
186
187 extern const basic some_basic;
188 extern const type_info & typeid_basic;
189
190 // global variables
191
192 extern int max_recursion_level;
193
194 // convenience macros
195
196 #ifndef NO_NAMESPACE_GINAC
197
198 #define is_of_type(OBJ,TYPE) \
199     (dynamic_cast<TYPE *>(const_cast<GiNaC::basic *>(&OBJ))!=0)
200
201 #define is_exactly_of_type(OBJ,TYPE) \
202     ((OBJ).tinfo()==GiNaC::TINFO_##TYPE)
203
204 #define is_ex_of_type(OBJ,TYPE) \
205     (dynamic_cast<TYPE *>(const_cast<GiNaC::basic *>((OBJ).bp))!=0)
206
207 #define is_ex_exactly_of_type(OBJ,TYPE) \
208     ((*(OBJ).bp).tinfo()==GiNaC::TINFO_##TYPE)
209
210 #else // ndef NO_NAMESPACE_GINAC
211
212 #define is_of_type(OBJ,TYPE) \
213     (dynamic_cast<TYPE *>(const_cast<basic *>(&OBJ))!=0)
214
215 #define is_exactly_of_type(OBJ,TYPE) \
216     ((OBJ).tinfo()==TINFO_##TYPE)
217
218 #define is_ex_of_type(OBJ,TYPE) \
219     (dynamic_cast<TYPE *>(const_cast<basic *>((OBJ).bp))!=0)
220
221 #define is_ex_exactly_of_type(OBJ,TYPE) \
222     ((*(OBJ).bp).tinfo()==TINFO_##TYPE)
223
224 #endif // ndef NO_NAMESPACE_GINAC
225
226 #ifndef NO_NAMESPACE_GINAC
227 } // namespace GiNaC
228 #endif // ndef NO_NAMESPACE_GINAC
229
230 #endif // ndef __GINAC_BASIC_H__