paludis Version 3.0.2
The one true package mangler
stream_holder.hh
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2013 Wouter van Kesteren <woutershep@gmail.com>
5 *
6 * This file is part of the Paludis package manager. Paludis is free software;
7 * you can redistribute it and/or modify it under the terms of the GNU General
8 * Public License version 2, as published by the Free Software Foundation.
9 *
10 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef PALUDIS_GUARD_PALUDIS_STREAM_HOLDER_HH
21#define PALUDIS_GUARD_PALUDIS_STREAM_HOLDER_HH 1
22
24#include <utility>
25#include <ostream>
26#include <ios>
27
28namespace paludis
29{
30 /**
31 * Holder for a stream so the destructor can throw.
32 */
33 template <typename Stream_>
34 class PALUDIS_VISIBLE StreamHolder
35 {
36 private:
37 Stream_ s;
38 typedef std::basic_ostream<typename Stream_::char_type, typename Stream_::traits_type> ostream_type;
39 typedef std::basic_ios<typename Stream_::char_type, typename Stream_::traits_type> ios_type;
40
41 public:
42 ///\name Basic operations
43 ///\{
44
45 template<typename... Args_>
46 StreamHolder(Args_ && ... args) : s(std::forward<Args_>(args)...)
47 {
48 }
49
50 ///\}
51
52 ///\name Formatted output
53 ///\{
54
55 template <typename T_>
56 StreamHolder<Stream_>& operator<<(T_ && rhs)
57 {
58 s << std::forward<T_>(rhs);
59 return *this;
60 }
61
62 /**
63 * Overload for stream manipulators (std::endl, std::flush, etc.)
64 *
65 * see 27.7.3.6.3
66 */
67 ostream_type& operator<<(ostream_type& (*rhs)(ostream_type&))
68 {
69 return rhs(s);
70 }
71
72 /**
73 * Overload for stream manipulators (std::endl, std::flush, etc.)
74 *
75 * see 27.7.3.6.3
76 */
77 ostream_type& operator<<(ios_type& (*rhs)(ios_type&))
78 {
79 rhs(s);
80 return s;
81 }
82
83 /**
84 * Overload for stream manipulators (std::endl, std::flush, etc.)
85 *
86 * see 27.7.3.6.3
87 */
88 ostream_type& operator<<(std::ios_base& (*rhs)(std::ios_base&))
89 {
90 rhs(s);
91 return s;
92 }
93
94 ///\}
95
96 ///\name Conversions
97 ///\{
98
99 bool operator!() const
100 {
101 return !s;
102 }
103
104 operator bool() const
105 {
106 return s;
107 }
108
109 operator Stream_&()
110 {
111 return s;
112 }
113
114 operator Stream_*()
115 {
116 return &s;
117 }
118
119 ///\}
120 };
121}
122
123#endif
ostream_type & operator<<(ostream_type &(*rhs)(ostream_type &))
Definition stream_holder.hh:67
ostream_type & operator<<(ios_type &(*rhs)(ios_type &))
Definition stream_holder.hh:77
ostream_type & operator<<(std::ios_base &(*rhs)(std::ios_base &))
Definition stream_holder.hh:88
#define PALUDIS_VISIBLE
Definition attributes.hh:59
Definition args_dumper.hh:39
Definition about_metadata-fwd.hh:24