mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
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
33namespace mx
34{
35namespace ioutils
36{
37
38template <char space, bool flush, char eol>
39void pout()
40{
41 if( eol )
42 std::cout << eol;
43 if( flush )
44 std::cout.flush();
45
46 return;
47}
48
49/// A simple formatted output function.
50/** This function writes its arguments, of any type and of any number, to stdout. By default, the
51 * arguments are separated by a space, the new line '\\n' is written at the end, and the std::cout
52 * stream is flushed. These behaviors can be altered via template parameters.
53 *
54 * Example:
55 * \code
56 * std::string s="output:";
57 * double d=2.567;
58 * int i=3;
59 *
60 * pout(s,d,i);
61 * \endcode
62 * Note that the types of the values do not need to be specified as templated arguments.
63 * When run, this code results in
64 * \verbatim
65 * $ output: 2.567 3
66 * $
67 * \endverbatim
68 *
69 * The behavior can be changed with template arguments like
70 * \code
71 * pout<'\t', false, 0>(s,d,i);
72 * \endcode
73 * which would use the tab instead of space, and neither flush nor print a newline at the end.
74 *
75 * \tparam space is the character to place between the values, by default this is space.
76 * \tparam flush controls whether std::cout.flush() is called, by default it is true.
77 * \tparam eol is the character to print at end of line, by default it is '\\n'.
78 * \tparam valT a type which can be output by std::cout
79 * \tparam valTs a variadic list of additional types which can be output by std::cout
80 *
81 * \ingroup ioutils
82 */
83template <char space = ' ', bool flush = true, char eol = '\n', typename valT, typename... valTs>
84void pout( valT value, ///< [in] a value to print.
85 const valTs &...values ///< [in] a variadic list of additional values. Any number of values can be specified,
86 ///< with any type handled by std::cout.
87)
88{
89 static const unsigned short int nargs = sizeof...( valTs );
90
91 std::cout << value;
92
93 if( nargs > 0 )
94 std::cout << space;
95
96 pout<space, flush, eol>( values... );
97}
98
99} // namespace ioutils
100} // namespace mx
101
102#endif // ioutils_pout_hpp
The mxlib c++ namespace.
Definition mxError.hpp:106