]> www.ginac.de Git - ginac.git/blob - ginac/print.h
- added registry for print_context classes (use print_context_class_info::dump_hierar...
[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
29 #include "class_info.h"
30
31 namespace GiNaC {
32
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 private: \
71         static GiNaC::print_context_class_info reg_info; \
72 public: \
73         virtual const GiNaC::print_context_class_info &get_class_info() const { return reg_info; } \
74         virtual const char *class_name() const { return reg_info.options.get_name(); } \
75         \
76         classname(); \
77         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         GiNaC::print_context_class_info classname::reg_info = GiNaC::print_context_class_info(print_context_options(#classname, #supername, next_print_context_id++));
83
84 extern unsigned next_print_context_id;
85
86
87 /** Base class for print_contexts. */
88 class print_context
89 {
90         GINAC_DECLARE_PRINT_CONTEXT(print_context, void)
91 public:
92         print_context(std::ostream &, unsigned options = 0);
93         virtual ~print_context() {}
94
95         std::ostream & s; /**< stream to output to */
96         unsigned options; /**< option flags */
97 };
98
99 /** Context for default (ginsh-parsable) output. */
100 class print_dflt : public print_context
101 {
102         GINAC_DECLARE_PRINT_CONTEXT(print_dflt, print_context)
103 public:
104         print_dflt(std::ostream &, unsigned options = 0);
105 };
106
107 /** Context for latex-parsable output. */
108 class print_latex : public print_context
109 {
110         GINAC_DECLARE_PRINT_CONTEXT(print_latex, print_context)
111 public:
112         print_latex(std::ostream &, unsigned options = 0);
113 };
114
115 /** Context for python pretty-print output. */
116 class print_python : public print_context
117 {
118         GINAC_DECLARE_PRINT_CONTEXT(print_python, print_context)
119 public:
120         print_python(std::ostream &, unsigned options = 0);
121 };
122
123 /** Context for python-parsable output. */
124 class print_python_repr : public print_context
125 {
126         GINAC_DECLARE_PRINT_CONTEXT(print_python_repr, print_context)
127 public:
128         print_python_repr(std::ostream &, unsigned options = 0);
129 };
130
131 /** Context for tree-like output for debugging. */
132 class print_tree : public print_context
133 {
134         GINAC_DECLARE_PRINT_CONTEXT(print_tree, print_context)
135 public:
136         print_tree(unsigned d);
137         print_tree(std::ostream &, unsigned options = 0, unsigned d = 4);
138
139         const unsigned delta_indent; /**< size of indentation step */
140 };
141
142 /** Base context for C source output. */
143 class print_csrc : public print_context
144 {
145         GINAC_DECLARE_PRINT_CONTEXT(print_csrc, print_context)
146 public:
147         print_csrc(std::ostream &, unsigned options = 0);
148 };
149
150 /** Context for C source output using float precision. */
151 class print_csrc_float : public print_csrc
152 {
153         GINAC_DECLARE_PRINT_CONTEXT(print_csrc_float, print_csrc)
154 public:
155         print_csrc_float(std::ostream &, unsigned options = 0);
156 };
157
158 /** Context for C source output using double precision. */
159 class print_csrc_double : public print_csrc
160 {
161         GINAC_DECLARE_PRINT_CONTEXT(print_csrc_double, print_csrc)
162 public:
163         print_csrc_double(std::ostream &, unsigned options = 0);
164 };
165
166 /** Context for C source output using CLN numbers. */
167 class print_csrc_cl_N : public print_csrc
168 {
169         GINAC_DECLARE_PRINT_CONTEXT(print_csrc_cl_N, print_csrc)
170 public:
171         print_csrc_cl_N(std::ostream &, unsigned options = 0);
172 };
173
174 /** Check if obj is a T, including base classes. */
175 template <class T>
176 inline bool is_a(const print_context & obj)
177 { return dynamic_cast<const T *>(&obj) != 0; }
178
179 } // namespace GiNaC
180
181 #endif // ndef __GINAC_BASIC_H__