fstream.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //
  2. // Copyright (c) 2012 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_NOWIDE_FSTREAM_HPP_INCLUDED
  7. #define BOOST_NOWIDE_FSTREAM_HPP_INCLUDED
  8. #include <boost/nowide/config.hpp>
  9. #include <boost/nowide/detail/is_path.hpp>
  10. #include <boost/nowide/filebuf.hpp>
  11. #include <istream>
  12. #include <ostream>
  13. #include <utility>
  14. namespace boost {
  15. namespace nowide {
  16. /// \cond INTERNAL
  17. namespace detail {
  18. // clang-format off
  19. struct StreamTypeIn
  20. {
  21. static std::ios_base::openmode mode() { return std::ios_base::in; }
  22. static std::ios_base::openmode mode_modifier() { return mode(); }
  23. template<typename CharType, typename Traits>
  24. struct stream_base{
  25. using type = std::basic_istream<CharType, Traits>;
  26. };
  27. };
  28. struct StreamTypeOut
  29. {
  30. static std::ios_base::openmode mode() { return std::ios_base::out; }
  31. static std::ios_base::openmode mode_modifier() { return mode(); }
  32. template<typename CharType, typename Traits>
  33. struct stream_base{
  34. using type = std::basic_ostream<CharType, Traits>;
  35. };
  36. };
  37. struct StreamTypeInOut
  38. {
  39. static std::ios_base::openmode mode() { return std::ios_base::in | std::ios_base::out; }
  40. static std::ios_base::openmode mode_modifier() { return std::ios_base::openmode(); }
  41. template<typename CharType, typename Traits>
  42. struct stream_base{
  43. using type = std::basic_iostream<CharType, Traits>;
  44. };
  45. };
  46. // clang-format on
  47. /// Base class for all basic_*fstream classes
  48. /// Contains basic_filebuf instance so its pointer can be used to construct basic_*stream
  49. /// Provides common functions to reduce boilerplate code including inheriting from
  50. /// the correct std::basic_[io]stream class and initializing it
  51. /// \tparam T_StreamType One of StreamType* above.
  52. /// Class used instead of value, because openmode::operator| may not be constexpr
  53. /// \tparam FileBufType Discriminator to force a differing ABI if depending on the contained filebuf
  54. template<typename CharType,
  55. typename Traits,
  56. typename T_StreamType,
  57. int FileBufType = BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT>
  58. class fstream_impl;
  59. } // namespace detail
  60. /// \endcond
  61. ///
  62. /// \brief Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows
  63. ///
  64. /// Affected by #BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT and #BOOST_NOWIDE_USE_WCHAR_OVERLOADS
  65. template<typename CharType, typename Traits = std::char_traits<CharType>>
  66. class basic_ifstream : public detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>
  67. {
  68. using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeIn>;
  69. public:
  70. basic_ifstream()
  71. {}
  72. explicit basic_ifstream(const char* file_name, std::ios_base::openmode mode = std::ios_base::in)
  73. {
  74. open(file_name, mode);
  75. }
  76. #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
  77. explicit basic_ifstream(const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::in)
  78. {
  79. open(file_name, mode);
  80. }
  81. #endif
  82. explicit basic_ifstream(const std::string& file_name, std::ios_base::openmode mode = std::ios_base::in)
  83. {
  84. open(file_name, mode);
  85. }
  86. template<typename Path>
  87. explicit basic_ifstream(const Path& file_name,
  88. detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::in)
  89. {
  90. open(file_name, mode);
  91. }
  92. using fstream_impl::open;
  93. using fstream_impl::is_open;
  94. using fstream_impl::close;
  95. using fstream_impl::rdbuf;
  96. using fstream_impl::swap;
  97. basic_ifstream(const basic_ifstream&) = delete;
  98. basic_ifstream& operator=(const basic_ifstream&) = delete;
  99. basic_ifstream(basic_ifstream&& other) noexcept : fstream_impl(std::move(other))
  100. {}
  101. basic_ifstream& operator=(basic_ifstream&& rhs) noexcept
  102. {
  103. fstream_impl::operator=(std::move(rhs));
  104. return *this;
  105. }
  106. };
  107. ///
  108. /// \brief Same as std::basic_ofstream<char> but accepts UTF-8 strings under Windows
  109. ///
  110. /// Affected by #BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT and #BOOST_NOWIDE_USE_WCHAR_OVERLOADS
  111. template<typename CharType, typename Traits = std::char_traits<CharType>>
  112. class basic_ofstream : public detail::fstream_impl<CharType, Traits, detail::StreamTypeOut>
  113. {
  114. using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeOut>;
  115. public:
  116. basic_ofstream()
  117. {}
  118. explicit basic_ofstream(const char* file_name, std::ios_base::openmode mode = std::ios_base::out)
  119. {
  120. open(file_name, mode);
  121. }
  122. #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
  123. explicit basic_ofstream(const wchar_t* file_name, std::ios_base::openmode mode = std::ios_base::out)
  124. {
  125. open(file_name, mode);
  126. }
  127. #endif
  128. explicit basic_ofstream(const std::string& file_name, std::ios_base::openmode mode = std::ios_base::out)
  129. {
  130. open(file_name, mode);
  131. }
  132. template<typename Path>
  133. explicit basic_ofstream(const Path& file_name,
  134. detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::out)
  135. {
  136. open(file_name, mode);
  137. }
  138. using fstream_impl::open;
  139. using fstream_impl::is_open;
  140. using fstream_impl::close;
  141. using fstream_impl::rdbuf;
  142. using fstream_impl::swap;
  143. basic_ofstream(const basic_ofstream&) = delete;
  144. basic_ofstream& operator=(const basic_ofstream&) = delete;
  145. basic_ofstream(basic_ofstream&& other) noexcept : fstream_impl(std::move(other))
  146. {}
  147. basic_ofstream& operator=(basic_ofstream&& rhs)
  148. {
  149. fstream_impl::operator=(std::move(rhs));
  150. return *this;
  151. }
  152. };
  153. #ifdef BOOST_MSVC
  154. #pragma warning(push)
  155. #pragma warning(disable : 4250) // <class> : inherits <method> via dominance
  156. #endif
  157. ///
  158. /// \brief Same as std::basic_fstream<char> but accepts UTF-8 strings under Windows
  159. ///
  160. /// Affected by #BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT and #BOOST_NOWIDE_USE_WCHAR_OVERLOADS
  161. template<typename CharType, typename Traits = std::char_traits<CharType>>
  162. class basic_fstream : public detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>
  163. {
  164. using fstream_impl = detail::fstream_impl<CharType, Traits, detail::StreamTypeInOut>;
  165. public:
  166. basic_fstream()
  167. {}
  168. explicit basic_fstream(const char* file_name,
  169. std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
  170. {
  171. open(file_name, mode);
  172. }
  173. #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
  174. explicit basic_fstream(const wchar_t* file_name,
  175. std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
  176. {
  177. open(file_name, mode);
  178. }
  179. #endif
  180. explicit basic_fstream(const std::string& file_name,
  181. std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
  182. {
  183. open(file_name, mode);
  184. }
  185. template<typename Path>
  186. explicit basic_fstream(const Path& file_name,
  187. detail::enable_if_path_t<Path, std::ios_base::openmode> mode = std::ios_base::in
  188. | std::ios_base::out)
  189. {
  190. open(file_name, mode);
  191. }
  192. using fstream_impl::open;
  193. using fstream_impl::is_open;
  194. using fstream_impl::close;
  195. using fstream_impl::rdbuf;
  196. using fstream_impl::swap;
  197. basic_fstream(const basic_fstream&) = delete;
  198. basic_fstream& operator=(const basic_fstream&) = delete;
  199. basic_fstream(basic_fstream&& other) noexcept : fstream_impl(std::move(other))
  200. {}
  201. basic_fstream& operator=(basic_fstream&& rhs)
  202. {
  203. fstream_impl::operator=(std::move(rhs));
  204. return *this;
  205. }
  206. };
  207. template<typename CharType, typename Traits>
  208. void swap(basic_ifstream<CharType, Traits>& lhs, basic_ifstream<CharType, Traits>& rhs)
  209. {
  210. lhs.swap(rhs);
  211. }
  212. template<typename CharType, typename Traits>
  213. void swap(basic_ofstream<CharType, Traits>& lhs, basic_ofstream<CharType, Traits>& rhs)
  214. {
  215. lhs.swap(rhs);
  216. }
  217. template<typename CharType, typename Traits>
  218. void swap(basic_fstream<CharType, Traits>& lhs, basic_fstream<CharType, Traits>& rhs)
  219. {
  220. lhs.swap(rhs);
  221. }
  222. ///
  223. /// Same as std::filebuf but accepts UTF-8 strings under Windows
  224. ///
  225. using filebuf = basic_filebuf<char>;
  226. ///
  227. /// Same as std::ifstream but accepts UTF-8 strings under Windows
  228. /// and *\::filesystem::path on all systems
  229. ///
  230. using ifstream = basic_ifstream<char>;
  231. ///
  232. /// Same as std::ofstream but accepts UTF-8 strings under Windows
  233. /// and *\::filesystem::path on all systems
  234. ///
  235. using ofstream = basic_ofstream<char>;
  236. ///
  237. /// Same as std::fstream but accepts UTF-8 strings under Windows
  238. /// and *\::filesystem::path on all systems
  239. ///
  240. using fstream = basic_fstream<char>;
  241. // Implementation
  242. namespace detail {
  243. /// Holds an instance of T
  244. /// Required to make sure this is constructed first before passing it to sibling classes
  245. template<typename T>
  246. struct buf_holder
  247. {
  248. T buf_;
  249. };
  250. template<typename CharType, typename Traits, typename T_StreamType, int>
  251. class fstream_impl : private buf_holder<basic_filebuf<CharType, Traits>>, // must be first due to init order
  252. public T_StreamType::template stream_base<CharType, Traits>::type
  253. {
  254. using internal_buffer_type = basic_filebuf<CharType, Traits>;
  255. using base_buf_holder = buf_holder<internal_buffer_type>;
  256. using stream_base = typename T_StreamType::template stream_base<CharType, Traits>::type;
  257. public:
  258. using stream_base::setstate;
  259. using stream_base::clear;
  260. protected:
  261. using base_buf_holder::buf_;
  262. fstream_impl() : stream_base(&buf_)
  263. {}
  264. fstream_impl(const fstream_impl&) = delete;
  265. fstream_impl& operator=(const fstream_impl&) = delete;
  266. // coverity[exn_spec_violation]
  267. fstream_impl(fstream_impl&& other) noexcept :
  268. base_buf_holder(std::move(other)), stream_base(std::move(other))
  269. {
  270. this->set_rdbuf(rdbuf());
  271. }
  272. fstream_impl& operator=(fstream_impl&& rhs) noexcept
  273. {
  274. base_buf_holder::operator=(std::move(rhs));
  275. stream_base::operator=(std::move(rhs));
  276. return *this;
  277. }
  278. void swap(fstream_impl& other)
  279. {
  280. stream_base::swap(other);
  281. rdbuf()->swap(*other.rdbuf());
  282. }
  283. void open(const std::string& file_name, std::ios_base::openmode mode = T_StreamType::mode())
  284. {
  285. open(file_name.c_str(), mode);
  286. }
  287. template<typename Path>
  288. detail::enable_if_path_t<Path, void> open(const Path& file_name,
  289. std::ios_base::openmode mode = T_StreamType::mode())
  290. {
  291. open(file_name.c_str(), mode);
  292. }
  293. void open(const char* file_name, std::ios_base::openmode mode = T_StreamType::mode())
  294. {
  295. if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
  296. setstate(std::ios_base::failbit);
  297. else
  298. clear();
  299. }
  300. #if BOOST_NOWIDE_USE_WCHAR_OVERLOADS
  301. void open(const wchar_t* file_name, std::ios_base::openmode mode = T_StreamType::mode())
  302. {
  303. if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier()))
  304. setstate(std::ios_base::failbit);
  305. else
  306. clear();
  307. }
  308. #endif
  309. bool is_open()
  310. {
  311. return rdbuf()->is_open();
  312. }
  313. bool is_open() const
  314. {
  315. return rdbuf()->is_open();
  316. }
  317. void close()
  318. {
  319. if(!rdbuf()->close())
  320. setstate(std::ios_base::failbit);
  321. }
  322. internal_buffer_type* rdbuf() const
  323. {
  324. return const_cast<internal_buffer_type*>(&buf_);
  325. }
  326. };
  327. #ifdef BOOST_MSVC
  328. #pragma warning(pop)
  329. #endif
  330. } // namespace detail
  331. } // namespace nowide
  332. } // namespace boost
  333. #endif