MADNESS  0.10.1
print.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 */
31 
32 #ifndef MADNESS_WORLD_PRINT_H__INCLUDED
33 #define MADNESS_WORLD_PRINT_H__INCLUDED
34 
35 /**
36  \file print.h
37  \brief Defines simple templates for printing to \c std::cout "a la Python".
38  \ingroup libraries
39 */
40 
41 #include <type_traits>
42 #include <iostream>
43 #include <complex>
44 #include <list>
45 #include <vector>
48 
49 #ifdef BRAINDEAD
50 // Cray XT nonsense
51 #define ENDL "\n"
52 
53 #else
54 
55 #define ENDL std::endl
56 
57 #endif
58 
59 
60 namespace madness {
61 
62 namespace operators {
63 
64 /// \addtogroup libraries
65 /// @{
66 
67 /// Easy printing of complex numbers.
68 
69 /// \tparam T The "real" type of the complex number.
70 /// \param[in,out] s The output stream.
71 /// \param[in] c The complex number.
72 /// \return The output stream (for chaining).
73 template <typename T>
74 std::ostream &operator<<(std::ostream &s, const std::complex<T> &c) {
75  s << c.real() << "+" << c.imag() << "j";
76  return s;
77 }
78 
79 /// Easy printing of pairs.
80 
81 /// \tparam T Type 1 of the pair.
82 /// \tparam U Type 2 of the pair.
83 /// \param[in,out] s The output stream.
84 /// \param[in] p The pair.
85 /// \return The output stream (for chaining).
86 template <typename T, typename U>
87 std::ostream &operator<<(std::ostream &s, const std::pair<T, U> &p) {
88  s << "(" << p.first << "," << p.second << ")";
89  return s;
90 }
91 
92 ///// Easy printing of std::arrays.
93 //
94 ///// \tparam U Type 2 of the pair.
95 ///// \param[in,out] s The output stream.
96 ///// \param[in] p The pair.
97 ///// \return The output stream (for chaining).
98 //template <typename T, std::size_t NDIM>
99 //std::ostream &operator<<(std::ostream &s, const std::array<T, NDIM> &p) {
100 // s << "[";
101 // for (int i=0; i<p.size(); ++i) s << p[i];
102 // s << "]";
103 // return s;
104 //}
105 
106 /// Easy printing of lists.
107 
108 /// \tparam T Type stored in the list.
109 /// \param[in,out] s The output stream.
110 /// \param[in] c The list.
111 /// \return The output stream (for chaining).
112 template <typename T>
113 std::ostream &operator<<(std::ostream &s, const std::list<T> &c) {
114  s << "[";
115  typename std::list<T>::const_iterator it = c.begin();
116  while (it != c.end()) {
117  s << *it;
118  ++it;
119  if (it != c.end())
120  s << ", ";
121  };
122  s << "]";
123  return s;
124 }
125 
126 /// Easy printing of vectors.
127 
128 /// \tparam T Type stored in the vector.
129 /// \param[in,out] s The output stream.
130 /// \param[in] c The vector.
131 /// \return The output stream (for chaining).
132 template <typename T>
133 std::ostream &operator<<(std::ostream &s, const std::vector<T> &c) {
134  s << "[";
135  typename std::vector<T>::const_iterator it = c.begin();
136  while (it != c.end()) {
137  s << *it;
138  ++it;
139  if (it != c.end())
140  s << ", ";
141  };
142  s << "]";
143  return s;
144 }
145 
146 /// Easy printing of fixed dimension arrays.
147 
148 /// STL I/O already does char (thus the \c enable_if business).
149 /// \tparam T Type of data in the array.
150 /// \tparam N Size of the array.
151 /// \param[in,out] s The output stream.
152 /// \param[in] v The array.
153 /// \return The output stream (for chaining).
154 template <typename T, std::size_t N>
155 typename std::enable_if<!std::is_same<T, char>::value, std::ostream &>::type
156 operator<<(std::ostream &s, const T (&v)[N]) {
157  s << "[";
158  for (std::size_t i = 0; i < N; ++i) {
159  s << v[i];
160  if (i != (N - 1))
161  s << ",";
162  }
163  s << "]";
164  return s;
165 }
166 
167 } // namespace operators
168 
169  /// big section heading
170  void print_header1(const std::string& s);
171 
172  /// medium section heading
173  void print_header2(const std::string& s);
174 
175  /// small section heading
176  void print_header3(const std::string& s);
177 
178  /// Print a string justified on the left to start at the given column with optional underlining.
179  void print_justified(const char* s, int column=0, bool underline=true);
180 
181  /// Print a string centered at the given column with optional underlining.
182  void print_centered(const char* s, int column=40, bool underline=true);
183 
184  ///
185  void printf_msg_energy_time(const std::string msg, const double energy, const double time);
186 
187  // the "print" function and functions to help handle the variadic templates
188 
189  /// Helper function for \c print. Base case.
190 
191  /// This gets called recursively when there are no items left to print.
192  /// \param[in,out] out Output stream.
193  /// \return The output stream (for chaining).
194  inline std::ostream& print_helper(std::ostream& out) {
195  return out;
196  }
197 
198  /// \brief Helper function for \c print. Prints the first item (\c t) and
199  /// recursively passes on the other items.
200 
201  /// \tparam T Type of the item to print in this call.
202  /// \tparam Ts Argument pack type for the remaining items.
203  /// \param[in,out] out Output stream.
204  /// \param[in] t The item to print in this call.
205  /// \param[in] ts The remaining items in the argument pack (they get
206  /// recursively passed on).
207  /// \return The output stream (for chaining).
208  template <typename T, typename... Ts>
209  inline std::ostream& print_helper(std::ostream& out,
210  const T& t, const Ts&... ts) {
211  using madness::operators::operator<<;
212  out << ' ' << t;
213  return print_helper(out, ts...);
214  }
215 
216  /// \brief Print items to \c std::cout (items separated by spaces) and
217  /// terminate with a new line
218 
219  /// The first item is printed here so that it isn't preceded by a space.
220  /// \tparam T Type of the first item to be printed.
221  /// \tparam Ts Argument pack type for the items to be printed.
222  /// \param[in] t The first item to be printed.
223  /// \param[in] ts The remaining items to be printed in the argument pack.
224  template<typename T, typename... Ts>
225  void print(const T& t, const Ts&... ts) {
226  using madness::operators::operator<<;
228  std::cout << t;
229  print_helper(std::cout, ts...) << ENDL;
230  }
231 
232  /// \brief Print items to \c std::cerr (items separated by spaces) and
233  /// terminate with a new line
234 
235  /// The first item is printed here so that it isn't preceded by a space.
236  /// \tparam T Type of the first item to be printed.
237  /// \tparam Ts Argument pack type for the items to be printed.
238  /// \param[in] t The first item to be printed.
239  /// \param[in] ts The remaining items to be printed in the argument pack.
240  template<typename T, typename... Ts>
241  void print_error(const T& t, const Ts&... ts) {
242  using madness::operators::operator<<;
244  std::cerr << t;
245  print_helper(std::cerr, ts...) << ENDL;
246  }
247 
248  /// @}
249 
250 }
251 #endif // MADNESS_WORLD_PRINT_H__INCLUDED
Supplements to the std::array class, such as I/O operations, for convenience.
Mutex that is applied/released at start/end of a scope.
Definition: worldmutex.h:239
double(* energy)()
Definition: derivatives.cc:58
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition: derivatives.cc:72
auto T(World &world, response_space &f) -> response_space
Definition: global_functions.cc:34
static const double v
Definition: hatom_sf_dirac.cc:20
Mutex printmutex
Definition: worldmutex.cc:146
std::ostream & operator<<(std::ostream &s, const std::array< T, N > &a)
Output std::array to stream for human consumption.
Definition: array_addons.h:59
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
void print_header2(const std::string &s)
medium section heading
Definition: print.cc:54
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition: print.h:225
void print_justified(const char *s, int column, bool underline)
Print a string justified on the left to start at the given column with optional underlining.
Definition: print.cc:75
void printf_msg_energy_time(const std::string msg, const double energy, const double time)
Definition: print.cc:70
std::string type(const PairType &n)
Definition: PNOParameters.h:18
void print_header1(const std::string &s)
big section heading
Definition: print.cc:44
void print_centered(const char *s, int column, bool underline)
Print a string centered at the given column with optional underlining.
Definition: print.cc:85
void print_header3(const std::string &s)
small section heading
Definition: print.cc:63
std::ostream & print_helper(std::ostream &out)
Helper function for print. Base case.
Definition: print.h:194
void print_error(const T &t, const Ts &... ts)
Print items to std::cerr (items separated by spaces) and terminate with a new line.
Definition: print.h:241
#define ENDL
Definition: print.h:55
static const double c
Definition: relops.cc:10
#define N
Definition: testconv.cc:37
Implements Mutex, MutexFair, Spinlock, ConditionVariable.