#pragma once #include "basic_text_istream.h" namespace utilib { class basic_text_ostream : public noncopyable { public: template class array { public: array(const T * p, const size_t size) :p_(p), size_(size) { } ~array() { } private: const T * p_; const size_t size_; friend class basic_text_ostream; }; // class array basic_text_ostream(basic_stream_buffer & buf) : s_(buf) { } ~basic_text_ostream() { } basic_text_ostream & operator<< (const bool b) { char c = b ? '1' : '0'; return this->operator <<(c); } basic_text_ostream & operator<< (const char c) { s_.copy(&c, sizeof(char)); return *this; } basic_text_ostream & operator<< (const unsigned char c) { s_.copy(&c, sizeof(unsigned char)); return *this; } basic_text_ostream & operator<< (const short n) { transfer("%d", (int)n); return *this; } basic_text_ostream & operator<< (const unsigned short n) { transfer("%u", (unsigned)n); return *this; } basic_text_ostream & operator<< (const int n) { transfer("%d", n); return *this; } basic_text_ostream & operator<< (const unsigned int n) { transfer("%u", n); return *this; } basic_text_ostream & operator<< (const long long l); basic_text_ostream & operator>> (const unsigned long long l); basic_text_ostream & operator<< (const float val) { transfer("%f", val); return *this; } basic_text_ostream & operator<< (const double val); basic_text_ostream & operator<< (const long double val); // no definition basic_text_ostream & operator<< (const char * lpszStr); basic_text_ostream & operator<< (const wchar_t * lpszStr); template basic_text_ostream & operator<< (const T (&v)[N]); template basic_text_ostream & operator<< (const T *); template basic_text_ostream & operator<< (const array arr) { this->operator <<(arr.size_); for(size_t i = 0; i < arr.size_; ++i) { *this << arr.p_[i]; } return *this; } template basic_text_ostream & operator<< (const std::basic_string & str) { const size_t len = str.size(); this->operator <<(len); if (sizeof(CharType) == sizeof(char)) s_.copy(str.c_str(), len); else for(size_t i = 0; i < len; ++i) *this << str[i]; return *this; } template basic_text_ostream & operator<< (const std::vector & vec) { typedef const std::vector vector_type; const size_t len = vec.size(); this->operator <<(len); for (typename vector_type::const_iterator it = vec.begin(); it != vec.end(); ++it) { *this << *it; } return *this; } template basic_text_ostream & operator<< (const std::list & lst) { typedef const std::list list_type; const size_t len = lst.size(); this->operator <<(len); for (typename list_type::const_iterator it = lst.begin(); it != lst.end(); ++it) { *this << *it; } return *this; } template basic_text_ostream & operator<< (std::queue qu) { #pragma message("Performance Warning! copy queue.") typedef std::queue queue_type; const size_t len = qu.size(); this->operator <<(len); while (!qu.empty()) { *this << qu.front(); qu.pop(); } return *this; } template basic_text_ostream & operator<< (const std::deque & dq) { typedef std::deque deque_type; const size_t len = dq.size(); this->operator <<(len); for(typename deque_type::const_iterator it = dq.begin(); it != dq.end(); ++it) { *this << *it; } return *this; } template basic_text_ostream & operator<< (const std::set & st) { typedef std::set set_type; const size_t len = st.size(); this->operator <<(len); for(typename set_type::const_iterator it = st.begin(); it != st.end(); ++it) { *this << *it; } return *this; } template basic_text_ostream & operator<< (const std::pair & par) { typedef std::pair pair_type; *this << par.first; *this << par.second; return *this; } template basic_text_ostream & operator<< (const std::map & mp) { typedef std::map map_type; const size_t len = mp.size(); this->operator <<(len); for(typename map_type::const_iterator it = mp.begin(); it != mp.end(); ++it) { *this << it->first; *this << it->second; } return *this; } basic_stream_buffer & get_buffer()const { return s_; } private: template void transfer(const char * fmt, const T val) { char buf[256]; int n = ::sprintf(buf, fmt, val); if (n == -1) throw std::runtime_error("error when calling sprintf"); this->operator <<((char)n); s_.copy(buf, n); } basic_stream_buffer & s_; }; // class basic_text_ostream } //