Some defensive programming at destruction time, and fix a latent dangling pointer...
[cvc5.git] / src / printer / printer.h
1 /********************* */
2 /*! \file printer.h
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): Andrew Reynolds
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
11 **
12 ** \brief Base of the pretty-printer interface
13 **
14 ** Base of the pretty-printer interface.
15 **/
16
17 #include "cvc4_private.h"
18
19 #ifndef __CVC4__PRINTER__PRINTER_H
20 #define __CVC4__PRINTER__PRINTER_H
21
22 #include "util/language.h"
23 #include "util/sexpr.h"
24 #include "util/model.h"
25 #include "expr/node.h"
26 #include "expr/command.h"
27
28 namespace CVC4 {
29
30 class Printer {
31 /** Printers for each OutputLanguage */
32 static Printer* d_printers[language::output::LANG_MAX];
33
34 /** Make a Printer for a given OutputLanguage */
35 static Printer* makePrinter(OutputLanguage lang) throw();
36
37 // disallow copy, assignment
38 Printer(const Printer&) CVC4_UNUSED;
39 Printer& operator=(const Printer&) CVC4_UNUSED;
40
41 protected:
42 // derived classes can construct, but no one else.
43 Printer() throw() {}
44
45 /** write model response to command */
46 virtual void toStream(std::ostream& out, const Model& m, const Command* c) const throw() = 0;
47
48 /** write model response to command using another language printer */
49 void toStreamUsing(OutputLanguage lang, std::ostream& out, const Model& m, const Command* c) const throw() {
50 getPrinter(lang)->toStream(out, m, c);
51 }
52
53 public:
54 /** Get the Printer for a given OutputLanguage */
55 static Printer* getPrinter(OutputLanguage lang) throw() {
56 if(lang == language::output::LANG_AUTO) {
57 // Infer the language to use for output.
58 //
59 // Options can be null in certain circumstances (e.g., when printing
60 // the singleton "null" expr. So we guard against segfault
61 if(&Options::current() != NULL) {
62 if(options::outputLanguage.wasSetByUser()) {
63 lang = options::outputLanguage();
64 }
65 if(lang == language::output::LANG_AUTO && options::inputLanguage.wasSetByUser()) {
66 lang = language::toOutputLanguage(options::inputLanguage());
67 }
68 }
69 if(lang == language::output::LANG_AUTO) {
70 lang = language::output::LANG_CVC4; // default
71 }
72 }
73 if(d_printers[lang] == NULL) {
74 d_printers[lang] = makePrinter(lang);
75 }
76 return d_printers[lang];
77 }
78
79 /** Write a Node out to a stream with this Printer. */
80 virtual void toStream(std::ostream& out, TNode n,
81 int toDepth, bool types, size_t dag) const throw() = 0;
82
83 /** Write a Command out to a stream with this Printer. */
84 virtual void toStream(std::ostream& out, const Command* c,
85 int toDepth, bool types, size_t dag) const throw() = 0;
86
87 /** Write a CommandStatus out to a stream with this Printer. */
88 virtual void toStream(std::ostream& out, const CommandStatus* s) const throw() = 0;
89
90 /** Write an SExpr out to a stream with this Printer. */
91 virtual void toStream(std::ostream& out, const SExpr& sexpr) const throw();
92
93 /**
94 * Write a Result out to a stream with this Printer.
95 *
96 * The default implementation writes a reasonable string in lowercase
97 * for sat, unsat, valid, invalid, or unknown results. This behavior
98 * is overridable by each Printer, since sometimes an output language
99 * has a particular preference for how results should appear.
100 */
101 virtual void toStream(std::ostream& out, const Result& r) const throw();
102
103 /** Write a Model out to a stream with this Printer. */
104 virtual void toStream(std::ostream& out, const Model& m) const throw();
105
106 };/* class Printer */
107
108 /**
109 * IOStream manipulator to pretty-print SExprs.
110 */
111 class PrettySExprs {
112 /**
113 * The allocated index in ios_base for our setting.
114 */
115 static const int s_iosIndex;
116
117 /**
118 * When this manipulator is used, the setting is stored here.
119 */
120 bool d_prettySExprs;
121
122 public:
123 /**
124 * Construct a PrettySExprs with the given setting.
125 */
126 PrettySExprs(bool prettySExprs) : d_prettySExprs(prettySExprs) {}
127
128 inline void applyPrettySExprs(std::ostream& out) {
129 out.iword(s_iosIndex) = d_prettySExprs;
130 }
131
132 static inline bool getPrettySExprs(std::ostream& out) {
133 return out.iword(s_iosIndex);
134 }
135
136 static inline void setPrettySExprs(std::ostream& out, bool prettySExprs) {
137 out.iword(s_iosIndex) = prettySExprs;
138 }
139
140 /**
141 * Set the pretty-sexprs state on the output stream for the current
142 * stack scope. This makes sure the old state is reset on the
143 * stream after normal OR exceptional exit from the scope, using the
144 * RAII C++ idiom.
145 */
146 class Scope {
147 std::ostream& d_out;
148 bool d_oldPrettySExprs;
149
150 public:
151
152 inline Scope(std::ostream& out, bool prettySExprs) :
153 d_out(out),
154 d_oldPrettySExprs(PrettySExprs::getPrettySExprs(out)) {
155 PrettySExprs::setPrettySExprs(out, prettySExprs);
156 }
157
158 inline ~Scope() {
159 PrettySExprs::setPrettySExprs(d_out, d_oldPrettySExprs);
160 }
161
162 };/* class PrettySExprs::Scope */
163
164 };/* class PrettySExprs */
165
166 /**
167 * Sets the default pretty-sexprs setting for an ostream. Use like this:
168 *
169 * // let out be an ostream, s an SExpr
170 * out << PrettySExprs(true) << s << endl;
171 *
172 * The setting stays permanently (until set again) with the stream.
173 */
174 inline std::ostream& operator<<(std::ostream& out, PrettySExprs ps) {
175 ps.applyPrettySExprs(out);
176 return out;
177 }
178
179 }/* CVC4 namespace */
180
181 #endif /* __CVC4__PRINTER__PRINTER_H */
182