[Assorted-commits] SF.net SVN: assorted:[1232] cpp-commons/trunk
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-25 19:25:57
|
Revision: 1232 http://assorted.svn.sourceforge.net/assorted/?rev=1232&view=rev Author: yangzhang Date: 2009-02-25 19:25:50 +0000 (Wed, 25 Feb 2009) Log Message: ----------- - st_reader takes an externally managed buffer and allows for manual pointer adjustment via reset_range() - reset_range - added sized_array and associated swap() - updated setup.bash for the reorganized files Modified Paths: -------------- cpp-commons/trunk/setup.bash cpp-commons/trunk/src/commons/array.h cpp-commons/trunk/src/commons/st/st.h Modified: cpp-commons/trunk/setup.bash =================================================================== --- cpp-commons/trunk/setup.bash 2009-02-25 07:45:26 UTC (rev 1231) +++ cpp-commons/trunk/setup.bash 2009-02-25 19:25:50 UTC (rev 1232) @@ -3,4 +3,4 @@ pkg=cpp-commons . simple-setup.bash version=0.1 -install include/ src/commons +install include/ src/{boost,commons,yonat} Modified: cpp-commons/trunk/src/commons/array.h =================================================================== --- cpp-commons/trunk/src/commons/array.h 2009-02-25 07:45:26 UTC (rev 1231) +++ cpp-commons/trunk/src/commons/array.h 2009-02-25 19:25:50 UTC (rev 1232) @@ -12,13 +12,36 @@ using namespace boost; template<typename T> class array; + template<typename T> class sized_array; template<typename T> void swap(array<T> &a, array<T> &b); + template<typename T> void swap(sized_array<T> &a, sized_array<T> &b); + // TODO: rename /** * A thin wrapper around arrays. Like a fixed-size vector. Unlike array * since the size can be dynamically determined. */ template<typename T> + class sized_array { + friend void swap<>(sized_array<T> &a, sized_array<T> &b); + public: + explicit sized_array(char *p, size_t n) : p_(p), n_(n) {} + size_t size() const { return n_; } + T *get() const { return p_; } + T *end() const { return p_ + n_; } + T operator[](size_t i) const { return p_[i]; } + void reset(T *p, size_t n) { p_ = p; n_ = n; } + private: + T *p_; + size_t n_; + }; + + // TODO: rename + /** + * A thin wrapper around arrays. Like a fixed-size vector. Unlike array + * since the size can be dynamically determined. + */ + template<typename T> class array { EXPAND(unique_ptr<T[]>) friend void swap<>(array<T> &a, array<T> &b); @@ -26,9 +49,10 @@ explicit array(size_t n) : p_(checkpass(new T[n])), n_(n) {} size_t size() const { return n_; } T *get() const { return p_.get(); } - void release() { p_.release(); } + T *release() { return p_.release(); } T *end() const { return this->get() + n_; } T operator[](size_t i) const { return p_[i]; } + void reset(T *p) { p_.reset(p); } private: unique_ptr<T[]> p_; size_t n_; @@ -45,7 +69,19 @@ swap(a.n_, b.n_); } + // TODO: try just specifying a single templated function /** + * Swap two arrays. + */ + template<typename T> + void + swap(sized_array<T> &a, sized_array<T> &b) + { + swap(a.p_, b.p_); + swap(a.n_, b.n_); + } + + /** * Conditionally-scoped, move-able, release-able, un-sized array. */ template<typename T> @@ -63,6 +99,7 @@ operator T*() { return p_; } operator const T*() const { return p_; } bool scoped() const { return scoped_; } + bool &scoped() { return scoped_; } private: T *p_; bool scoped_; Modified: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h 2009-02-25 07:45:26 UTC (rev 1231) +++ cpp-commons/trunk/src/commons/st/st.h 2009-02-25 19:25:50 UTC (rev 1232) @@ -383,9 +383,9 @@ { NONCOPYABLE(st_reader) public: - st_reader(st_netfd_t fd, size_t bufsize = 1e7) : + st_reader(st_netfd_t fd, char *buf, size_t bufsize) : fd_(fd), - buf_(bufsize), + buf_(buf, bufsize), start_(buf_.get()), end_(buf_.get()) {} @@ -403,9 +403,17 @@ /** * The entire read buffer. */ - array<char> &buf() { return buf_; } + sized_array<char> &buf() { return buf_; } /** + * Manually update/adjust the pointers; useful after changing buf_. + */ + void reset_range(char *start, char *end) { + start_ = start; + end_ = end; + } + + /** * Discard the requested number of bytes. */ void skip(size_t req, st_utime_t to = ST_UTIME_NO_TIMEOUT) { @@ -519,7 +527,7 @@ /** * The temporary storage buffer. */ - array<char> buf_; + sized_array<char> buf_; /** * The start of the unconsumed range of bytes. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |