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