7#ifndef BOOST_NOWIDE_FSTREAM_HPP_INCLUDED
8#define BOOST_NOWIDE_FSTREAM_HPP_INCLUDED
11#include <boost/nowide/detail/is_path.hpp>
12#include <boost/nowide/filebuf.hpp>
24 static std::ios_base::openmode mode() {
return std::ios_base::in; }
25 static std::ios_base::openmode mode_modifier() {
return mode(); }
26 template<
typename CharType,
typename Traits>
28 using type = std::basic_istream<CharType, Traits>;
33 static std::ios_base::openmode mode() {
return std::ios_base::out; }
34 static std::ios_base::openmode mode_modifier() {
return mode(); }
35 template<
typename CharType,
typename Traits>
37 using type = std::basic_ostream<CharType, Traits>;
40 struct StreamTypeInOut
42 static std::ios_base::openmode mode() {
return std::ios_base::in | std::ios_base::out; }
43 static std::ios_base::openmode mode_modifier() {
return std::ios_base::openmode(); }
44 template<
typename CharType,
typename Traits>
46 using type = std::basic_iostream<CharType, Traits>;
58 template<
typename CharType,
60 typename T_StreamType,
71 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
72 class basic_ifstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>
74 using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>;
80 explicit basic_ifstream(
const char* file_name, std::ios_base::openmode mode = std::ios_base::in)
82 open(file_name, mode);
84#if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
85 explicit basic_ifstream(
const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::in)
87 open(file_name, mode);
91 explicit basic_ifstream(
const std::string& file_name, std::ios_base::openmode mode = std::ios_base::in)
93 open(file_name, mode);
96 template<
typename Path>
98 detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::in)
100 open(file_name, mode);
102 using fstream_impl::open;
103 using fstream_impl::is_open;
104 using fstream_impl::close;
105 using fstream_impl::rdbuf;
106 using fstream_impl::swap;
113 fstream_impl::operator=(std::move(rhs));
122 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
123 class basic_ofstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeOut>
125 using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeOut>;
130 explicit basic_ofstream(
const char* file_name, std::ios_base::openmode mode = std::ios_base::out)
132 open(file_name, mode);
134#if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
135 explicit basic_ofstream(
const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::out)
137 open(file_name, mode);
140 explicit basic_ofstream(
const std::string& file_name, std::ios_base::openmode mode = std::ios_base::out)
142 open(file_name, mode);
144 template<
typename Path>
146 detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::out)
148 open(file_name, mode);
151 using fstream_impl::open;
152 using fstream_impl::is_open;
153 using fstream_impl::close;
154 using fstream_impl::rdbuf;
155 using fstream_impl::swap;
162 fstream_impl::operator=(std::move(rhs));
169#pragma warning(disable : 4250)
175 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
176 class basic_fstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>
178 using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>;
184 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
186 open(file_name, mode);
188#if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
190 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
192 open(file_name, mode);
196 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
198 open(file_name, mode);
200 template<
typename Path>
202 detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::in
203 | std::ios_base::out)
205 open(file_name, mode);
208 using fstream_impl::open;
209 using fstream_impl::is_open;
210 using fstream_impl::close;
211 using fstream_impl::rdbuf;
212 using fstream_impl::swap;
219 fstream_impl::operator=(std::move(rhs));
224 template<
typename CharType,
typename Traits>
229 template<
typename CharType,
typename Traits>
230 void swap(basic_ofstream<CharType, Traits>& lhs, basic_ofstream<CharType, Traits>& rhs)
234 template<
typename CharType,
typename Traits>
235 void swap(basic_fstream<CharType, Traits>& lhs, basic_fstream<CharType, Traits>& rhs)
243 using filebuf = basic_filebuf<char>;
269 template<
typename CharType,
typename Traits,
typename T_StreamType,
int>
270 class fstream_impl :
private buf_holder<basic_filebuf<CharType, Traits>>,
271 public T_StreamType::template stream_base<CharType, Traits>::type
274 using base_buf_holder = buf_holder<internal_buffer_type>;
275 using stream_base =
typename T_StreamType::template stream_base<CharType, Traits>::type;
278 using stream_base::setstate;
279 using stream_base::clear;
282 using base_buf_holder::buf_;
284 fstream_impl() : stream_base(&buf_)
286 fstream_impl(
const fstream_impl&) =
delete;
287 fstream_impl& operator=(
const fstream_impl&) =
delete;
290 fstream_impl(fstream_impl&& other) noexcept :
291 base_buf_holder(std::move(other)), stream_base(std::move(other))
293 this->set_rdbuf(rdbuf());
295 fstream_impl& operator=(fstream_impl&& rhs)
noexcept
297 base_buf_holder::operator=(std::move(rhs));
298 stream_base::operator=(std::move(rhs));
301 void swap(fstream_impl& other)
303 stream_base::swap(other);
304 rdbuf()->swap(*other.rdbuf());
307 void open(
const std::string& file_name, std::ios_base::openmode mode = T_StreamType::mode())
309 open(file_name.c_str(), mode);
311 template<
typename Path>
312 detail::enable_if_path_t<Path, void> open(
const Path& file_name,
313 std::ios_base::openmode mode = T_StreamType::mode())
315 open(file_name.c_str(), mode);
317 void open(
const char* file_name, std::ios_base::openmode mode = T_StreamType::mode())
319 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
320 setstate(std::ios_base::failbit);
324#if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
325 void open(
const wchar_t* file_name, std::ios_base::openmode mode = T_StreamType::mode())
327 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
328 setstate(std::ios_base::failbit);
335 return rdbuf()->is_open();
339 return rdbuf()->is_open();
343 if(!rdbuf()->close())
344 setstate(std::ios_base::failbit);
347 internal_buffer_type* rdbuf()
const
349 return const_cast<internal_buffer_type*
>(&buf_);
This forward declaration defines the basic_filebuf type which is used when BOOST_NOWIDE_USE_FILEBUF_R...
Definition: filebuf.hpp:48
Same as std::basic_fstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:177
Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:73
Same as std::basic_ofstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:124
#define BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT
Define to 1 to use the class from <filebuf.hpp> that is used on Windows.
Definition: config.hpp:76
basic_filebuf< char > filebuf
Convenience typedef.
Definition: filebuf.hpp:532
void swap(basic_filebuf< CharType, Traits > &lhs, basic_filebuf< CharType, Traits > &rhs)
Swap the basic_filebuf instances.
Definition: filebuf.hpp:536