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