output.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #pragma once
  2. #include "fwd.h"
  3. #include "labeled.h"
  4. #include <util/generic/noncopyable.h>
  5. #include <util/generic/string.h>
  6. #include <util/generic/strbuf.h>
  7. #include <util/generic/typetraits.h>
  8. #include <util/system/compiler.h>
  9. #include <type_traits>
  10. /**
  11. * @addtogroup Streams_Base
  12. * @{
  13. */
  14. /**
  15. * Abstract output stream.
  16. */
  17. class IOutputStream: public TNonCopyable {
  18. public:
  19. /**
  20. * Data block for output.
  21. */
  22. struct TPart {
  23. inline TPart(const void* Buf, size_t Len) noexcept
  24. : buf(Buf)
  25. , len(Len)
  26. {
  27. }
  28. inline TPart(const TStringBuf s) noexcept
  29. : buf(s.data())
  30. , len(s.size())
  31. {
  32. }
  33. inline TPart() noexcept
  34. : buf(nullptr)
  35. , len(0)
  36. {
  37. }
  38. inline ~TPart() = default;
  39. static inline TPart CrLf() noexcept {
  40. return TPart("\r\n", 2);
  41. }
  42. const void* buf;
  43. size_t len;
  44. };
  45. IOutputStream() noexcept;
  46. virtual ~IOutputStream();
  47. IOutputStream(IOutputStream&&) noexcept {
  48. }
  49. IOutputStream& operator=(IOutputStream&&) noexcept {
  50. return *this;
  51. }
  52. /**
  53. * Writes into this stream.
  54. *
  55. * @param buf Data to write.
  56. * @param len Number of bytes to write.
  57. */
  58. inline void Write(const void* buf, size_t len) {
  59. if (len) {
  60. DoWrite(buf, len);
  61. }
  62. }
  63. /**
  64. * Writes a string into this stream.
  65. *
  66. * @param st String to write.
  67. */
  68. inline void Write(const TStringBuf st) {
  69. Write(st.data(), st.size());
  70. }
  71. /**
  72. * Writes several data blocks into this stream.
  73. *
  74. * @param parts Pointer to the start of the data blocks
  75. * array.
  76. * @param count Number of data blocks to write.
  77. */
  78. inline void Write(const TPart* parts, size_t count) {
  79. if (count > 1) {
  80. DoWriteV(parts, count);
  81. } else if (count) {
  82. DoWrite(parts->buf, parts->len);
  83. }
  84. }
  85. /**
  86. * Writes a single character into this stream.
  87. *
  88. * @param ch Character to write.
  89. */
  90. inline void Write(char ch) {
  91. DoWriteC(ch);
  92. }
  93. /**
  94. * Flushes this stream's buffer, if any.
  95. *
  96. * Note that this can also be done with a `Flush` manipulator:
  97. * @code
  98. * stream << "some string" << Flush;
  99. * @endcode
  100. */
  101. inline void Flush() {
  102. DoFlush();
  103. }
  104. /**
  105. * Flushes and closes this stream. No more data can be written into a stream
  106. * once it's closed.
  107. */
  108. inline void Finish() {
  109. DoFinish();
  110. }
  111. protected:
  112. /**
  113. * Writes into this stream.
  114. *
  115. * @param buf Data to write.
  116. * @param len Number of bytes to write.
  117. * @throws yexception If IO error occurs.
  118. */
  119. virtual void DoWrite(const void* buf, size_t len) = 0;
  120. /**
  121. * Writes several data blocks into this stream.
  122. *
  123. * @param parts Pointer to the start of the data blocks
  124. * array.
  125. * @param count Number of data blocks to write.
  126. * @throws yexception If IO error occurs.
  127. */
  128. virtual void DoWriteV(const TPart* parts, size_t count);
  129. /**
  130. * Writes a single character into this stream. Can be overridden with a faster implementation.
  131. *
  132. * @param ch Character to write.
  133. */
  134. virtual void DoWriteC(char ch);
  135. /**
  136. * Flushes this stream's buffer, if any.
  137. *
  138. * @throws yexception If IO error occurs.
  139. */
  140. virtual void DoFlush();
  141. /**
  142. * Flushes and closes this stream. No more data can be written into a stream
  143. * once it's closed.
  144. *
  145. * @throws yexception If IO error occurs.
  146. */
  147. virtual void DoFinish();
  148. };
  149. /**
  150. * `operator<<` for `IOutputStream` by default delegates to this function.
  151. *
  152. * Note that while `operator<<` uses overloading (and thus argument-dependent
  153. * lookup), `Out` uses template specializations. This makes it possible to
  154. * have a single `Out` declaration, and then just provide specializations in
  155. * cpp files, letting the linker figure everything else out. This approach
  156. * reduces compilation times.
  157. *
  158. * However, if the flexibility of overload resolution is needed, then one should
  159. * just overload `operator<<`.
  160. *
  161. * @param out Output stream to write into.
  162. * @param value Value to write.
  163. */
  164. template <class T>
  165. void Out(IOutputStream& out, typename TTypeTraits<T>::TFuncParam value);
  166. #define Y_DECLARE_OUT_SPEC(MODIF, T, stream, value) \
  167. template <> \
  168. MODIF void Out<T>(IOutputStream & stream, TTypeTraits<T>::TFuncParam value)
  169. template <>
  170. inline void Out<const char*>(IOutputStream& o, const char* t) {
  171. if (t) {
  172. o.Write(t);
  173. } else {
  174. o.Write("(null)");
  175. }
  176. }
  177. template <>
  178. void Out<const wchar16*>(IOutputStream& o, const wchar16* w);
  179. template <>
  180. void Out<const wchar32*>(IOutputStream& o, const wchar32* w);
  181. static inline IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, TStreamManipulator m) {
  182. m(o);
  183. return o;
  184. }
  185. static inline IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, const char* t) {
  186. Out<const char*>(o, t);
  187. return o;
  188. }
  189. static inline IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, char* t) {
  190. Out<const char*>(o, t);
  191. return o;
  192. }
  193. template <class T>
  194. static inline std::enable_if_t<std::is_scalar<T>::value, IOutputStream&> operator<<(IOutputStream& o Y_LIFETIME_BOUND, T t) {
  195. Out<T>(o, t);
  196. return o;
  197. }
  198. template <class T>
  199. static inline std::enable_if_t<!std::is_scalar<T>::value, IOutputStream&> operator<<(IOutputStream& o Y_LIFETIME_BOUND, const T& t) {
  200. Out<T>(o, t);
  201. return o;
  202. }
  203. static inline IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, const wchar16* t) {
  204. Out<const wchar16*>(o, t);
  205. return o;
  206. }
  207. static inline IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, wchar16* t) {
  208. Out<const wchar16*>(o, t);
  209. return o;
  210. }
  211. static inline IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, const wchar32* t) {
  212. Out<const wchar32*>(o, t);
  213. return o;
  214. }
  215. static inline IOutputStream& operator<<(IOutputStream& o Y_LIFETIME_BOUND, wchar32* t) {
  216. Out<const wchar32*>(o, t);
  217. return o;
  218. }
  219. namespace NPrivate {
  220. IOutputStream& StdOutStream() noexcept;
  221. IOutputStream& StdErrStream() noexcept;
  222. } // namespace NPrivate
  223. /**
  224. * Standard output stream.
  225. */
  226. #define Cout (::NPrivate::StdOutStream())
  227. /**
  228. * Standard error stream.
  229. */
  230. #define Cerr (::NPrivate::StdErrStream())
  231. /**
  232. * Standard log stream.
  233. */
  234. #define Clog Cerr
  235. /**
  236. * End-of-line output manipulator, basically the same as `std::endl`.
  237. */
  238. static inline void Endl(IOutputStream& o) {
  239. (o << '\n').Flush();
  240. }
  241. /**
  242. * Flushing stream manipulator, basically the same as `std::flush`.
  243. */
  244. static inline void Flush(IOutputStream& o) {
  245. o.Flush();
  246. }
  247. /*
  248. * Also see format.h for additional manipulators.
  249. */
  250. #include "debug.h"
  251. void RedirectStdioToAndroidLog(bool redirect);
  252. /** @} */