8 #ifndef BOOST_NOWIDE_FSTREAM_HPP_INCLUDED 9 #define BOOST_NOWIDE_FSTREAM_HPP_INCLUDED 12 #include <boost/nowide/filebuf.hpp> 23 static std::ios_base::openmode mode() {
return std::ios_base::in; }
24 static std::ios_base::openmode mode_modifier() {
return mode(); }
25 template<
typename CharType,
typename Traits>
27 typedef std::basic_istream<CharType, Traits> type;
32 static std::ios_base::openmode mode() {
return std::ios_base::out; }
33 static std::ios_base::openmode mode_modifier() {
return mode(); }
34 template<
typename CharType,
typename Traits>
36 typedef std::basic_ostream<CharType, Traits> type;
39 struct StreamTypeInOut
41 static std::ios_base::openmode mode() {
return std::ios_base::in | std::ios_base::out; }
42 static std::ios_base::openmode mode_modifier() {
return std::ios_base::openmode(); }
43 template<
typename CharType,
typename Traits>
45 typedef std::basic_iostream<CharType, Traits> type;
57 template<
typename CharType,
59 typename T_StreamType,
63 template<
typename Path,
typename Result>
64 struct enable_if_path;
71 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
72 class basic_ifstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>
74 typedef detail::fstream_impl<CharType, Traits, detail::StreamTypeIn> fstream_impl;
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 const Path& file_name,
99 typename detail::enable_if_path<Path, std::ios_base::openmode>::type mode = std::ios_base::in)
101 open(file_name, mode);
103 using fstream_impl::open;
104 using fstream_impl::is_open;
105 using fstream_impl::close;
106 using fstream_impl::rdbuf;
107 using fstream_impl::swap;
114 fstream_impl::operator=(std::move(rhs));
123 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
124 class basic_ofstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeOut>
126 typedef detail::fstream_impl<CharType, Traits, detail::StreamTypeOut> fstream_impl;
131 explicit basic_ofstream(
const char* file_name, std::ios_base::openmode mode = std::ios_base::out)
133 open(file_name, mode);
135 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 136 explicit basic_ofstream(
const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::out)
138 open(file_name, mode);
141 explicit basic_ofstream(
const std::string& file_name, std::ios_base::openmode mode = std::ios_base::out)
143 open(file_name, mode);
145 template<
typename Path>
147 const Path& file_name,
148 typename detail::enable_if_path<Path, std::ios_base::openmode>::type mode = std::ios_base::out)
150 open(file_name, mode);
153 using fstream_impl::open;
154 using fstream_impl::is_open;
155 using fstream_impl::close;
156 using fstream_impl::rdbuf;
157 using fstream_impl::swap;
164 fstream_impl::operator=(std::move(rhs));
170 #pragma warning(push) 171 #pragma warning(disable : 4250) // <class> : inherits <method> via dominance 173 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
177 class basic_fstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>
179 typedef detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut> fstream_impl;
185 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
187 open(file_name, mode);
189 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 191 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
193 open(file_name, mode);
197 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
199 open(file_name, mode);
201 template<
typename Path>
203 typename detail::enable_if_path<Path, std::ios_base::openmode>::type mode =
204 std::ios_base::in | std::ios_base::out)
206 open(file_name, mode);
209 using fstream_impl::open;
210 using fstream_impl::is_open;
211 using fstream_impl::close;
212 using fstream_impl::rdbuf;
213 using fstream_impl::swap;
220 fstream_impl::operator=(std::move(rhs));
224 template<
typename CharType,
typename Traits>
229 template<
typename CharType,
typename Traits>
230 void swap(basic_ifstream<CharType, Traits>& lhs, basic_ifstream<CharType, Traits>& rhs)
234 template<
typename CharType,
typename Traits>
235 void swap(basic_ofstream<CharType, Traits>& lhs, basic_ofstream<CharType, Traits>& rhs)
239 template<
typename CharType,
typename Traits>
240 void swap(basic_fstream<CharType, Traits>& lhs, basic_fstream<CharType, Traits>& rhs)
248 typedef basic_filebuf<char>
filebuf;
274 template<
typename CharType,
typename Traits,
typename T_StreamType,
int>
275 class fstream_impl :
private buf_holder<basic_filebuf<CharType, Traits> >,
276 public T_StreamType::template stream_base<CharType, Traits>::type
279 typedef buf_holder<internal_buffer_type> base_buf_holder;
280 typedef typename T_StreamType::template stream_base<CharType, Traits>::type stream_base;
283 using stream_base::setstate;
284 using stream_base::clear;
287 using base_buf_holder::buf_;
289 fstream_impl() : stream_base(&buf_)
291 fstream_impl(
const fstream_impl&) =
delete;
292 fstream_impl& operator=(
const fstream_impl&) =
delete;
295 fstream_impl(fstream_impl&& other) noexcept : base_buf_holder(std::move(other)),
296 stream_base(std::move(other))
298 this->set_rdbuf(rdbuf());
300 fstream_impl& operator=(fstream_impl&& rhs) noexcept
302 base_buf_holder::operator=(std::move(rhs));
303 stream_base::operator=(std::move(rhs));
306 void swap(fstream_impl& other)
308 stream_base::swap(other);
309 rdbuf()->swap(*other.rdbuf());
312 void open(
const std::string& file_name, std::ios_base::openmode mode = T_StreamType::mode())
314 open(file_name.c_str(), mode);
316 template<
typename Path>
317 typename detail::enable_if_path<Path, void>::type open(
const Path& file_name,
318 std::ios_base::openmode mode = T_StreamType::mode())
320 open(file_name.c_str(), mode);
322 void open(
const char* file_name, std::ios_base::openmode mode = T_StreamType::mode())
324 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
325 setstate(std::ios_base::failbit);
329 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 330 void open(
const wchar_t* file_name, std::ios_base::openmode mode = T_StreamType::mode())
332 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
333 setstate(std::ios_base::failbit);
340 return rdbuf()->is_open();
344 return rdbuf()->is_open();
348 if(!rdbuf()->close())
349 setstate(std::ios_base::failbit);
352 internal_buffer_type* rdbuf()
const 354 return const_cast<internal_buffer_type*>(&buf_);
371 template<
typename U, U& (U::*)(), U (U::*)() const>
374 static one test(Check<U, &U::make_preferred, &U::filename>*);
376 static two test(...);
380 value =
sizeof(test<T>(0)) ==
sizeof(one)
383 template<
bool B,
typename T>
387 struct enable_if<true, T>
392 template<
typename Path,
typename Result>
393 struct enable_if_path : enable_if<is_path<Path>::value, Result>
basic_ifstream< char > ifstream
Definition: fstream.hpp:253
basic_filebuf< char > filebuf
Convenience typedef.
Definition: filebuf.hpp:464
Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:72
basic_ofstream< char > ofstream
Definition: fstream.hpp:258
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:177
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 internal class from filebuf.hpp.
Definition: config.hpp:84
basic_fstream< char > fstream
Definition: fstream.hpp:263