paludis Version 3.0.2
The one true package mangler
safe_ofstream.hh
Go to the documentation of this file.
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
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_UTIL_SAFE_OFSTREAM_HH
21#define PALUDIS_GUARD_PALUDIS_UTIL_SAFE_OFSTREAM_HH 1
22
25#include <paludis/util/fs_path-fwd.hh>
26#include <paludis/util/pimp.hh>
27#include <paludis/util/stream_holder.hh>
28#include <ostream>
29
30/** \file
31 * Declarations for SafeOFStream.
32 *
33 * \ingroup g_fs
34 *
35 * \section Examples
36 *
37 * - None at this time.
38 */
39
40namespace paludis
41{
42 /**
43 * Output stream buffer class that can be opened via an FD, and that doesn't
44 * do retarded things when given a non-file.
45 *
46 * See \ref TCppSL Ch. 13.13 for what we're doing here. The buffer code is
47 * based upon the "io/outbuf2.hpp" example in section 13.13.3.
48 *
49 * \ingroup g_fs
50 * \since 0.34.3
51 */
52 class PALUDIS_VISIBLE SafeOFStreamBuf :
53 public std::streambuf
54 {
55 private:
57
58 protected:
59 int_type
60 overflow(int_type c) override;
61
62 std::streamsize
63 xsputn(const char * s, std::streamsize num) override;
64
65 public:
66 ///\name Basic operations
67 ///\{
68
69 SafeOFStreamBuf(const int f, const bool buffer);
70 ~SafeOFStreamBuf() override;
71
72 ///\}
73
74 void write_buffered();
75
76 /// Our file descriptor.
77 int fd;
78 };
79
80 /**
81 * Member from base initialisation for SafeOFStream.
82 *
83 * \ingroup g_fs
84 * \since 0.34.3
85 */
86 class PALUDIS_VISIBLE SafeOFStreamBase
87 {
88 protected:
89 /// Our buffer.
91
92 public:
93 ///\name Basic operations
94 ///\{
95
96 SafeOFStreamBase(const int fd, const bool buffer);
97
98 ///\}
99 };
100
101 /**
102 * Output stream buffer class that can be opened via an FD, and that doesn't
103 * do retarded things when given a non-file.
104 *
105 * \ingroup g_fs
106 * \since 0.34.3
107 */
108 class PALUDIS_VISIBLE SafeOFStream :
109 protected SafeOFStreamBase,
110 public StreamHolder<std::ostream>
111 {
112 private:
113 const bool _close;
114
115 public:
116 ///\name Basic operations
117 ///\{
118
119 SafeOFStream(const int fd, const bool buffer);
120 SafeOFStream(const FSPath &, const int open_flags, const bool buffer);
121 ~SafeOFStream() noexcept(false);
122
123 ///\}
124 };
125
126 /**
127 * Thrown by SafeOFStream if an error occurs.
128 *
129 * \ingroup g_fs
130 * \since 0.34.3
131 */
132 class PALUDIS_VISIBLE SafeOFStreamError :
133 public Exception
134 {
135 public:
136 SafeOFStreamError(const std::string &) noexcept;
137 };
138
139 extern template class Pimp<SafeOFStreamBuf>;
140}
141
142#endif
Definition fs_path.hh:34
Definition pimp.hh:52
SafeOFStreamBuf buf
Our buffer.
Definition safe_ofstream.hh:90
Definition safe_ofstream.hh:54
int fd
Our file descriptor.
Definition safe_ofstream.hh:77
#define PALUDIS_VISIBLE
Definition attributes.hh:59
Definition about_metadata-fwd.hh:24