8 #ifndef BOOST_NOWIDE_FSTREAM_HPP_INCLUDED 9 #define BOOST_NOWIDE_FSTREAM_HPP_INCLUDED 12 #include <boost/nowide/detail/is_path.hpp> 13 #include <boost/nowide/filebuf.hpp> 25 static std::ios_base::openmode mode() {
return std::ios_base::in; }
26 static std::ios_base::openmode mode_modifier() {
return mode(); }
27 template<
typename CharType,
typename Traits>
29 using type = std::basic_istream<CharType, Traits>;
34 static std::ios_base::openmode mode() {
return std::ios_base::out; }
35 static std::ios_base::openmode mode_modifier() {
return mode(); }
36 template<
typename CharType,
typename Traits>
38 using type = std::basic_ostream<CharType, Traits>;
41 struct StreamTypeInOut
43 static std::ios_base::openmode mode() {
return std::ios_base::in | std::ios_base::out; }
44 static std::ios_base::openmode mode_modifier() {
return std::ios_base::openmode(); }
45 template<
typename CharType,
typename Traits>
47 using type = std::basic_iostream<CharType, Traits>;
59 template<
typename CharType,
61 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));
168 #pragma warning(push) 169 #pragma warning(disable : 4250) // <class> : inherits <method> via dominance 171 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
175 class basic_fstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>
177 using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>;
183 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
185 open(file_name, mode);
187 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 189 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
191 open(file_name, mode);
195 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
197 open(file_name, mode);
199 template<
typename Path>
201 detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::in
202 | std::ios_base::out)
204 open(file_name, mode);
207 using fstream_impl::open;
208 using fstream_impl::is_open;
209 using fstream_impl::close;
210 using fstream_impl::rdbuf;
211 using fstream_impl::swap;
218 fstream_impl::operator=(std::move(rhs));
222 template<
typename CharType,
typename Traits>
227 template<
typename CharType,
typename Traits>
228 void swap(basic_ifstream<CharType, Traits>& lhs, basic_ifstream<CharType, Traits>& rhs)
232 template<
typename CharType,
typename Traits>
233 void swap(basic_ofstream<CharType, Traits>& lhs, basic_ofstream<CharType, Traits>& rhs)
237 template<
typename CharType,
typename Traits>
238 void swap(basic_fstream<CharType, Traits>& lhs, basic_fstream<CharType, Traits>& rhs)
246 using filebuf = basic_filebuf<char>;
272 template<
typename CharType,
typename Traits,
typename T_StreamType,
int>
273 class fstream_impl :
private buf_holder<basic_filebuf<CharType, Traits>>,
274 public T_StreamType::template stream_base<CharType, Traits>::type
277 using base_buf_holder = buf_holder<internal_buffer_type>;
278 using stream_base =
typename T_StreamType::template stream_base<CharType, Traits>::type;
281 using stream_base::setstate;
282 using stream_base::clear;
285 using base_buf_holder::buf_;
287 fstream_impl() : stream_base(&buf_)
289 fstream_impl(
const fstream_impl&) =
delete;
290 fstream_impl& operator=(
const fstream_impl&) =
delete;
293 fstream_impl(fstream_impl&& other) noexcept :
294 base_buf_holder(std::move(other)), stream_base(std::move(other))
296 this->set_rdbuf(rdbuf());
298 fstream_impl& operator=(fstream_impl&& rhs) noexcept
300 base_buf_holder::operator=(std::move(rhs));
301 stream_base::operator=(std::move(rhs));
304 void swap(fstream_impl& other)
306 stream_base::swap(other);
307 rdbuf()->swap(*other.rdbuf());
310 void open(
const std::string& file_name, std::ios_base::openmode mode = T_StreamType::mode())
312 open(file_name.c_str(), mode);
314 template<
typename Path>
315 detail::enable_if_path_t<Path, void> open(
const Path& file_name,
316 std::ios_base::openmode mode = T_StreamType::mode())
318 open(file_name.c_str(), mode);
320 void open(
const char* file_name, std::ios_base::openmode mode = T_StreamType::mode())
322 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
323 setstate(std::ios_base::failbit);
327 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 328 void open(
const wchar_t* file_name, std::ios_base::openmode mode = T_StreamType::mode())
330 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
331 setstate(std::ios_base::failbit);
338 return rdbuf()->is_open();
342 return rdbuf()->is_open();
346 if(!rdbuf()->close())
347 setstate(std::ios_base::failbit);
350 internal_buffer_type* rdbuf()
const 352 return const_cast<internal_buffer_type*>(&buf_);
basic_filebuf< char > filebuf
Convenience typedef.
Definition: filebuf.hpp:472
Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:72
This forward declaration defines the basic_filebuf type.
Definition: filebuf.hpp:47
Same as std::basic_fstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:175
Same as std::basic_ofstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:123
#define BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT
Define to 1 to use internal class from filebuf.hpp.
Definition: config.hpp:77