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,
72 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
73 class basic_ifstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>
75 using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>;
81 explicit basic_ifstream(
const char* file_name, std::ios_base::openmode mode = std::ios_base::in)
83 open(file_name, mode);
85 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 86 explicit basic_ifstream(
const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::in)
88 open(file_name, mode);
92 explicit basic_ifstream(
const std::string& file_name, std::ios_base::openmode mode = std::ios_base::in)
94 open(file_name, mode);
97 template<
typename Path>
99 detail::enable_if_path_t<Path, std::ios_base::openmode> 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 using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeOut>;
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 detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::out)
149 open(file_name, mode);
152 using fstream_impl::open;
153 using fstream_impl::is_open;
154 using fstream_impl::close;
155 using fstream_impl::rdbuf;
156 using fstream_impl::swap;
163 fstream_impl::operator=(std::move(rhs));
169 #pragma warning(push) 170 #pragma warning(disable : 4250) // <class> : inherits <method> via dominance 172 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
177 class basic_fstream :
public detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>
179 using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>;
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 detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::in
204 | 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));
225 template<
typename CharType,
typename Traits>
230 template<
typename CharType,
typename Traits>
231 void swap(basic_ofstream<CharType, Traits>& lhs, basic_ofstream<CharType, Traits>& rhs)
235 template<
typename CharType,
typename Traits>
236 void swap(basic_fstream<CharType, Traits>& lhs, basic_fstream<CharType, Traits>& rhs)
244 using filebuf = basic_filebuf<char>;
270 template<
typename CharType,
typename Traits,
typename T_StreamType,
int>
271 class fstream_impl :
private buf_holder<basic_filebuf<CharType, Traits>>,
272 public T_StreamType::template stream_base<CharType, Traits>::type
275 using base_buf_holder = buf_holder<internal_buffer_type>;
276 using stream_base =
typename T_StreamType::template stream_base<CharType, Traits>::type;
279 using stream_base::setstate;
280 using stream_base::clear;
283 using base_buf_holder::buf_;
285 fstream_impl() : stream_base(&buf_)
287 fstream_impl(
const fstream_impl&) =
delete;
288 fstream_impl& operator=(
const fstream_impl&) =
delete;
291 fstream_impl(fstream_impl&& other) noexcept :
292 base_buf_holder(std::move(other)), stream_base(std::move(other))
294 this->set_rdbuf(rdbuf());
296 fstream_impl& operator=(fstream_impl&& rhs) noexcept
298 base_buf_holder::operator=(std::move(rhs));
299 stream_base::operator=(std::move(rhs));
302 void swap(fstream_impl& other)
304 stream_base::swap(other);
305 rdbuf()->swap(*other.rdbuf());
308 void open(
const std::string& file_name, std::ios_base::openmode mode = T_StreamType::mode())
310 open(file_name.c_str(), mode);
312 template<
typename Path>
313 detail::enable_if_path_t<Path, void> open(
const Path& file_name,
314 std::ios_base::openmode mode = T_StreamType::mode())
316 open(file_name.c_str(), mode);
318 void open(
const char* file_name, std::ios_base::openmode mode = T_StreamType::mode())
320 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
321 setstate(std::ios_base::failbit);
325 #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS 326 void open(
const wchar_t* file_name, std::ios_base::openmode mode = T_StreamType::mode())
328 if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
329 setstate(std::ios_base::failbit);
336 return rdbuf()->is_open();
340 return rdbuf()->is_open();
344 if(!rdbuf()->close())
345 setstate(std::ios_base::failbit);
348 internal_buffer_type* rdbuf()
const 350 return const_cast<internal_buffer_type*>(&buf_);
basic_filebuf< char > filebuf
Convenience typedef.
Definition: filebuf.hpp:533
Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:73
This forward declaration defines the basic_filebuf type which is used when BOOST_NOWIDE_USE_FILEBUF_R...
Definition: filebuf.hpp:49
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 the class from <filebuf.hpp> that is used on Windows.
Definition: config.hpp:77
void swap(basic_filebuf< CharType, Traits > &lhs, basic_filebuf< CharType, Traits > &rhs)
Swap the basic_filebuf instances.
Definition: filebuf.hpp:537