1 /*********************                                                        */
2 /*! \file expr_iomanip.cpp
3  ** \verbatim
4  ** Top contributors (to current version):
5  **   Tim King, Morgan Deters, Kshitij Bansal
6  ** This file is part of the CVC4 project.
7  ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8  ** in the top-level source directory) and their institutional affiliations.
9  ** All rights reserved.  See the file COPYING in the top-level source
10  ** directory for licensing information.\endverbatim
11  **
12  ** \brief Expr IO manipulation classes.
13  **
14  ** Expr IO manipulation classes.
15  **/
16 
17 #include "expr/expr_iomanip.h"
18 
19 #include <iomanip>
20 #include <iostream>
21 
22 #include "options/options.h"
23 #include "options/expr_options.h"
24 
25 namespace CVC4 {
26 namespace expr {
27 
28 const int ExprSetDepth::s_iosIndex = std::ios_base::xalloc();
29 const int ExprPrintTypes::s_iosIndex = std::ios_base::xalloc();
30 const int ExprDag::s_iosIndex = std::ios_base::xalloc();
31 
32 
33 
ExprSetDepth(long depth)34 ExprSetDepth::ExprSetDepth(long depth) : d_depth(depth) {}
35 
applyDepth(std::ostream & out)36 void ExprSetDepth::applyDepth(std::ostream& out) {
37   out.iword(s_iosIndex) = d_depth;
38 }
39 
getDepth(std::ostream & out)40 long ExprSetDepth::getDepth(std::ostream& out) {
41   long& l = out.iword(s_iosIndex);
42   if(l == 0) {
43     // set the default print depth on this ostream
44     if(not Options::isCurrentNull()) {
45       l = options::defaultExprDepth();
46     }
47     if(l == 0) {
48       // if called from outside the library, we may not have options
49       // available to us at this point (or perhaps the output language
50       // is not set in Options).  Default to something reasonable, but
51       // don't set "l" since that would make it "sticky" for this
52       // stream.
53       return s_defaultPrintDepth;
54     }
55   }
56   return l;
57 }
58 
setDepth(std::ostream & out,long depth)59 void ExprSetDepth::setDepth(std::ostream& out, long depth) {
60   out.iword(s_iosIndex) = depth;
61 }
62 
63 
Scope(std::ostream & out,long depth)64 ExprSetDepth::Scope::Scope(std::ostream& out, long depth)
65   : d_out(out), d_oldDepth(ExprSetDepth::getDepth(out))
66 {
67   ExprSetDepth::setDepth(out, depth);
68 }
69 
~Scope()70 ExprSetDepth::Scope::~Scope() {
71   ExprSetDepth::setDepth(d_out, d_oldDepth);
72 }
73 
74 
ExprPrintTypes(bool printTypes)75 ExprPrintTypes::ExprPrintTypes(bool printTypes) : d_printTypes(printTypes) {}
76 
applyPrintTypes(std::ostream & out)77 void ExprPrintTypes::applyPrintTypes(std::ostream& out) {
78   out.iword(s_iosIndex) = d_printTypes;
79 }
80 
getPrintTypes(std::ostream & out)81 bool ExprPrintTypes::getPrintTypes(std::ostream& out) {
82   return out.iword(s_iosIndex);
83 }
84 
setPrintTypes(std::ostream & out,bool printTypes)85 void ExprPrintTypes::setPrintTypes(std::ostream& out, bool printTypes) {
86   out.iword(s_iosIndex) = printTypes;
87 }
88 
Scope(std::ostream & out,bool printTypes)89 ExprPrintTypes::Scope::Scope(std::ostream& out, bool printTypes)
90   : d_out(out),
91     d_oldPrintTypes(ExprPrintTypes::getPrintTypes(out)) {
92   ExprPrintTypes::setPrintTypes(out, printTypes);
93 }
94 
~Scope()95 ExprPrintTypes::Scope::~Scope() {
96   ExprPrintTypes::setPrintTypes(d_out, d_oldPrintTypes);
97 }
98 
ExprDag(bool dag)99 ExprDag::ExprDag(bool dag) : d_dag(dag ? 1 : 0) {}
100 
ExprDag(int dag)101 ExprDag::ExprDag(int dag) : d_dag(dag < 0 ? 0 : dag) {}
102 
applyDag(std::ostream & out)103 void ExprDag::applyDag(std::ostream& out) {
104   // (offset by one to detect whether default has been set yet)
105   out.iword(s_iosIndex) = static_cast<long>(d_dag) + 1;
106 }
107 
getDag(std::ostream & out)108 size_t ExprDag::getDag(std::ostream& out) {
109   long& l = out.iword(s_iosIndex);
110   if(l == 0) {
111     // set the default dag setting on this ostream
112     // (offset by one to detect whether default has been set yet)
113     if(not Options::isCurrentNull()) {
114       l = options::defaultDagThresh() + 1;
115     }
116     if(l == 0) {
117       // if called from outside the library, we may not have options
118       // available to us at this point (or perhaps the output language
119       // is not set in Options).  Default to something reasonable, but
120       // don't set "l" since that would make it "sticky" for this
121       // stream.
122       return s_defaultDag + 1;
123     }
124   }
125   return static_cast<size_t>(l - 1);
126 }
127 
setDag(std::ostream & out,size_t dag)128 void ExprDag::setDag(std::ostream& out, size_t dag) {
129   // (offset by one to detect whether default has been set yet)
130   out.iword(s_iosIndex) = static_cast<long>(dag) + 1;
131 }
132 
Scope(std::ostream & out,size_t dag)133 ExprDag::Scope::Scope(std::ostream& out, size_t dag)
134   : d_out(out),
135     d_oldDag(ExprDag::getDag(out)) {
136   ExprDag::setDag(out, dag);
137 }
138 
~Scope()139 ExprDag::Scope::~Scope() {
140   ExprDag::setDag(d_out, d_oldDag);
141 }
142 
operator <<(std::ostream & out,ExprDag d)143 std::ostream& operator<<(std::ostream& out, ExprDag d) {
144   d.applyDag(out);
145   return out;
146 }
147 
operator <<(std::ostream & out,ExprPrintTypes pt)148 std::ostream& operator<<(std::ostream& out, ExprPrintTypes pt) {
149   pt.applyPrintTypes(out);
150   return out;
151 }
152 
operator <<(std::ostream & out,ExprSetDepth sd)153 std::ostream& operator<<(std::ostream& out, ExprSetDepth sd) {
154   sd.applyDepth(out);
155   return out;
156 }
157 
158 
159 }/* namespace CVC4::expr */
160 }/* namespace CVC4 */
161