ModErn Text Analysis
META Enumerates Textual Applications
moveable_stream.h
Go to the documentation of this file.
1 
10 #ifndef META_IO_MOVEABLE_STREAM_H_
11 #define META_IO_MOVEABLE_STREAM_H_
12 
13 #include <fstream>
14 
15 #include "meta/config.h"
16 #include "meta/util/shim.h"
17 
18 namespace meta
19 {
20 namespace io
21 {
22 
23 namespace detail
24 {
25 template <class T>
26 T& get_stream(std::unique_ptr<T>& stream)
27 {
28  return *stream;
29 }
30 
31 template <class T>
32 const T& get_stream(const std::unique_ptr<T>& stream)
33 {
34  return *stream;
35 }
36 
37 inline std::ifstream& get_stream(std::ifstream& stream)
38 {
39  return stream;
40 }
41 
42 inline const std::ifstream& get_stream(const std::ifstream& stream)
43 {
44  return stream;
45 }
46 
47 inline std::ofstream& get_stream(std::ofstream& stream)
48 {
49  return stream;
50 }
51 
52 inline const std::ofstream& get_stream(const std::ofstream& stream)
53 {
54  return stream;
55 }
56 
57 #if META_HAS_STREAM_MOVE
58 template <class T, class... Args>
59 T make_stream(Args&&... args)
60 {
61  return T{std::forward<Args>(args)...};
62 }
63 
64 #else
65 template <class T, class... Args>
66 std::unique_ptr<T> make_stream(Args&&... args)
67 {
68  return make_unique<T>(std::forward<Args>(args)...);
69 }
70 #endif
71 
72 template <class T>
74 
75 template <>
76 struct default_openmode<std::ifstream>
77 {
78  const static constexpr std::ios_base::openmode value = std::ios_base::in;
79 };
80 
81 template <>
82 struct default_openmode<std::ofstream>
83 {
84  const static constexpr std::ios_base::openmode value = std::ios_base::out;
85 };
86 }
87 
92 template <class Stream>
93 class mfstream
94 {
95  public:
96  mfstream() : stream_{detail::make_stream<Stream>()}
97  {
98  // nothing
99  }
100 
101  explicit mfstream(const char* filename,
102  std::ios_base::openmode mode
104  : stream_{detail::make_stream<Stream>(filename, mode)}
105  {
106  // nothing
107  }
108 
109  explicit mfstream(const std::string& filename,
110  std::ios_base::openmode mode
112  : mfstream{filename.c_str(), mode}
113  {
114  // nothing
115  }
116 
117  operator Stream&()
118  {
119  return detail::get_stream(stream_);
120  }
121 
122  operator const Stream&() const
123  {
124  return detail::get_stream(stream_);
125  }
126 
127  Stream& stream()
128  {
129  return detail::get_stream(stream_);
130  }
131 
132  const Stream& stream() const
133  {
134  return detail::get_stream(stream_);
135  }
136 
137  explicit operator bool() const
138  {
139  return static_cast<bool>(detail::get_stream(stream_));
140  }
141 
142  private:
143 #if META_HAS_STREAM_MOVE
144  Stream stream_;
145 #else
146  std::unique_ptr<Stream> stream_;
147 #endif
148 };
149 
152 }
153 }
154 #endif
STL namespace.
A stupid wrapper around a std::fstream to work around GCC&#39;s libstdc++ lacking move constructors for s...
Definition: moveable_stream.h:93
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retrieval, data mining, and other applications of text processing.
Definition: analyzer.h:25
Definition: moveable_stream.h:73