GiNaC  1.8.0
print.h
Go to the documentation of this file.
1 
5 /*
6  * GiNaC Copyright (C) 1999-2020 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 
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;
46  const char *parent_name;
47  unsigned id;
48 };
49 
51 
52 
55 public:
56  enum {
57  print_index_dimensions = 0x0001
58  };
59 };
60 
61 
63 #define GINAC_DECLARE_PRINT_CONTEXT_COMMON(classname) \
64 public: \
65  friend class function_options; \
66  friend class registered_class_options; \
67  static const GiNaC::print_context_class_info &get_class_info_static(); \
68  classname();
69 
70 #define GINAC_DECLARE_PRINT_CONTEXT_BASE(classname) \
71  GINAC_DECLARE_PRINT_CONTEXT_COMMON(classname) \
72  virtual const GiNaC::print_context_class_info &get_class_info() const { return classname::get_class_info_static(); } \
73  virtual const char *class_name() const { return classname::get_class_info_static().options.get_name(); } \
74  virtual classname * duplicate() const { return new classname(*this); } \
75 private:
76 
81 #define GINAC_DECLARE_PRINT_CONTEXT(classname, supername) \
82  GINAC_DECLARE_PRINT_CONTEXT_COMMON(classname) \
83  typedef supername inherited; \
84  const GiNaC::print_context_class_info &get_class_info() const override { return classname::get_class_info_static(); } \
85  const char *class_name() const override { return classname::get_class_info_static().options.get_name(); } \
86  classname * duplicate() const override { return new classname(*this); } \
87 private:
88 
90 #define GINAC_IMPLEMENT_PRINT_CONTEXT(classname, supername) \
91 const GiNaC::print_context_class_info &classname::get_class_info_static() \
92 { \
93  static GiNaC::print_context_class_info reg_info = GiNaC::print_context_class_info(GiNaC::print_context_options(#classname, #supername, GiNaC::next_print_context_id++)); \
94  return reg_info; \
95 }
96 
97 
98 extern unsigned next_print_context_id;
99 
100 
103 {
105 public:
106  print_context(std::ostream &, unsigned options = 0);
107  virtual ~print_context() {}
108 
109  std::ostream & s;
110  unsigned options;
111 };
112 
114 class print_dflt : public print_context
115 {
117 public:
118  print_dflt(std::ostream &, unsigned options = 0);
119 };
120 
123 {
125 public:
126  print_latex(std::ostream &, unsigned options = 0);
127 };
128 
131 {
133 public:
134  print_python(std::ostream &, unsigned options = 0);
135 };
136 
139 {
141 public:
142  print_python_repr(std::ostream &, unsigned options = 0);
143 };
144 
146 class print_tree : public print_context
147 {
149 public:
150  print_tree(unsigned d);
151  print_tree(std::ostream &, unsigned options = 0, unsigned d = 4);
152 
153  const unsigned delta_indent;
154 };
155 
157 class print_csrc : public print_context
158 {
160 public:
161  print_csrc(std::ostream &, unsigned options = 0);
162 };
163 
166 {
168 public:
169  print_csrc_float(std::ostream &, unsigned options = 0);
170 };
171 
174 {
176 public:
177  print_csrc_double(std::ostream &, unsigned options = 0);
178 };
179 
182 {
184 public:
185  print_csrc_cl_N(std::ostream &, unsigned options = 0);
186 };
187 
189 template <class T>
190 inline bool is_a(const print_context & obj)
191 { return dynamic_cast<const T *>(&obj) != nullptr; }
192 
193 
194 class basic;
195 
198 public:
199  virtual ~print_functor_impl() {}
200  virtual print_functor_impl *duplicate() const = 0;
201  virtual void operator()(const basic & obj, const print_context & c, unsigned level) const = 0;
202 };
203 
205 template <class T, class C>
207 public:
208  typedef void (*F)(const T &, const C &, unsigned);
209 
210  print_ptrfun_handler(F f_) : f(f_) {}
211  print_ptrfun_handler *duplicate() const override { return new print_ptrfun_handler(*this); }
212 
213  void operator()(const basic & obj, const print_context & c, unsigned level) const override
214  {
215  // Call the supplied function
216  f(dynamic_cast<const T &>(obj), dynamic_cast<const C &>(c), level);
217  }
218 
219 private:
220  F f;
221 };
222 
224 template <class T, class C>
226 public:
227  typedef void (T::*F)(const C & c, unsigned level) const;
228 
229  print_memfun_handler(F f_) : f(f_) {}
230  print_memfun_handler *duplicate() const override { return new print_memfun_handler(*this); }
231 
232  void operator()(const basic & obj, const print_context & c, unsigned level) const override
233  {
234  // Call the supplied member function
235  return (dynamic_cast<const T &>(obj).*f)(dynamic_cast<const C &>(c), level);
236  }
237 
238 private:
239  F f;
240 };
241 
249 public:
250  print_functor() : impl(nullptr) {}
251  print_functor(const print_functor & other) : impl(other.impl.get() ? other.impl->duplicate() : 0) {}
252  print_functor(std::unique_ptr<print_functor_impl> impl_) : impl(std::move(impl_)) {}
253 
254  template <class T, class C>
255  print_functor(void f(const T &, const C &, unsigned)) : impl(new print_ptrfun_handler<T, C>(f)) {}
256 
257  template <class T, class C>
258  print_functor(void (T::*f)(const C &, unsigned) const) : impl(new print_memfun_handler<T, C>(f)) {}
259 
261  {
262  if (this != &other) {
263  print_functor_impl *p = other.impl.get();
264  impl.reset(p ? other.impl->duplicate() : nullptr);
265  }
266  return *this;
267  }
268 
269  void operator()(const basic & obj, const print_context & c, unsigned level) const
270  {
271  (*impl)(obj, c, level);
272  }
273 
274  bool is_valid() const { return impl.get(); }
275 
276 private:
277  std::unique_ptr<print_functor_impl> impl;
278 };
279 
280 
281 } // namespace GiNaC
282 
283 #endif // ndef GINAC_BASIC_H
GiNaC::print_dflt
Context for default (ginsh-parsable) output.
Definition: print.h:115
GiNaC::is_a
bool is_a(const basic &obj)
Check if obj is a T, including base classes.
Definition: basic.h:313
GiNaC::print_python_repr
Context for python-parsable output.
Definition: print.h:139
GiNaC::print_ptrfun_handler::duplicate
print_ptrfun_handler * duplicate() const override
Definition: print.h:211
class_info.h
Helper templates to provide per-class information for class hierarchies.
GiNaC::print_context::print_context
print_context(std::ostream &, unsigned options=0)
Definition: print.cpp:44
GiNaC::print_options::print_index_dimensions
@ print_index_dimensions
print the dimensions of indices
Definition: print.h:57
GiNaC::print_csrc_cl_N::print_csrc_cl_N
print_csrc_cl_N(std::ostream &, unsigned options=0)
Definition: print.cpp:91
GiNaC::print_csrc_float
Context for C source output using float precision.
Definition: print.h:166
GiNaC::print_tree::print_tree
print_tree(unsigned d)
Definition: print.cpp:71
GiNaC::print_memfun_handler::f
F f
Definition: print.h:239
GiNaC::print_functor::is_valid
bool is_valid() const
Definition: print.h:274
GiNaC::print_context
Base class for print_contexts.
Definition: print.h:103
GiNaC::print_memfun_handler::F
void(T::* F)(const C &c, unsigned level) const
Definition: print.h:227
GiNaC::print_csrc
Base context for C source output.
Definition: print.h:158
GiNaC::print_options
Flags to control the behavior of a print_context.
Definition: print.h:54
GiNaC::print_dflt::print_dflt
print_dflt(std::ostream &, unsigned options=0)
Definition: print.cpp:49
GiNaC::print_functor::print_functor
print_functor()
Definition: print.h:250
GiNaC::print_context::~print_context
virtual ~print_context()
Definition: print.h:107
GINAC_DECLARE_PRINT_CONTEXT
#define GINAC_DECLARE_PRINT_CONTEXT(classname, supername)
Macro for inclusion in the declaration of a print_context class.
Definition: print.h:81
GINAC_DECLARE_PRINT_CONTEXT_BASE
#define GINAC_DECLARE_PRINT_CONTEXT_BASE(classname)
Definition: print.h:70
GiNaC::print_ptrfun_handler
print_functor handler for pointer-to-functions of class T, context type C
Definition: print.h:206
GiNaC::print_ptrfun_handler::print_ptrfun_handler
print_ptrfun_handler(F f_)
Definition: print.h:210
GiNaC::print_functor::print_functor
print_functor(std::unique_ptr< print_functor_impl > impl_)
Definition: print.h:252
GiNaC::print_context_options::id
unsigned id
ID number (assigned automatically).
Definition: print.h:47
GiNaC
Definition: add.cpp:38
GiNaC::print_context_class_info
class_info< print_context_options > print_context_class_info
Definition: print.h:50
GiNaC::print_functor_impl::operator()
virtual void operator()(const basic &obj, const print_context &c, unsigned level) const =0
GiNaC::print_context_options::name
const char * name
Class name.
Definition: print.h:45
GiNaC::print_functor_impl
Base class for print_functor handlers.
Definition: print.h:197
GiNaC::print_tree::delta_indent
const unsigned delta_indent
size of indentation step
Definition: print.h:153
GiNaC::print_ptrfun_handler::operator()
void operator()(const basic &obj, const print_context &c, unsigned level) const override
Definition: print.h:213
GiNaC::print_functor::print_functor
print_functor(const print_functor &other)
Definition: print.h:251
GiNaC::print_functor::operator=
print_functor & operator=(const print_functor &other)
Definition: print.h:260
GiNaC::print_csrc_double::print_csrc_double
print_csrc_double(std::ostream &, unsigned options=0)
Definition: print.cpp:86
GiNaC::print_python
Context for python pretty-print output.
Definition: print.h:131
GiNaC::print_context_options::get_id
unsigned get_id() const
Definition: print.h:42
GiNaC::print_context_options::parent_name
const char * parent_name
Name of superclass.
Definition: print.h:46
GiNaC::print_functor_impl::duplicate
virtual print_functor_impl * duplicate() const =0
GiNaC::print_memfun_handler::operator()
void operator()(const basic &obj, const print_context &c, unsigned level) const override
Definition: print.h:232
GiNaC::print_latex
Context for latex-parsable output.
Definition: print.h:123
GiNaC::print_functor::print_functor
print_functor(void f(const T &, const C &, unsigned))
Definition: print.h:255
GiNaC::print_context_options
This class stores information about a registered print_context class.
Definition: print.h:35
GiNaC::next_print_context_id
unsigned next_print_context_id
Next free ID for print_context types.
Definition: print.cpp:30
GiNaC::print_context_options::get_parent_name
const char * get_parent_name() const
Definition: print.h:41
GiNaC::print_csrc_double
Context for C source output using double precision.
Definition: print.h:174
GiNaC::print_tree
Context for tree-like output for debugging.
Definition: print.h:147
GiNaC::print_functor_impl::~print_functor_impl
virtual ~print_functor_impl()
Definition: print.h:199
c
size_t c
Definition: factor.cpp:770
GiNaC::print_context_options::print_context_options
print_context_options(const char *n, const char *p, unsigned i)
Definition: print.h:37
GiNaC::print_ptrfun_handler::F
void(* F)(const T &, const C &, unsigned)
Definition: print.h:208
GiNaC::print_functor::operator()
void operator()(const basic &obj, const print_context &c, unsigned level) const
Definition: print.h:269
std
Definition: ex.h:972
GiNaC::print_memfun_handler::duplicate
print_memfun_handler * duplicate() const override
Definition: print.h:230
GiNaC::print_csrc::print_csrc
print_csrc(std::ostream &, unsigned options=0)
Definition: print.cpp:76
n
size_t n
Definition: factor.cpp:1463
GiNaC::print_ptrfun_handler::f
F f
Definition: print.h:220
GiNaC::print_context::s
std::ostream & s
stream to output to
Definition: print.h:109
GiNaC::print_latex::print_latex
print_latex(std::ostream &, unsigned options=0)
Definition: print.cpp:54
GiNaC::basic
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition: basic.h:105
GiNaC::class_info
Definition: class_info.h:41
GiNaC::print_csrc_cl_N
Context for C source output using CLN numbers.
Definition: print.h:182
GiNaC::print_context::options
unsigned options
option flags
Definition: print.h:110
GiNaC::print_functor::impl
std::unique_ptr< print_functor_impl > impl
Definition: print.h:277
GiNaC::print_memfun_handler
print_functor handler for member functions of class T, context type C
Definition: print.h:225
GiNaC::print_functor::print_functor
print_functor(void(T::*f)(const C &, unsigned) const)
Definition: print.h:258
GiNaC::print_python::print_python
print_python(std::ostream &, unsigned options=0)
Definition: print.cpp:59
GiNaC::print_csrc_float::print_csrc_float
print_csrc_float(std::ostream &, unsigned options=0)
Definition: print.cpp:81
GiNaC::print_functor
This class represents a print method for a certain algebraic class and print_context type.
Definition: print.h:248
GiNaC::print_python_repr::print_python_repr
print_python_repr(std::ostream &, unsigned options=0)
Definition: print.cpp:64
GiNaC::print_context_options::get_name
const char * get_name() const
Definition: print.h:40
GiNaC::print_memfun_handler::print_memfun_handler
print_memfun_handler(F f_)
Definition: print.h:229

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.