TiledArray  0.7.0
expr_trace.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2014 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * expr_trace.h
22  * Mar 16, 2014
23  *
24  */
25 
26 #ifndef TILEDARRAY_EXPR_TRACE_H__INCLUDED
27 #define TILEDARRAY_EXPR_TRACE_H__INCLUDED
28 
30 #include <iostream>
31 
32 namespace TiledArray {
33  namespace expressions {
34 
35  template <typename> class Expr;
36  template <typename, bool> class TsrExpr;
37 
39  class ExprOStream {
40  std::ostream& os_;
41  unsigned int tab_;
42 
43  public:
44 
46 
48  ExprOStream(std::ostream& os) : os_(os), tab_(0u) { }
49 
51 
53  ExprOStream(const ExprOStream& other) : os_(other.os_), tab_(other.tab_) { }
54 
56 
59  template <typename T>
60  std::ostream& operator <<(const T& t) {
61  for(unsigned int i = 0u; i < tab_; ++i) {
62  os_ << " ";
63  }
64 
65  os_ << t;
66  return os_;
67  }
68 
70  void inc() { ++tab_; }
71 
73  void dec() { --tab_; }
74 
76  std::ostream& get_stream() const { return os_; }
77 
78  }; // class ExprOStream
79 
81 
84  std::ostream& os_;
85  VariableList target_vars_;
86 
87  public:
89 
92  ExprTraceTarget(std::ostream& os, const std::string& target_vars) :
93  os_(os), target_vars_(target_vars)
94  { }
95 
97 
100  os_(other.os_), target_vars_(other.target_vars_)
101  { }
102 
104 
108  template <typename D>
109  std::ostream& operator<<(const Expr<D>& expr) const {
110  if(TiledArray::get_default_world().rank() == 0) {
111  os_ << target_vars_ << " =\n";
112 
113  ExprOStream expr_stream(os_);
114  expr_stream.inc();
115  expr.derived().print(expr_stream, target_vars_);
116  }
117 
118  return os_;
119  }
120 
121  }; // class ExprTrace
122 
124 
130  template <typename A, bool Alias>
131  inline ExprTraceTarget operator<<(std::ostream& os, const TsrExpr<A, Alias>& tsr) {
132  return ExprTraceTarget(os, tsr.vars());
133  }
134 
135  } // namespace expressions
136 } // namespace TiledArray
137 
138 #endif // TILEDARRAY_EXPR_TRACE_H__INCLUDED
std::ostream & get_stream() const
Output stream accessor.
Definition: expr_trace.h:76
ExprOStream(std::ostream &os)
Constructor.
Definition: expr_trace.h:48
ExprOStream(const ExprOStream &other)
Copy constructor.
Definition: expr_trace.h:53
Variable list manages a list variable strings.
ExprTraceTarget(std::ostream &os, const std::string &target_vars)
Constructor.
Definition: expr_trace.h:92
ExprTraceTarget(const ExprTraceTarget &other)
Copy constructor.
Definition: expr_trace.h:99
void inc()
Increment the number of tabs.
Definition: expr_trace.h:70
Expression output stream.
Definition: expr_trace.h:39
Expression trace target.
Definition: expr_trace.h:83
std::ostream & operator<<(const T &t)
Output operator.
Definition: expr_trace.h:60
void dec()
Decrement the number of tabs.
Definition: expr_trace.h:73