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;
56 template<
typename CharType,
typename Traits,
typename T_StreamType>
59 template<
typename Path,
typename Result>
60 struct enable_if_path;
67 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
68 class basic_ifstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>
70 typedef detail::fstream_impl<CharType, Traits, detail::StreamTypeIn> fstream_impl;
76 explicit basic_ifstream(
const char* file_name, std::ios_base::openmode mode = std::ios_base::in)
78 open(file_name, mode);
80 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 81 explicit basic_ifstream(
const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::in)
83 open(file_name, mode);
87 explicit basic_ifstream(
const std::string& file_name, std::ios_base::openmode mode = std::ios_base::in)
89 open(file_name, mode);
92 template<
typename Path>
94 const Path& file_name,
95 typename detail::enable_if_path<Path, std::ios_base::openmode>::type mode = std::ios_base::in)
97 open(file_name, mode);
99 using fstream_impl::open;
100 using fstream_impl::is_open;
101 using fstream_impl::close;
102 using fstream_impl::rdbuf;
103 #if BOOST_NOWIDE_CXX11 104 using fstream_impl::swap;
111 fstream_impl::operator=(std::move(rhs));
121 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
122 class basic_ofstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeOut>
124 typedef detail::fstream_impl<CharType, Traits, detail::StreamTypeOut> fstream_impl;
129 explicit basic_ofstream(
const char* file_name, std::ios_base::openmode mode = std::ios_base::out)
131 open(file_name, mode);
133 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 134 explicit basic_ofstream(
const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::out)
136 open(file_name, mode);
139 explicit basic_ofstream(
const std::string& file_name, std::ios_base::openmode mode = std::ios_base::out)
141 open(file_name, mode);
143 template<
typename Path>
145 const Path& file_name,
146 typename detail::enable_if_path<Path, std::ios_base::openmode>::type 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 #if BOOST_NOWIDE_CXX11 156 using fstream_impl::swap;
163 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 #if BOOST_NOWIDE_CXX11 214 using fstream_impl::swap;
221 fstream_impl::operator=(std::move(rhs));
226 #if BOOST_NOWIDE_CXX11 227 template<
typename CharType,
typename Traits>
232 template<
typename CharType,
typename Traits>
233 void swap(basic_ifstream<CharType, Traits>& lhs, basic_ifstream<CharType, Traits>& rhs)
237 template<
typename CharType,
typename Traits>
238 void swap(basic_ofstream<CharType, Traits>& lhs, basic_ofstream<CharType, Traits>& rhs)
242 template<
typename CharType,
typename Traits>
243 void swap(basic_fstream<CharType, Traits>& lhs, basic_fstream<CharType, Traits>& rhs)
252 typedef basic_filebuf<char>
filebuf;
278 template<
typename CharType,
typename Traits,
typename T_StreamType>
279 class fstream_impl :
private buf_holder<basic_filebuf<CharType, Traits> >,
280 public T_StreamType::template stream_base<CharType, Traits>::type
283 typedef buf_holder<internal_buffer_type> base_buf_holder;
284 typedef typename T_StreamType::template stream_base<CharType, Traits>::type stream_base;
287 using stream_base::setstate;
288 using stream_base::clear;
291 using base_buf_holder::buf_;
293 fstream_impl() : stream_base(&buf_)
296 #if BOOST_NOWIDE_CXX11 297 fstream_impl(
const fstream_impl& other) =
delete;
298 fstream_impl& operator=(
const fstream_impl& other) =
delete;
301 fstream_impl(fstream_impl&& other) noexcept : base_buf_holder(std::move(other)),
302 stream_base(std::move(other))
304 this->set_rdbuf(rdbuf());
306 fstream_impl& operator=(fstream_impl&& rhs) noexcept
308 base_buf_holder::operator=(std::move(rhs));
309 stream_base::operator=(std::move(rhs));
312 void swap(fstream_impl& other)
314 stream_base::swap(other);
315 rdbuf()->swap(*other.rdbuf());
319 void open(
const std::string& file_name, std::ios_base::openmode mode = T_StreamType::mode())
321 open(file_name.c_str(), mode);
323 template<
typename Path>
324 typename detail::enable_if_path<Path, void>::type open(
const Path& file_name,
325 std::ios_base::openmode mode = T_StreamType::mode())
327 open(file_name.c_str(), mode);
329 void open(
const char* file_name, std::ios_base::openmode mode = T_StreamType::mode())
331 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
332 setstate(std::ios_base::failbit);
336 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 337 void open(
const wchar_t* file_name, std::ios_base::openmode mode = T_StreamType::mode())
339 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
340 setstate(std::ios_base::failbit);
347 return rdbuf()->is_open();
351 return rdbuf()->is_open();
355 if(!rdbuf()->close())
356 setstate(std::ios_base::failbit);
359 internal_buffer_type* rdbuf()
const 361 return const_cast<internal_buffer_type*>(&buf_);
378 template<
typename U, U& (U::*)(), U (U::*)() const>
381 static one test(Check<U, &U::make_preferred, &U::filename>*);
383 static two test(...);
387 value =
sizeof(test<T>(0)) ==
sizeof(one)
390 template<
bool B,
typename T>
394 struct enable_if<true, T>
399 template<
typename Path,
typename Result>
400 struct enable_if_path : enable_if<is_path<Path>::value, Result>
basic_ifstream< char > ifstream
Definition: fstream.hpp:257
basic_filebuf< char > filebuf
Convenience typedef.
Definition: filebuf.hpp:458
Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:68
basic_ofstream< char > ofstream
Definition: fstream.hpp:262
This forward declaration defines the basic_filebuf type.
Definition: filebuf.hpp:40
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:122
basic_fstream< char > fstream
Definition: fstream.hpp:267