]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
- minor ISO cleanup.
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
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_BASIC_H__
24 #define __GINAC_BASIC_H__
25
26 #include <iostream>
27 #include <vector>
28
29 // CINT needs <algorithm> to work properly with <vector>
30 #include <algorithm>
31
32 #include "flags.h"
33 #include "tinfos.h"
34 #include "assertion.h"
35 #include "registrar.h"
36 /*#include "debugmsg.h"*/
37
38 namespace GiNaC {
39
40 class ex;
41 class symbol;
42 class lst;
43 class numeric;
44 class relational;
45 class archive_node;
46 class print_context;
47
48 typedef std::vector<ex> exvector;
49
50
51 /** Function object for map(). */
52 struct map_function {
53         typedef const ex & argument_type;
54         typedef ex result_type;
55         virtual ex operator()(const ex & e) = 0;
56 };
57
58
59 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
60  *  It is responsible for the reference counting. */
61 class basic
62 {
63         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
64         
65         friend class ex;
66         
67         // default ctor, dtor, copy ctor assignment operator and helpers
68 public:
69         basic() : tinfo_key(TINFO_basic), flags(0), refcount(0)
70         {
71                 /* debugmsg("basic default ctor", LOGLEVEL_CONSTRUCT); */
72         }
73         /** basic dtor, virtual because class ex will delete objects via ptr. */
74         virtual ~basic()
75         {
76                 /* debugmsg("basic dtor", LOGLEVEL_DESTRUCT); */
77                 destroy(false);
78                 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
79         }
80         basic(const basic & other);
81         const basic & operator=(const basic & other);
82 protected:
83         /** For use by copy ctor and assignment operator. */
84         void copy(const basic & other)
85         {
86                 flags = other.flags & ~status_flags::dynallocated;
87                 hashvalue = other.hashvalue;
88                 tinfo_key = other.tinfo_key;
89         }
90         /** For use by dtor and assignment operator. */
91         virtual void destroy(bool call_parent) { }
92         
93         // other ctors
94         /** ctor with specified tinfo_key */
95         basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0)
96         {
97                 /* debugmsg("basic ctor with tinfo_key", LOGLEVEL_CONSTRUCT); */
98         }
99         
100         // new virtual functions which can be overridden by derived classes
101 public: // only const functions please (may break reference counting)
102         virtual basic * duplicate() const;
103         virtual void print(const print_context & c, unsigned level = 0) const;
104         virtual void dbgprint(void) const;
105         virtual void dbgprinttree(void) const;
106         virtual unsigned precedence(void) const;
107         virtual bool info(unsigned inf) const;
108         virtual unsigned nops() const;
109         virtual ex op(int i) const;
110         virtual ex & let_op(int i);
111         virtual ex operator[](const ex & index) const;
112         virtual ex operator[](int i) const;
113         virtual ex expand(unsigned options = 0) const;
114         virtual bool has(const ex & other) const;
115         virtual ex map(map_function & f) const;
116         virtual int degree(const ex & s) const;
117         virtual int ldegree(const ex & s) const;
118         virtual ex coeff(const ex & s, int n = 1) const;
119         virtual ex collect(const ex & s, bool distributed = false) const;
120         virtual ex eval(int level = 0) const;
121         virtual ex evalf(int level = 0) const;
122         virtual ex evalm(void) const;
123         virtual ex series(const relational & r, int order, unsigned options = 0) const;
124         virtual bool match(const ex & pattern, lst & repl_lst) const;
125         virtual ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
126         virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
127         virtual ex to_rational(lst &repl_lst) const;
128         virtual numeric integer_content(void) const;
129         virtual ex smod(const numeric &xi) const;
130         virtual numeric max_coefficient(void) const;
131         virtual exvector get_free_indices(void) const;
132         virtual ex eval_indexed(const basic & i) const;
133         virtual ex add_indexed(const ex & self, const ex & other) const;
134         virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
135         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
136         virtual unsigned return_type(void) const;
137         virtual unsigned return_type_tinfo(void) const;
138 protected: // functions that should be called from class ex only
139         virtual ex derivative(const symbol & s) const;
140         virtual int compare_same_type(const basic & other) const;
141         virtual bool is_equal_same_type(const basic & other) const;
142         virtual bool match_same_type(const basic & other) const;
143         virtual unsigned calchash(void) const;
144         virtual ex simplify_ncmul(const exvector & v) const;
145         
146         // non-virtual functions in this class
147 public:
148         ex subs(const ex & e, bool no_pattern = false) const;
149         ex diff(const symbol & s, unsigned nth=1) const;
150         int compare(const basic & other) const;
151         bool is_equal(const basic & other) const;
152         const basic & hold(void) const;
153         unsigned gethash(void) const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
154         unsigned tinfo(void) const {return tinfo_key;}
155
156         /** Set some status_flags. */
157         const basic & setflag(unsigned f) const {flags |= f; return *this;}
158
159         /** Clear some status_flags. */
160         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
161
162 protected:
163         void ensure_if_modifiable(void) const;
164         
165         // member variables
166 protected:
167         unsigned tinfo_key;                 ///< typeinfo
168         mutable unsigned flags;             ///< of type status_flags
169         mutable unsigned hashvalue;         ///< hash value
170 private:
171         unsigned refcount;                  ///< Number of reference counts
172 };
173
174 // global variables
175
176 extern int max_recursion_level;
177
178 // convenience type checker template functions
179
180 /** Check if obj is a T, including base classes. */
181 template <class T>
182 inline bool is_a(const basic & obj)
183 {
184         return dynamic_cast<const T *>(&obj)!=0;
185 }
186
187 /** Check if obj is a T, not including base classes.  This one is just an
188  *  inefficient default.  It should in all time-critical cases be overridden
189  *  by template specializations that don't create a temporary. */
190 template <class T>
191 inline bool is_exactly_a(const class basic & obj)
192 {
193         const T foo; return foo.tinfo()==obj.tinfo();
194 }
195
196 /** Check if ex is a handle to a T, including base classes. */
197 template <class T>
198 inline bool is_a(const ex & obj)
199 {
200         return is_a<T>(*obj.bp);
201 }
202
203 /** Check if ex is a handle to a T, not including base classes. */
204 template <class T>
205 inline bool is_exactly_a(const ex & obj)
206 {
207         return is_exactly_a<T>(*obj.bp);
208 }
209
210 /** Return a reference to the basic-derived class T object embedded in an
211  *  expression.  This is fast but unsafe: the result is undefined if the
212  *  expression does not contain a T object at its top level.
213  *
214  *  @param e expression
215  *  @return reference to pseries object
216  *  @see is_exactly_a<class T>() */
217 template <class T>
218 inline const T &ex_to(const ex &e)
219 {
220         return static_cast<const T &>(*e.bp);
221 }
222
223 } // namespace GiNaC
224
225 #endif // ndef __GINAC_BASIC_H__