lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
|
00001 /*****************************************************/ 00002 /* lean Strings (c) Tobias Zirr 2011 */ 00003 /*****************************************************/ 00004 00005 #ifndef LEAN_STRINGS_CHARSTREAM 00006 #define LEAN_STRINGS_CHARSTREAM 00007 00008 #include "../lean.h" 00009 #include <iostream> 00010 00011 namespace lean 00012 { 00013 namespace strings 00014 { 00015 00017 template < class Elem, class Traits = std::char_traits<Elem> > 00018 class basic_charbuf : public std::basic_streambuf<Elem, Traits> 00019 { 00020 protected: 00021 virtual void __CLR_OR_THIS_CALL _Lock() { } 00022 virtual void __CLR_OR_THIS_CALL _Unlock() { } 00023 00024 public: 00026 basic_charbuf(char_type *begin, char_type *end) 00027 { 00028 setp(begin, end); 00029 setg(begin, begin, end); 00030 } 00031 00033 void reset() 00034 { 00035 setp(pbase(), epptr()); 00036 setg(eback(), eback(), egptr()); 00037 } 00038 00040 char_type* begin() const { return pbase(); } 00042 char_type* end() const { return epptr(); } 00044 char_type* write_end() const { return pptr(); } 00046 char_type* read_end() const { return gptr(); } 00047 00048 }; 00049 00050 namespace impl 00051 { 00053 template < class Elem, class Traits = std::char_traits<Elem> > 00054 class charbuf_holder 00055 { 00056 protected: 00058 typedef basic_charbuf<Elem, Traits> stream_buffer; 00060 stream_buffer m_buffer; 00061 00063 charbuf_holder(typename stream_buffer::char_type *begin, typename stream_buffer::char_type *end) 00064 : m_buffer(begin, end) { } 00065 }; 00066 } 00067 00069 template < class Elem, class Traits = std::char_traits<Elem> > 00070 class basic_charstream : private impl::charbuf_holder<Elem, Traits>, public std::basic_iostream<Elem, Traits> 00071 { 00072 private: 00073 typedef impl::charbuf_holder<char_type, traits_type> holder_base_type; 00074 typedef std::basic_iostream<char_type, traits_type> stream_base_type; 00075 00076 public: 00078 typedef typename holder_base_type::stream_buffer stream_buffer; 00079 00081 basic_charstream(char_type *begin, char_type *end) 00082 : holder_base_type(begin, end), 00083 stream_base_type(&m_buffer) { } 00085 basic_charstream(char_type *begin) 00086 : holder_base_type(begin, begin + std::numeric_limits<int>::max()), // required to be int 00087 stream_base_type(&m_buffer) { } 00088 00090 basic_charstream& reset() 00091 { 00092 m_buffer.reset(); 00093 return *this; 00094 } 00095 00097 stream_buffer* rdbuf() const { return static_cast<stream_buffer*>(stream_base_type::rdbuf()); } 00098 00100 char_type* begin() const { return m_buffer.begin(); } 00102 char_type* end() const { return m_buffer.end(); } 00104 char_type* write_end() const { return m_buffer.write_end(); } 00106 char_type* read_end() const { return m_buffer.read_end(); } 00107 00108 }; 00109 00111 typedef basic_charstream<char> charstream; 00113 typedef basic_charstream<wchar_t> wcharstream; 00114 00115 } // namespace 00116 00117 using strings::basic_charbuf; 00118 00119 using strings::basic_charstream; 00120 using strings::charstream; 00121 using strings::wcharstream; 00122 00123 } // namespace 00124 00125 #endif