]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
4277d949e83e5bca42a71b60131c4a0dada6a552
[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 <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 /*#include "debugmsg.h"*/
38
39 namespace GiNaC {
40
41 class ex;
42 class symbol;
43 class lst;
44 class numeric;
45 class relational;
46 class archive_node;
47
48 // Cint doesn't like vector<..,default_alloc> but malloc_alloc is
49 // unstandardized and not supported by newer GCCs.
50 #if defined(__GNUC__) && ((__GNUC__ == 2) && (__GNUC_MINOR__ < 97))
51   typedef std::vector<ex,malloc_alloc> exvector;
52 #else
53   typedef std::vector<ex> exvector;
54 #endif
55
56 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
57  *  It is responsible for the reference counting. */
58 class basic
59 {
60         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
61         
62         friend class ex;
63         
64 // member functions
65         
66         // default ctor, dtor, copy ctor assignment operator and helpers
67 public:
68         basic() : tinfo_key(TINFO_basic), flags(0), refcount(0)
69         {
70                 /* debugmsg("basic default ctor", LOGLEVEL_CONSTRUCT); */
71         }
72         /** basic dtor, virtual because class ex will delete objects via ptr. */
73         virtual ~basic()
74         {
75                 /* debugmsg("basic dtor", LOGLEVEL_DESTRUCT); */
76                 destroy(false);
77                 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
78         }
79         basic(const basic & other);
80         const basic & operator=(const basic & other);
81 protected:
82         /** For use by copy ctor and assignment operator. */
83         void copy(const basic & other)
84         {
85                 flags = other.flags & ~status_flags::dynallocated;
86                 hashvalue = other.hashvalue;
87                 tinfo_key = other.tinfo_key;
88         }
89         /** For use by dtor and assignment operator. */
90         virtual void destroy(bool call_parent) { }
91         
92         // other ctors
93         /** ctor with specified tinfo_key */
94         basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0)
95         {
96                 /* debugmsg("basic ctor with tinfo_key", LOGLEVEL_CONSTRUCT); */
97         }
98         // functions overriding virtual functions from bases classes
99         // none
100         
101         // new virtual functions which can be overridden by derived classes
102 public: // only const functions please (may break reference counting)
103         virtual basic * duplicate() const;
104         virtual void print(std::ostream & os,unsigned upper_precedence = 0) const;
105         virtual void printraw(std::ostream & os) const;
106         virtual void printtree(std::ostream & os, unsigned indent) const;
107         virtual void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence = 0) const;
108         virtual void dbgprint(void) const;
109         virtual void dbgprinttree(void) const;
110         virtual bool info(unsigned inf) const;
111         virtual unsigned nops() const;
112         virtual ex op(int i) const;
113         virtual ex & let_op(int i);
114         virtual ex operator[](const ex & index) const;
115         virtual ex operator[](int i) const;
116         virtual bool has(const ex & other) const;
117         virtual int degree(const symbol & s) const;
118         virtual int ldegree(const symbol & s) const;
119         virtual ex coeff(const symbol & s, int n = 1) const;
120         virtual ex collect(const symbol & s) const;
121         virtual ex eval(int level = 0) const;
122         virtual ex evalf(int level = 0) const;
123         virtual ex series(const relational & r, int order, unsigned options = 0) const;
124         virtual ex subs(const lst & ls, const lst & lr) const;
125         virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
126         virtual ex to_rational(lst &repl_lst) const;
127         virtual numeric integer_content(void) const;
128         virtual ex smod(const numeric &xi) const;
129         virtual numeric max_coefficient(void) const;
130         virtual exvector get_free_indices(void) const;
131         virtual ex simplify_ncmul(const exvector & v) const;
132         virtual ex eval_indexed(const basic & i) const;
133         virtual ex add_indexed(const ex & self, const ex & other) const;
134         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
135 protected: // non-const functions should be called from class ex only
136         virtual ex derivative(const symbol & s) const;
137         virtual int compare_same_type(const basic & other) const;
138         virtual bool is_equal_same_type(const basic & other) const;
139         virtual unsigned return_type(void) const;
140         virtual unsigned return_type_tinfo(void) const;
141         virtual unsigned calchash(void) const;
142         virtual ex expand(unsigned options = 0) const;
143         
144         // non-virtual functions in this class
145 public:
146         ex subs(const ex & e) const;
147         ex diff(const symbol & s, unsigned nth=1) const;
148         int compare(const basic & other) const;
149         bool is_equal(const basic & other) const;
150         const basic & hold(void) const;
151         unsigned gethash(void) const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
152         unsigned tinfo(void) const {return tinfo_key;}
153         /** Set some status_flags. */
154         const basic & setflag(unsigned f) const {flags |= f; return *this;}
155         /** Clear some status_flags. */
156         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
157 protected:
158         void ensure_if_modifiable(void) const;
159         
160 // member variables
161         
162 protected:
163         unsigned tinfo_key;                 ///< typeinfo
164         mutable unsigned flags;             ///< of type status_flags
165         mutable unsigned hashvalue;         ///< hash value
166         static unsigned precedence;         ///< precedence for printing parens
167         static unsigned delta_indent;       ///< precedence for printtree
168 private:
169         unsigned refcount;                  ///< Number of reference counts
170 };
171
172 // global variables
173
174 extern int max_recursion_level;
175
176 // convenience macros
177
178 /** Check if OBJ is a TYPE, including base classes. */
179 #define is_of_type(OBJ,TYPE) \
180         (dynamic_cast<const TYPE *>(&OBJ)!=0)
181
182 /** Check if OBJ is a TYPE, not including base classes. */
183 #define is_exactly_of_type(OBJ,TYPE) \
184         ((OBJ).tinfo()==GiNaC::TINFO_##TYPE)
185
186 /** Check if ex is a handle to a TYPE, including base classes. */
187 #define is_ex_of_type(OBJ,TYPE) \
188         (dynamic_cast<const TYPE *>((OBJ).bp)!=0)
189
190 /** Check if ex is a handle to a TYPE, not including base classes. */
191 #define is_ex_exactly_of_type(OBJ,TYPE) \
192         ((*(OBJ).bp).tinfo()==GiNaC::TINFO_##TYPE)
193
194 } // namespace GiNaC
195
196 #endif // ndef __GINAC_BASIC_H__