_ostream.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_INTERNAL_OSTREAM_H
  19. #define _STLP_INTERNAL_OSTREAM_H
  20. #ifndef _STLP_INTERNAL_IOS_H
  21. # include <stl/_ios.h> // For basic_ios<>. Includes <iosfwd>.
  22. #endif
  23. #ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
  24. # include <stl/_ostreambuf_iterator.h>
  25. #endif
  26. #if !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) && !defined (_STLP_INTERNAL_EXCEPTION)
  27. # include <stl/_exception.h>
  28. #endif
  29. _STLP_BEGIN_NAMESPACE
  30. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  31. template <class _CharT, class _Traits>
  32. class _Osentry;
  33. #endif
  34. _STLP_MOVE_TO_PRIV_NAMESPACE
  35. template <class _CharT, class _Traits>
  36. bool __init_bostr(basic_ostream<_CharT, _Traits>& __str);
  37. _STLP_MOVE_TO_STD_NAMESPACE
  38. //----------------------------------------------------------------------
  39. // class basic_ostream<>
  40. template <class _CharT, class _Traits>
  41. class basic_ostream : virtual public basic_ios<_CharT, _Traits> {
  42. typedef basic_ostream<_CharT, _Traits> _Self;
  43. #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
  44. //explicitely defined as private to avoid warnings:
  45. basic_ostream(_Self const&);
  46. _Self& operator = (_Self const&);
  47. #endif
  48. public: // Types
  49. typedef _CharT char_type;
  50. typedef typename _Traits::int_type int_type;
  51. typedef typename _Traits::pos_type pos_type;
  52. typedef typename _Traits::off_type off_type;
  53. typedef _Traits traits_type;
  54. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  55. public: // Constructor and destructor.
  56. explicit basic_ostream(basic_streambuf<_CharT, _Traits>* __buf);
  57. ~basic_ostream();
  58. public: // Hooks for manipulators.
  59. typedef basic_ios<_CharT, _Traits>& (_STLP_CALL *__ios_fn)(basic_ios<_CharT, _Traits>&);
  60. typedef ios_base& (_STLP_CALL *__ios_base_fn)(ios_base&);
  61. typedef _Self& (_STLP_CALL *__ostream_fn)(_Self&);
  62. _Self& operator<< (__ostream_fn __f) { return __f(*this); }
  63. _Self & operator<< (__ios_base_fn __f) { __f(*this); return *this; }
  64. _Self& operator<< (__ios_fn __ff) { __ff(*this); return *this; }
  65. private:
  66. bool _M_copy_buffered(basic_streambuf<_CharT, _Traits>* __from,
  67. basic_streambuf<_CharT, _Traits>* __to);
  68. bool _M_copy_unbuffered(basic_streambuf<_CharT, _Traits>* __from,
  69. basic_streambuf<_CharT, _Traits>* __to);
  70. public:
  71. void _M_put_char(_CharT __c);
  72. void _M_put_nowiden(const _CharT* __s);
  73. void _M_put_widen(const char* __s);
  74. bool _M_put_widen_aux(const char* __s, streamsize __n);
  75. public: // Unformatted output.
  76. _Self& put(char_type __c);
  77. _Self& write(const char_type* __s, streamsize __n);
  78. public: // Formatted output.
  79. // Formatted output from a streambuf.
  80. _Self& operator<<(basic_streambuf<_CharT, _Traits>* __buf);
  81. # ifndef _STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER
  82. // this is needed for compiling with option char = unsigned
  83. _Self& operator<<(unsigned char __x) { _M_put_char(__x); return *this; }
  84. # endif
  85. _Self& operator<<(short __x);
  86. _Self& operator<<(unsigned short __x);
  87. _Self& operator<<(int __x);
  88. #if defined (_WIN64) || !defined (_STLP_MSVC) || (_STLP_MSVC < 1300)
  89. _Self& operator<<(unsigned int __x);
  90. #else
  91. /* We define this operator with size_t rather than unsigned int to avoid
  92. * 64 bits warning.
  93. */
  94. _Self& operator<<(size_t __x);
  95. #endif
  96. _Self& operator<<(long __x);
  97. _Self& operator<<(unsigned long __x);
  98. #ifdef _STLP_LONG_LONG
  99. _Self& operator<< (_STLP_LONG_LONG __x);
  100. _Self& operator<< (unsigned _STLP_LONG_LONG __x);
  101. #endif
  102. _Self& operator<<(float __x);
  103. _Self& operator<<(double __x);
  104. # ifndef _STLP_NO_LONG_DOUBLE
  105. _Self& operator<<(long double __x);
  106. # endif
  107. _Self& operator<<(const void* __x);
  108. # ifndef _STLP_NO_BOOL
  109. _Self& operator<<(bool __x);
  110. # endif
  111. public: // Buffer positioning and manipulation.
  112. _Self& flush() {
  113. if (this->rdbuf())
  114. if (this->rdbuf()->pubsync() == -1)
  115. this->setstate(ios_base::badbit);
  116. return *this;
  117. }
  118. pos_type tellp() {
  119. return this->rdbuf() && !this->fail()
  120. ? this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out)
  121. : pos_type(-1);
  122. }
  123. _Self& seekp(pos_type __pos) {
  124. if (this->rdbuf() && !this->fail()) {
  125. if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1)) {
  126. this->setstate(ios_base::failbit);
  127. }
  128. }
  129. return *this;
  130. }
  131. _Self& seekp(off_type __off, ios_base::seekdir __dir) {
  132. if (this->rdbuf() && !this->fail())
  133. this->rdbuf()->pubseekoff(__off, __dir, ios_base::out);
  134. return *this;
  135. }
  136. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  137. // If we are using DLL specs, we have not to use inner classes
  138. // end class declaration here
  139. typedef _Osentry<_CharT, _Traits> sentry;
  140. };
  141. # define sentry _Osentry
  142. template <class _CharT, class _Traits>
  143. class _Osentry {
  144. typedef _Osentry<_CharT, _Traits> _Self;
  145. #else
  146. class sentry {
  147. typedef sentry _Self;
  148. #endif
  149. private:
  150. basic_ostream<_CharT, _Traits>& _M_str;
  151. // basic_streambuf<_CharT, _Traits>* _M_buf;
  152. bool _M_ok;
  153. public:
  154. explicit sentry(basic_ostream<_CharT, _Traits>& __str)
  155. : _M_str(__str), /* _M_buf(__str.rdbuf()), */ _M_ok(_STLP_PRIV __init_bostr(__str))
  156. {}
  157. ~sentry() {
  158. if (_M_str.flags() & ios_base::unitbuf)
  159. #if !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT)
  160. if (!uncaught_exception())
  161. #endif
  162. _M_str.flush();
  163. }
  164. operator bool() const { return _M_ok; }
  165. private: // Disable assignment and copy constructor.
  166. //Implementation is here only to avoid warning with some compilers.
  167. sentry(const _Self& __s) : _M_str(__s._M_str) {}
  168. _Self& operator=(const _Self&) { return *this; }
  169. };
  170. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  171. # undef sentry
  172. #else
  173. // close basic_ostream class definition here
  174. };
  175. #endif
  176. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  177. _STLP_EXPORT_TEMPLATE_CLASS basic_ostream<char, char_traits<char> >;
  178. _STLP_EXPORT_TEMPLATE_CLASS _Osentry<char, char_traits<char> >;
  179. # if !defined (_STLP_NO_WCHAR_T)
  180. _STLP_EXPORT_TEMPLATE_CLASS basic_ostream<wchar_t, char_traits<wchar_t> >;
  181. _STLP_EXPORT_TEMPLATE_CLASS _Osentry<wchar_t, char_traits<wchar_t> >;
  182. # endif
  183. #endif /* _STLP_USE_TEMPLATE_EXPORT */
  184. _STLP_MOVE_TO_PRIV_NAMESPACE
  185. // Helper functions for istream<>::sentry constructor.
  186. template <class _CharT, class _Traits>
  187. bool __init_bostr(basic_ostream<_CharT, _Traits>& __str) {
  188. if (__str.good()) {
  189. // boris : check if this is needed !
  190. if (!__str.rdbuf())
  191. __str.setstate(ios_base::badbit);
  192. if (__str.tie())
  193. __str.tie()->flush();
  194. return __str.good();
  195. }
  196. else
  197. return false;
  198. }
  199. template <class _CharT, class _Traits>
  200. inline basic_streambuf<_CharT, _Traits>* _STLP_CALL
  201. __get_ostreambuf(basic_ostream<_CharT, _Traits>& __St)
  202. { return __St.rdbuf(); }
  203. _STLP_MOVE_TO_STD_NAMESPACE
  204. // Non-member functions.
  205. template <class _CharT, class _Traits>
  206. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  207. operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c){
  208. __os._M_put_char(__c);
  209. return __os;
  210. }
  211. template <class _CharT, class _Traits>
  212. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  213. operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __s) {
  214. __os._M_put_nowiden(__s);
  215. return __os;
  216. }
  217. #if defined (_STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER)
  218. // some specializations
  219. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  220. operator<<(basic_ostream<char, char_traits<char> >& __os, char __c) {
  221. __os._M_put_char(__c);
  222. return __os;
  223. }
  224. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  225. operator<<(basic_ostream<char, char_traits<char> >& __os, signed char __c) {
  226. __os._M_put_char(__c);
  227. return __os;
  228. }
  229. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  230. operator<<(basic_ostream<char, char_traits<char> >& __os, unsigned char __c) {
  231. __os._M_put_char(__c);
  232. return __os;
  233. }
  234. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  235. operator<<(basic_ostream<char, char_traits<char> >& __os, const char* __s) {
  236. __os._M_put_nowiden(__s);
  237. return __os;
  238. }
  239. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  240. operator<<(basic_ostream<char, char_traits<char> >& __os, const signed char* __s) {
  241. __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  242. return __os;
  243. }
  244. inline basic_ostream<char, char_traits<char> >&
  245. operator<<(basic_ostream<char, char_traits<char> >& __os, const unsigned char* __s) {
  246. __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  247. return __os;
  248. }
  249. #else
  250. // also for compilers who might use that
  251. template <class _CharT, class _Traits>
  252. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  253. operator<<(basic_ostream<_CharT, _Traits>& __os, char __c) {
  254. __os._M_put_char(__os.widen(__c));
  255. return __os;
  256. }
  257. template <class _Traits>
  258. inline basic_ostream<char, _Traits>& _STLP_CALL
  259. operator<<(basic_ostream<char, _Traits>& __os, char __c) {
  260. __os._M_put_char(__c);
  261. return __os;
  262. }
  263. template <class _Traits>
  264. inline basic_ostream<char, _Traits>& _STLP_CALL
  265. operator<<(basic_ostream<char, _Traits>& __os, signed char __c) {
  266. __os._M_put_char(__c);
  267. return __os;
  268. }
  269. template <class _Traits>
  270. inline basic_ostream<char, _Traits>& _STLP_CALL
  271. operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) {
  272. __os._M_put_char(__c);
  273. return __os;
  274. }
  275. template <class _CharT, class _Traits>
  276. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  277. operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __s) {
  278. __os._M_put_widen(__s);
  279. return __os;
  280. }
  281. template <class _Traits>
  282. inline basic_ostream<char, _Traits>& _STLP_CALL
  283. operator<<(basic_ostream<char, _Traits>& __os, const char* __s) {
  284. __os._M_put_nowiden(__s);
  285. return __os;
  286. }
  287. template <class _Traits>
  288. inline basic_ostream<char, _Traits>& _STLP_CALL
  289. operator<<(basic_ostream<char, _Traits>& __os, const signed char* __s) {
  290. __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  291. return __os;
  292. }
  293. template <class _Traits>
  294. inline basic_ostream<char, _Traits>&
  295. operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __s) {
  296. __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  297. return __os;
  298. }
  299. #endif /* _STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER */
  300. //----------------------------------------------------------------------
  301. // basic_ostream manipulators.
  302. template <class _CharT, class _Traits>
  303. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  304. endl(basic_ostream<_CharT, _Traits>& __os) {
  305. __os.put(__os.widen('\n'));
  306. __os.flush();
  307. return __os;
  308. }
  309. template <class _CharT, class _Traits>
  310. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  311. ends(basic_ostream<_CharT, _Traits>& __os) {
  312. __os.put(_STLP_DEFAULT_CONSTRUCTED(_CharT));
  313. return __os;
  314. }
  315. template <class _CharT, class _Traits>
  316. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  317. flush(basic_ostream<_CharT, _Traits>& __os) {
  318. __os.flush();
  319. return __os;
  320. }
  321. _STLP_END_NAMESPACE
  322. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  323. # include <stl/_ostream.c>
  324. #endif
  325. #endif /* _STLP_INTERNAL_OSTREAM_H */
  326. // Local Variables:
  327. // mode:C++
  328. // End: