mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
pout.hpp
Go to the documentation of this file.
1 /** \file pout.hpp
2  * \author Jared R. Males
3  * \brief Declaration and definition of a simple formatted output function.
4  * \ingroup utils_files
5  */
6 
7 //***********************************************************************//
8 // Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
9 //
10 // This file is part of mxlib.
11 //
12 // mxlib is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // mxlib is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
24 //***********************************************************************//
25 
26 #ifndef ioutils_pout_hpp
27 #define ioutils_pout_hpp
28 
29 #include <iostream>
30 
31 #include "../mxlib.hpp"
32 
33 namespace mx
34 {
35 namespace ioutils
36 {
37 
38 template<char space, bool flush, char eol>
39 void pout()
40 {
41  if(eol) std::cout << eol;
42  if(flush) std::cout.flush();
43 
44  return;
45 }
46 
47 ///A simple formatted output function.
48 /** This function writes its arguments, of any type and of any number, to stdout. By default, the
49  * arguments are separated by a space, the new line '\\n' is written at the end, and the std::cout
50  * stream is flushed. These behaviors can be altered via template parameters.
51  *
52  * Example:
53  * \code
54  * std::string s="output:";
55  * double d=2.567;
56  * int i=3;
57  *
58  * pout(s,d,i);
59  * \endcode
60  * Note that the types of the values do not need to be specified as templated arguments.
61  * When run, this code results in
62  * \verbatim
63  * $ output: 2.567 3
64  * $
65  * \endverbatim
66  *
67  * The behavior can be changed with template arguments like
68  * \code
69  * pout<'\t', false, 0>(s,d,i);
70  * \endcode
71  * which would use the tab instead of space, and neither flush nor print a newline at the end.
72  *
73  * \tparam space is the character to place between the values, by default this is space.
74  * \tparam flush controls whether std::cout.flush() is called, by default it is true.
75  * \tparam eol is the character to print at end of line, by default it is '\\n'.
76  * \tparam valT a type which can be output by std::cout
77  * \tparam valTs a variadic list of additional types which can be output by std::cout
78  *
79  * \ingroup ioutils
80  */
81 template<char space=' ', bool flush=true, char eol='\n', typename valT, typename... valTs>
82 void pout( valT value, ///< [in] a value to print.
83  const valTs&... values ///< [in] a variadic list of additional values. Any number of values can be specified, with any type handled by std::cout.
84  )
85 {
86  static const unsigned short int nargs = sizeof...(valTs);
87 
88  std::cout << value;
89 
90  if(nargs > 0) std::cout << space;
91 
92  pout<space,flush,eol>(values...);
93 
94 }
95 
96 } //namespace ioutils
97 } //namespace mx
98 
99 #endif //ioutils_pout_hpp
void pout(valT value, const valTs &... values)
A simple formatted output function.
Definition: pout.hpp:82
The mxlib c++ namespace.
Definition: mxError.hpp:107