[Assorted-commits] SF.net SVN: assorted:[1186] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-17 04:33:57
|
Revision: 1186 http://assorted.svn.sourceforge.net/assorted/?rev=1186&view=rev Author: yangzhang Date: 2009-02-17 04:33:52 +0000 (Tue, 17 Feb 2009) Log Message: ----------- - renamed array_view to managed_array - added read<T>() to st_reader Modified Paths: -------------- cpp-commons/trunk/src/commons/array.h cpp-commons/trunk/src/commons/st/st.h Modified: cpp-commons/trunk/src/commons/array.h =================================================================== --- cpp-commons/trunk/src/commons/array.h 2009-02-16 21:05:26 UTC (rev 1185) +++ cpp-commons/trunk/src/commons/array.h 2009-02-17 04:33:52 UTC (rev 1186) @@ -24,7 +24,7 @@ }; /** - * A release-able array. + * A scoped, release-able array. */ template<typename T> class auto_array { @@ -41,18 +41,16 @@ }; /** - * Move-able, conditionally-scoped array. - * - * TODO: rename to managed_array + * Conditionally-scoped, move-able, release-able, un-sized array. */ template<typename T> - class array_view { + class managed_array { public: - array_view(T *p, bool scoped) : p_(p), scoped_(scoped) {} + managed_array(T *p, bool scoped) : p_(p), scoped_(scoped) {} #ifdef __GXX_EXPERIMENTAL_CXX0X__ - array_view(array_view<T> &&a) : p_(a.p_), scoped_(a.scoped_) { a.release(); } + managed_array(managed_array<T> &&a) : p_(a.p_), scoped_(a.scoped_) { a.release(); } #endif - ~array_view() { if (scoped_) delete [] p_; } + ~managed_array() { if (scoped_) delete [] p_; } T *release() { T *p = p_; p_ = nullptr; scoped_ = false; return p; } T *get() { return p_; } const T *get() const { return p_; } Modified: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h 2009-02-16 21:05:26 UTC (rev 1185) +++ cpp-commons/trunk/src/commons/st/st.h 2009-02-17 04:33:52 UTC (rev 1186) @@ -420,17 +420,17 @@ * Returns a char array that contains the requested number of bytes. If * we hit an error or EOF, then an exception is thrown. */ - array_view<char> read(size_t req = 0, st_utime_t to = ST_UTIME_NO_TIMEOUT) { + managed_array<char> read(size_t req = 0, st_utime_t to = ST_UTIME_NO_TIMEOUT) { // Do we already have the requested data? if (amt() >= req) { - array_view<char> p(start_, false); + managed_array<char> p(start_, false); start_ += req; return p; } // Handle large arrays specially. if (req > buf_.size()) { - array_view<char> p(checkpass(new char[req]), true); + managed_array<char> p(checkpass(new char[req]), true); copy(start_, end_, p.get()); checkeqnneg(st_read_fully(fd_, p + amt(), req, to), static_cast<ssize_t>(req)); start_ = end_ = buf_.get(); @@ -457,11 +457,50 @@ if (amt() < req) throw eof_exception(); - array_view<char> p(start_, false); + managed_array<char> p(start_, false); start_ += req; return p; } + template<typename T> + T read(st_utime_t to = ST_UTIME_NO_TIMEOUT) + { + size_t req = sizeof(T); + + // Do we already have the requested data? + if (amt() >= req) { + T x = *reinterpret_cast<const T*>(start_); + start_ += req; + return x; + } + + assert(req <= buf_.size()); + + // Shift things down if necessary. + if (req > static_cast<size_t>(buf_.end() - end_)) { + copy(start_, end_, buf_.get()); + size_t diff = start_ - buf_.get(); + start_ -= diff; + end_ -= diff; + } + + // Keep reading until we have enough. + while (amt() < req) { + ssize_t res = st_read(fd_, end_, rem(), to); + checknneg(res); + if (res == 0) break; + else end_ += res; + } + + // If we got a premature EOF. + if (amt() < req) + throw eof_exception(); + + T x = *reinterpret_cast<const T*>(start_); + start_ += req; + return x; + } + private: st_reader(const st_reader &); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |