3 * Definition of helper classes for expression output. */
6 * GiNaC Copyright (C) 1999-2025 Johannes Gutenberg University Mainz, Germany
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.
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.
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
26 #include "class_info.h"
33 /** This class stores information about a registered print_context class. */
34 class print_context_options {
36 print_context_options(const char *n, const char *p, unsigned i)
37 : name(n), parent_name(p), id(i) {}
39 const char *get_name() const { return name; }
40 const char *get_parent_name() const { return parent_name; }
41 unsigned get_id() const { return id; }
44 const char *name; /**< Class name. */
45 const char *parent_name; /**< Name of superclass. */
46 unsigned id; /**< ID number (assigned automatically). */
49 typedef class_info<print_context_options> print_context_class_info;
52 /** Flags to control the behavior of a print_context. */
56 print_index_dimensions = 0x0001 ///< print the dimensions of indices
61 /** Common part of GINAC_DECLARE_PRINT_CONTEXT_BASE and GINAC_DECLARE_PRINT_CONTEXT_DERIVED. */
62 #define GINAC_DECLARE_PRINT_CONTEXT_COMMON(classname) \
64 friend class function_options; \
65 friend class registered_class_options; \
66 static const GiNaC::print_context_class_info &get_class_info_static(); \
69 #define GINAC_DECLARE_PRINT_CONTEXT_BASE(classname) \
70 GINAC_DECLARE_PRINT_CONTEXT_COMMON(classname) \
71 virtual const GiNaC::print_context_class_info &get_class_info() const { return classname::get_class_info_static(); } \
72 virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
73 virtual classname * duplicate() const { return new classname(*this); } \
76 /** Macro for inclusion in the declaration of a print_context class.
77 * It declares some functions that are common to all classes derived
78 * from 'print_context' as well as all required stuff for the GiNaC
80 #define GINAC_DECLARE_PRINT_CONTEXT(classname, supername) \
81 GINAC_DECLARE_PRINT_CONTEXT_COMMON(classname) \
82 typedef supername inherited; \
83 const GiNaC::print_context_class_info &get_class_info() const override { return classname::get_class_info_static(); } \
84 const char *class_name() const override { return classname::get_class_info_static().options.get_name(); } \
85 classname * duplicate() const override { return new classname(*this); } \
88 /** Macro for inclusion in the implementation of each print_context class. */
89 #define GINAC_IMPLEMENT_PRINT_CONTEXT(classname, supername) \
90 const GiNaC::print_context_class_info &classname::get_class_info_static() \
92 static GiNaC::print_context_class_info reg_info = GiNaC::print_context_class_info(GiNaC::print_context_options(#classname, #supername, GiNaC::next_print_context_id++)); \
97 extern unsigned next_print_context_id;
100 /** Base class for print_contexts. */
103 GINAC_DECLARE_PRINT_CONTEXT_BASE(print_context)
105 print_context(std::ostream &, unsigned options = 0);
106 virtual ~print_context() {}
108 std::ostream & s; /**< stream to output to */
109 unsigned options; /**< option flags */
112 /** Context for default (ginsh-parsable) output. */
113 class print_dflt : public print_context
115 GINAC_DECLARE_PRINT_CONTEXT(print_dflt, print_context)
117 print_dflt(std::ostream &, unsigned options = 0);
120 /** Context for latex-parsable output. */
121 class print_latex : public print_context
123 GINAC_DECLARE_PRINT_CONTEXT(print_latex, print_context)
125 print_latex(std::ostream &, unsigned options = 0);
128 /** Context for python pretty-print output. */
129 class print_python : public print_context
131 GINAC_DECLARE_PRINT_CONTEXT(print_python, print_context)
133 print_python(std::ostream &, unsigned options = 0);
136 /** Context for python-parsable output. */
137 class print_python_repr : public print_context
139 GINAC_DECLARE_PRINT_CONTEXT(print_python_repr, print_context)
141 print_python_repr(std::ostream &, unsigned options = 0);
144 /** Context for tree-like output for debugging. */
145 class print_tree : public print_context
147 GINAC_DECLARE_PRINT_CONTEXT(print_tree, print_context)
149 print_tree(unsigned d);
150 print_tree(std::ostream &, unsigned options = 0, unsigned d = 4);
152 const unsigned delta_indent; /**< size of indentation step */
155 /** Base context for C source output. */
156 class print_csrc : public print_context
158 GINAC_DECLARE_PRINT_CONTEXT(print_csrc, print_context)
160 print_csrc(std::ostream &, unsigned options = 0);
163 /** Context for C source output using float precision. */
164 class print_csrc_float : public print_csrc
166 GINAC_DECLARE_PRINT_CONTEXT(print_csrc_float, print_csrc)
168 print_csrc_float(std::ostream &, unsigned options = 0);
171 /** Context for C source output using double precision. */
172 class print_csrc_double : public print_csrc
174 GINAC_DECLARE_PRINT_CONTEXT(print_csrc_double, print_csrc)
176 print_csrc_double(std::ostream &, unsigned options = 0);
179 /** Context for C source output using CLN numbers. */
180 class print_csrc_cl_N : public print_csrc
182 GINAC_DECLARE_PRINT_CONTEXT(print_csrc_cl_N, print_csrc)
184 print_csrc_cl_N(std::ostream &, unsigned options = 0);
187 /** Check if obj is a T, including base classes. */
189 inline bool is_a(const print_context & obj)
190 { return dynamic_cast<const T *>(&obj) != nullptr; }
195 /** Base class for print_functor handlers */
196 class print_functor_impl {
198 virtual ~print_functor_impl() {}
199 virtual print_functor_impl *duplicate() const = 0;
200 virtual void operator()(const basic & obj, const print_context & c, unsigned level) const = 0;
203 /** print_functor handler for pointer-to-functions of class T, context type C */
204 template <class T, class C>
205 class print_ptrfun_handler : public print_functor_impl {
207 typedef void (*F)(const T &, const C &, unsigned);
209 print_ptrfun_handler(F f_) : f(f_) {}
210 print_ptrfun_handler *duplicate() const override { return new print_ptrfun_handler(*this); }
212 void operator()(const basic & obj, const print_context & c, unsigned level) const override
214 // Call the supplied function
215 f(dynamic_cast<const T &>(obj), dynamic_cast<const C &>(c), level);
222 /** print_functor handler for member functions of class T, context type C */
223 template <class T, class C>
224 class print_memfun_handler : public print_functor_impl {
226 typedef void (T::*F)(const C & c, unsigned level) const;
228 print_memfun_handler(F f_) : f(f_) {}
229 print_memfun_handler *duplicate() const override { return new print_memfun_handler(*this); }
231 void operator()(const basic & obj, const print_context & c, unsigned level) const override
233 // Call the supplied member function
234 return (dynamic_cast<const T &>(obj).*f)(dynamic_cast<const C &>(c), level);
241 /** This class represents a print method for a certain algebraic class and
242 * print_context type. Its main purpose is to hide the difference between
243 * member functions and nonmember functions behind one unified operator()
244 * interface. print_functor has value semantics and acts as a smart pointer
245 * (with deep copy) to a class derived from print_functor_impl which
246 * implements the actual function call. */
247 class print_functor {
249 print_functor() : impl(nullptr) {}
250 print_functor(const print_functor & other) : impl(other.impl.get() ? other.impl->duplicate() : 0) {}
251 print_functor(std::unique_ptr<print_functor_impl> impl_) : impl(std::move(impl_)) {}
253 template <class T, class C>
254 print_functor(void f(const T &, const C &, unsigned)) : impl(new print_ptrfun_handler<T, C>(f)) {}
256 template <class T, class C>
257 print_functor(void (T::*f)(const C &, unsigned) const) : impl(new print_memfun_handler<T, C>(f)) {}
259 print_functor & operator=(const print_functor & other)
261 if (this != &other) {
262 print_functor_impl *p = other.impl.get();
263 impl.reset(p ? other.impl->duplicate() : nullptr);
268 void operator()(const basic & obj, const print_context & c, unsigned level) const
270 (*impl)(obj, c, level);
273 bool is_valid() const { return impl.get(); }
276 std::unique_ptr<print_functor_impl> impl;
282 #endif // ndef GINAC_BASIC_H