[Assorted-commits] SF.net SVN: assorted:[1251] ydb/trunk/src/ser.h
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-03-04 23:32:07
|
Revision: 1251 http://assorted.svn.sourceforge.net/assorted/?rev=1251&view=rev Author: yangzhang Date: 2009-03-04 23:31:58 +0000 (Wed, 04 Mar 2009) Log Message: ----------- moved writer out of ser.h and into commons Modified Paths: -------------- ydb/trunk/src/ser.h Modified: ydb/trunk/src/ser.h =================================================================== --- ydb/trunk/src/ser.h 2009-03-04 23:31:02 UTC (rev 1250) +++ ydb/trunk/src/ser.h 2009-03-04 23:31:58 UTC (rev 1251) @@ -4,6 +4,7 @@ #include <commons/array.h> #include <commons/exceptions.h> #include <commons/st/st.h> +#include <commons/streamwriter.h> #include <commons/utility.h> #include <iomanip> #include <iostream> @@ -64,79 +65,7 @@ // TODO try to make all of the following conform to the std interfaces, if // amenable -class writer -{ - NONCOPYABLE(writer) - private: - sized_array<char> a_; - char *unsent_; - char *mark_; - char *p_; - boost::function<void(void*, size_t)> flushcb; - char *reserve(int n, char *p) { - if (p + n > a_.end()) { - // check that the reserved space will fit - assert(size_t(p - mark_ + n + sizeof(uint32_t)) <= a_.size()); - // get rid of what we have - flush(); - size_t diff = mark_ - (a_.get() + sizeof(uint32_t)); - memmove(a_.get() + sizeof(uint32_t), mark_, p_ - mark_); - mark_ = (unsent_ = a_.get()) + sizeof(uint32_t); - p_ -= diff; - p -= diff; - } - return p; - } - char *prefix() { return mark_ - sizeof(uint32_t); } - template<typename T> - void write_(T x, char *p) { - *reinterpret_cast<T*>(reserve(sizeof x, p)) = x; - } - public: - writer(boost::function<void(void*, size_t)> flushcb, char *a, size_t buf_size) : - a_(a, buf_size), unsent_(a_.get()), mark_(unsent_ + sizeof(uint32_t)), - p_(mark_), flushcb(flushcb) {} - sized_array<char> &buf() { return a_; } - char *cur() { return p_; } - size_t pos() { return p_ - mark_; } - size_t size() { return a_.size(); } - void mark() { - if (p_ > mark_) { - // prefix last segment with its length - *reinterpret_cast<uint32_t*>(prefix()) = uint32_t(p_ - mark_); - // start new segment - mark_ = (p_ += sizeof(uint32_t)); - } - } - void reset() { p_ = mark_; } - void reserve(int n) { reserve(n, p_); } - void mark_and_flush() { - mark(); - flush(); - mark_ = p_ = (unsent_ = a_.get()) + sizeof(uint32_t); - } - void flush() { - if (prefix() > unsent_) { - flushcb(unsent_, prefix() - unsent_); - unsent_ = prefix(); - } - } - void show() { - cout << static_cast<void*>(p_); - for (size_t i = 0; i < a_.size(); ++i) - cout << " " << hex << setfill('0') << setw(2) - << int(static_cast<unsigned char>(a_.get()[i])); - cout << endl; - cout << static_cast<void*>(p_); - for (size_t i = 0; i < a_.size(); ++i) - cout << " " << setfill(' ') << setw(2) << (i == pos() ? "^^" : ""); - cout << endl; - } - template<typename T> void skip() { reserve(sizeof(T)); p_ += sizeof(T); } - template<typename T> void write(T x) { write_(x, p_); p_ += sizeof x; } - template<typename T> void write(T x, size_t off) { write_(x, mark_ + off); } -}; - +typedef stream_writer writer; typedef st_reader reader; class stream This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |