]> www.ginac.de Git - ginac.git/blob - ginac/print.h
ncmul::eval(): don't write beyond the end of the vector.
[ginac.git] / ginac / print.h
1 /** @file print.h
2  *
3  *  Definition of helper classes for expression output. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef GINAC_PRINT_H
24 #define GINAC_PRINT_H
25
26 #include "class_info.h"
27
28 #include <iosfwd>
29 #include <memory>
30 #include <string>
31
32 namespace GiNaC {
33
34 /** This class stores information about a registered print_context class. */
35 class print_context_options {
36 public:
37         print_context_options(const char *n, const char *p, unsigned i)
38          : name(n), parent_name(p), id(i) {}
39
40         const char *get_name() const { return name; }
41         const char *get_parent_name() const { return parent_name; }
42         unsigned get_id() const { return id; }
43
44 private:
45         const char *name;         /**< Class name. */
46         const char *parent_name;  /**< Name of superclass. */
47         unsigned id;              /**< ID number (assigned automatically). */
48 };
49
50 typedef class_info<print_context_options> print_context_class_info;
51
52
53 /** Flags to control the behavior of a print_context. */
54 class print_options {
55 public:
56         enum {
57                 print_index_dimensions = 0x0001 ///< print the dimensions of indices
58         };
59 };
60
61
62 /** Macro for inclusion in the declaration of a print_context class.
63  *  It declares some functions that are common to all classes derived
64  *  from 'print_context' as well as all required stuff for the GiNaC
65  *  registry. */
66 #define GINAC_DECLARE_PRINT_CONTEXT(classname, supername) \
67 public: \
68         typedef supername inherited; \
69         friend class function_options; \
70         friend class registered_class_options; \
71 public: \
72         static const GiNaC::print_context_class_info &get_class_info_static(); \
73         virtual const GiNaC::print_context_class_info &get_class_info() const { return classname::get_class_info_static(); } \
74         virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
75         \
76         classname(); \
77         virtual classname * duplicate() const { return new classname(*this); } \
78 private:
79
80 /** Macro for inclusion in the implementation of each print_context class. */
81 #define GINAC_IMPLEMENT_PRINT_CONTEXT(classname, supername) \
82 const GiNaC::print_context_class_info &classname::get_class_info_static() \
83 { \
84         static GiNaC::print_context_class_info reg_info = GiNaC::print_context_class_info(GiNaC::print_context_options(#classname, #supername, GiNaC::next_print_context_id++)); \
85         return reg_info; \
86 }
87
88
89 extern unsigned next_print_context_id;
90
91
92 /** Base class for print_contexts. */
93 class print_context
94 {
95         GINAC_DECLARE_PRINT_CONTEXT(print_context, void)
96 public:
97         print_context(std::ostream &, unsigned options = 0);
98         virtual ~print_context() {}
99
100         std::ostream & s; /**< stream to output to */
101         unsigned options; /**< option flags */
102 };
103
104 /** Context for default (ginsh-parsable) output. */
105 class print_dflt : public print_context
106 {
107         GINAC_DECLARE_PRINT_CONTEXT(print_dflt, print_context)
108 public:
109         print_dflt(std::ostream &, unsigned options = 0);
110 };
111
112 /** Context for latex-parsable output. */
113 class print_latex : public print_context
114 {
115         GINAC_DECLARE_PRINT_CONTEXT(print_latex, print_context)
116 public:
117         print_latex(std::ostream &, unsigned options = 0);
118 };
119
120 /** Context for python pretty-print output. */
121 class print_python : public print_context
122 {
123         GINAC_DECLARE_PRINT_CONTEXT(print_python, print_context)
124 public:
125         print_python(std::ostream &, unsigned options = 0);
126 };
127
128 /** Context for python-parsable output. */
129 class print_python_repr : public print_context
130 {
131         GINAC_DECLARE_PRINT_CONTEXT(print_python_repr, print_context)
132 public:
133         print_python_repr(std::ostream &, unsigned options = 0);
134 };
135
136 /** Context for tree-like output for debugging. */
137 class print_tree : public print_context
138 {
139         GINAC_DECLARE_PRINT_CONTEXT(print_tree, print_context)
140 public:
141         print_tree(unsigned d);
142         print_tree(std::ostream &, unsigned options = 0, unsigned d = 4);
143
144         const unsigned delta_indent; /**< size of indentation step */
145 };
146
147 /** Base context for C source output. */
148 class print_csrc : public print_context
149 {
150         GINAC_DECLARE_PRINT_CONTEXT(print_csrc, print_context)
151 public:
152         print_csrc(std::ostream &, unsigned options = 0);
153 };
154
155 /** Context for C source output using float precision. */
156 class print_csrc_float : public print_csrc
157 {
158         GINAC_DECLARE_PRINT_CONTEXT(print_csrc_float, print_csrc)
159 public:
160         print_csrc_float(std::ostream &, unsigned options = 0);
161 };
162
163 /** Context for C source output using double precision. */
164 class print_csrc_double : public print_csrc
165 {
166         GINAC_DECLARE_PRINT_CONTEXT(print_csrc_double, print_csrc)
167 public:
168         print_csrc_double(std::ostream &, unsigned options = 0);
169 };
170
171 /** Context for C source output using CLN numbers. */
172 class print_csrc_cl_N : public print_csrc
173 {
174         GINAC_DECLARE_PRINT_CONTEXT(print_csrc_cl_N, print_csrc)
175 public:
176         print_csrc_cl_N(std::ostream &, unsigned options = 0);
177 };
178
179 /** Check if obj is a T, including base classes. */
180 template <class T>
181 inline bool is_a(const print_context & obj)
182 { return dynamic_cast<const T *>(&obj) != 0; }
183
184
185 class basic;
186
187 /** Base class for print_functor handlers */
188 class print_functor_impl {
189 public:
190         virtual ~print_functor_impl() {}
191         virtual print_functor_impl *duplicate() const = 0;
192         virtual void operator()(const basic & obj, const print_context & c, unsigned level) const = 0;
193 };
194
195 /** print_functor handler for pointer-to-functions of class T, context type C */
196 template <class T, class C>
197 class print_ptrfun_handler : public print_functor_impl {
198 public:
199         typedef void (*F)(const T &, const C &, unsigned);
200
201         print_ptrfun_handler(F f_) : f(f_) {}
202         print_ptrfun_handler *duplicate() const { return new print_ptrfun_handler(*this); }
203
204         void operator()(const basic & obj, const print_context & c, unsigned level) const
205         {
206                 // Call the supplied function
207                 f(dynamic_cast<const T &>(obj), dynamic_cast<const C &>(c), level);
208         }
209
210 private:
211         F f;
212 };
213
214 /** print_functor handler for member functions of class T, context type C */
215 template <class T, class C>
216 class print_memfun_handler : public print_functor_impl {
217 public:
218         typedef void (T::*F)(const C & c, unsigned level) const;
219
220         print_memfun_handler(F f_) : f(f_) {}
221         print_memfun_handler *duplicate() const { return new print_memfun_handler(*this); }
222
223         void operator()(const basic & obj, const print_context & c, unsigned level) const
224         {
225                 // Call the supplied member function
226                 return (dynamic_cast<const T &>(obj).*f)(dynamic_cast<const C &>(c), level);
227         }
228
229 private:
230         F f;
231 };
232
233 /** This class represents a print method for a certain algebraic class and
234  *  print_context type. Its main purpose is to hide the difference between
235  *  member functions and nonmember functions behind one unified operator()
236  *  interface. print_functor has value semantics and acts as a smart pointer
237  *  (with deep copy) to a class derived from print_functor_impl which
238  *  implements the actual function call. */
239 class print_functor {
240 public:
241         print_functor() : impl(0) {}
242         print_functor(const print_functor & other) : impl(other.impl.get() ? other.impl->duplicate() : 0) {}
243         print_functor(std::auto_ptr<print_functor_impl> impl_) : impl(impl_) {}
244
245         template <class T, class C>
246         print_functor(void f(const T &, const C &, unsigned)) : impl(new print_ptrfun_handler<T, C>(f)) {}
247
248         template <class T, class C>
249         print_functor(void (T::*f)(const C &, unsigned) const) : impl(new print_memfun_handler<T, C>(f)) {}
250
251         print_functor & operator=(const print_functor & other)
252         {
253                 if (this != &other) {
254                         print_functor_impl *p = other.impl.get();
255                         impl.reset(p ? other.impl->duplicate() : 0);
256                 }
257                 return *this;
258         }
259
260         void operator()(const basic & obj, const print_context & c, unsigned level) const
261         {
262                 (*impl)(obj, c, level);
263         }
264
265         bool is_valid() const { return impl.get(); }
266
267 private:
268         std::auto_ptr<print_functor_impl> impl;
269 };
270
271
272 } // namespace GiNaC
273
274 #endif // ndef GINAC_BASIC_H