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));
223 template<
typename CharType,
typename Traits>
228 template<
typename CharType,
typename Traits>
229 void swap(basic_ofstream<CharType, Traits>& lhs, basic_ofstream<CharType, Traits>& rhs)
233 template<
typename CharType,
typename Traits>
234 void swap(basic_fstream<CharType, Traits>& lhs, basic_fstream<CharType, Traits>& rhs)
242 using filebuf = basic_filebuf<char>;
268 template<
typename CharType,
typename Traits,
typename T_StreamType,
int>
269 class fstream_impl :
private buf_holder<basic_filebuf<CharType, Traits>>,
270 public T_StreamType::template stream_base<CharType, Traits>::type
273 using base_buf_holder = buf_holder<internal_buffer_type>;
274 using stream_base =
typename T_StreamType::template stream_base<CharType, Traits>::type;
277 using stream_base::setstate;
278 using stream_base::clear;
281 using base_buf_holder::buf_;
283 fstream_impl() : stream_base(&buf_)
285 fstream_impl(
const fstream_impl&) =
delete;
286 fstream_impl& operator=(
const fstream_impl&) =
delete;
289 fstream_impl(fstream_impl&& other) noexcept :
290 base_buf_holder(std::move(other)), stream_base(std::move(other))
292 this->set_rdbuf(rdbuf());
294 fstream_impl& operator=(fstream_impl&& rhs) noexcept
296 base_buf_holder::operator=(std::move(rhs));
297 stream_base::operator=(std::move(rhs));
300 void swap(fstream_impl& other)
302 stream_base::swap(other);
303 rdbuf()->swap(*other.rdbuf());
306 void open(
const std::string& file_name, std::ios_base::openmode mode = T_StreamType::mode())
308 open(file_name.c_str(), mode);
310 template<
typename Path>
311 detail::enable_if_path_t<Path, void> open(
const Path& file_name,
312 std::ios_base::openmode mode = T_StreamType::mode())
314 open(file_name.c_str(), mode);
316 void open(
const char* file_name, std::ios_base::openmode mode = T_StreamType::mode())
318 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
319 setstate(std::ios_base::failbit);
323 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 324 void open(
const wchar_t* file_name, std::ios_base::openmode mode = T_StreamType::mode())
326 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
327 setstate(std::ios_base::failbit);
334 return rdbuf()->is_open();
338 return rdbuf()->is_open();
342 if(!rdbuf()->close())
343 setstate(std::ios_base::failbit);
346 internal_buffer_type* rdbuf()
const 348 return const_cast<internal_buffer_type*>(&buf_);
basic_filebuf< char > filebuf
Convenience typedef.
Definition: filebuf.hpp:476
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
void swap(basic_filebuf< CharType, Traits > &lhs, basic_filebuf< CharType, Traits > &rhs)
Swap the basic_filebuf instances.
Definition: filebuf.hpp:480