[Assorted-commits] SF.net SVN: assorted:[1303] cpp-commons/trunk
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-03-18 09:58:55
|
Revision: 1303 http://assorted.svn.sourceforge.net/assorted/?rev=1303&view=rev Author: yangzhang Date: 2009-03-18 09:58:41 +0000 (Wed, 18 Mar 2009) Log Message: ----------- - added anchored_stream_reader and associated functions - added default ctor for sized_array - added within - tweaks to stream_writer - simplified and error-check current_time_millis() - added unused warning for static functions die, _vcheck, _check, swap - added BEGIN_NAMESPACE, END_NAMESPACE - added readptr, ptr, skip to raw_reader, raw_writer - added todos to readme Modified Paths: -------------- cpp-commons/trunk/README cpp-commons/trunk/src/commons/algo.h cpp-commons/trunk/src/commons/array.h cpp-commons/trunk/src/commons/check.h cpp-commons/trunk/src/commons/die.h cpp-commons/trunk/src/commons/memory.h cpp-commons/trunk/src/commons/streamreader.h cpp-commons/trunk/src/commons/streamwriter.h cpp-commons/trunk/src/commons/time.h cpp-commons/trunk/src/commons/utility.h Modified: cpp-commons/trunk/README =================================================================== --- cpp-commons/trunk/README 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/README 2009-03-18 09:58:41 UTC (rev 1303) @@ -104,3 +104,8 @@ [GNU Common C++]: http://www.gnu.org/software/commoncpp/ [dlib]: http://dclib.sourceforge.net/ [FC++]: http://www.cc.gatech.edu/~yannis/fc++/ + +Todo +---- + +- Get to the bottom of statics, inlines, unused Modified: cpp-commons/trunk/src/commons/algo.h =================================================================== --- cpp-commons/trunk/src/commons/algo.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/algo.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -5,7 +5,8 @@ namespace commons { - void swap(size_t &x, size_t &y) { + __attribute__((unused)) inline void + swap(size_t &x, size_t &y) { x = x ^ y; y = x ^ y; x = x ^ y; Modified: cpp-commons/trunk/src/commons/array.h =================================================================== --- cpp-commons/trunk/src/commons/array.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/array.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -27,6 +27,7 @@ friend void swap<>(sized_array<T> &a, sized_array<T> &b); public: explicit sized_array(char *p, size_t n) : p_(p), n_(n) {} + sized_array() : p_(nullptr), n_(0) {} size_t size() const { return n_; } T *get() const { return p_; } T *begin() const { return p_; } @@ -125,6 +126,14 @@ bool scoped_; }; + /** + * Checks if a pointer is in an array. + */ + template<typename T> + inline bool within(T &a, const void *p) { + return a.begin() <= p && p < a.end(); + } + } #endif Modified: cpp-commons/trunk/src/commons/check.h =================================================================== --- cpp-commons/trunk/src/commons/check.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/check.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -47,7 +47,7 @@ const string name; }; - __attribute__((format(printf, 4, 0))) inline void + __attribute__((format(printf, 4, 0))) inline static void _vcheck(bool cond, const char *file, int line, const char *fmt, va_list ap) { if (!cond) { @@ -62,7 +62,7 @@ } } - __attribute__((format(printf, 4, 5))) void + __attribute__((format(printf, 4, 5))) static void _check(bool cond, const char *file, int line, const char *fmt, ...) { va_list ap; @@ -71,7 +71,7 @@ va_end(ap); } - inline void + inline static void _check(bool cond, const char *file, int line) { _check(cond, file, line, NULL); Modified: cpp-commons/trunk/src/commons/die.h =================================================================== --- cpp-commons/trunk/src/commons/die.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/die.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -15,7 +15,7 @@ * * TODO: move into C Commons. */ - __attribute__((format(printf, 1, 2))) void + __attribute__((format(printf, 1, 2),unused)) static void die(const char *format, ...) { const char *errstr; Modified: cpp-commons/trunk/src/commons/memory.h =================================================================== --- cpp-commons/trunk/src/commons/memory.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/memory.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -38,6 +38,8 @@ void write(const T &x) { memput(p_, x); p_ += sizeof(T); } /** Get the current value of the pointer. */ void *ptr() const { return p_; } + /** Skip n bytes. */ + void skip(size_t n) { p_ += n; } }; class raw_reader @@ -49,7 +51,12 @@ template<typename T> void read(T& x) { memget(p_, x); p_ += sizeof(T); } template<typename T> - T &read() { void *p = p_; p_ += sizeof(T); return memget<T>(p_); } + T &read() { void *p = p_; p_ += sizeof(T); return memget<T>(p); } + template<typename T> + T *readptr() { return reinterpret_cast<T*>(readptr(sizeof(T))); } + void *readptr(size_t n) { void *p = p_; p_ += n; return p; } + void *ptr() const { return p_; } + void skip(size_t n) { p_ += n; } }; } Modified: cpp-commons/trunk/src/commons/streamreader.h =================================================================== --- cpp-commons/trunk/src/commons/streamreader.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/streamreader.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -264,6 +264,252 @@ char *anchor_; }; + /** + * General-purpose stream reader for arbitrary data source streams, plus the + * ability to hold on to (don't discard) parts of the stream. + * + * TODO actually don't need anchor!!! factor that out and rename, and adjust + * the external functions accordingly. also look into replacing the + * pointer-ref-returning functions with just reset() method(s) (overload this + * to replace/not replace the buffer as well). + */ + class anchored_stream_reader + { + NONCOPYABLE(anchored_stream_reader) + public: + anchored_stream_reader(boost::function<size_t(char*, size_t)> reader, + boost::function<void(anchored_stream_reader&)> overflow, + char *buf, size_t bufsize) : + reader_(reader), + full_reader_(repeat_reader(reader_)), + overflow_(overflow), + buf_(buf, bufsize), + start_(buf), + end_(buf), + anchor_(buf) + {} + + anchored_stream_reader(boost::function<size_t(char*, size_t)> reader, + boost::function<void(char*, size_t)> full_reader, + boost::function<void(anchored_stream_reader&)> overflow, + char *buf, size_t bufsize) : + reader_(reader), + full_reader_(full_reader), + overflow_(overflow), + buf_(buf, bufsize), + start_(buf), + end_(buf), + anchor_(buf) + {} + + /** + * The size of the unconsumed range of bytes. + */ + size_t unread() { return end_ - start_; } + size_t anchored() { return end_ - anchor_; } + + /** + * The remaining number of bytes in the buffer + */ + size_t rem() { return buf_.end() - end_; } + + /** + * The entire read buffer. + */ + sized_array<char> &buf() { return buf_; } + + void set_anchor() { anchor_ = start_; } + + /** + * Reset the pointers, emptying the buffer. + */ + void reset() { + start_ = end_ = anchor_ = buf_.get(); + } + + char *start() const { return start_; } + char *end() const { return end_; } + char *anchor() const { return anchor_; } + char *&start() { return start_; } + char *&end() { return end_; } + char *&anchor() { return anchor_; } + +#if 0 + /** + * Discard the requested number of bytes. Disregards anchor. + */ + void skip(size_t req) { + if (unread() >= req) { + // We have more unconsumed bytes than requested, so we're done. + start_ += req; + return; + } + + // We have more requested bytes than unconsumed, so need to keep + // reading. Skip over bytes that are immediately available... + req -= unread(); + // ...and reset pointers to discard current buffer. + reset(); + + // Keep reading until we have enough. + while (true) { + size_t res = reader_(end_, rem()); + if (res == 0) throw eof_exception(); + if (res == req) break; + if (res > req) { + // Now skip over the rest of the bytes, which weren't available. + start_ += req; + end_ += res; + break; + } + req -= res; + } + } +#endif + + /** + * Read req bytes; if we hit an error or EOF, then an exception is + * thrown. Returns old value of start() normally (this call then + * increments start()), or returns nullptr if req too large for buffer. + */ + char *read(size_t req) { + // Do we need to perform a read (or do we already + // have requested data)? + if (unread() < req) { + // Do we have enough space? + if (req > buf_.size()) return nullptr; + + // Shift things down if necessary. + if (req > rem()) overflow_(*this); + ASSERT(req <= rem()); + + // Keep reading until we have enough. + while (unread() < req) { + size_t res = reader_(end_, rem()); + if (res == 0) break; + else end_ += res; + } + + // If we got a premature EOF. + if (unread() < req) + throw eof_exception(); + } + + char *ret = start_; + start_ += req; + return ret; + } + + template<typename T> + T read() + { + size_t req = sizeof(T); + + // Do we need to perform a read (or do we already + // have the requested data)? + if (unread() < req) { + ASSERT(req <= buf_.size()); + + // Shift things down if necessary. + if (req > rem()) overflow_(*this); + ASSERT(req <= rem()); + + // Keep reading until we have enough. + while (unread() < req) { + size_t res = reader_(end_, rem()); + if (res == 0) break; + else end_ += res; + } + + // If we got a premature EOF. + if (unread() < req) + throw eof_exception(); + } + + T x = *reinterpret_cast<const T*>(start_); + start_ += req; + return x; + } + +#if 0 + /** + * Shift the unread bytes down to the start of the buffer. + */ + void shift() { + memmove(buf_.get(), start_, unread()); + size_t diff = start_ - buf_.get(); + start_ -= diff; + end_ -= diff; + } +#endif + + private: + boost::function<size_t(char*, size_t)> reader_; + + boost::function<void(char*, size_t)> full_reader_; + + boost::function<void(anchored_stream_reader&)> overflow_; + + /** + * The temporary storage buffer. + */ + sized_array<char> buf_; + + /** + * The start of the unconsumed range of bytes. + */ + char *start_; + + /** + * The end of the unconsumed range of bytes. + */ + char *end_; + + /** + * Do not discard bytes >= this. + */ + char *anchor_; + }; + + template<typename T> + inline void + reset_reader(T &reader, char *anchor, char *start, char *end) { + reader.anchor() = anchor; + reader.start() = start; + reader.end() = end; + } + + template<typename T> + inline void + reset_reader(T &reader, sized_array<char> &buf, char *anchor, char *start, char *end) { + swap(reader.buf(), buf); + reset_reader(reader, anchor, start, end); + } + + template<typename T> + inline void + shift_reader(T &reader) + { + memmove(reader.buf().get(), reader.anchor(), reader.anchored()); + ptrdiff_t diff = reader.anchor() - reader.buf().get(); + reset_reader(reader, + reader.anchor() - diff, + reader.start() - diff, + reader.end() - diff); + } + + template<typename T> + inline void + replace_reader(T &reader) + { + sized_array<char> buf(new char[reader.buf().size()], reader.buf().size()); + memcpy(buf.get(), reader.anchor(), reader.anchored()); + reset_reader(reader, buf, + buf.get(), + buf.get() + (reader.start() - reader.anchor()), + buf.get() + (reader.end() - reader.anchor())); + } + } #endif Modified: cpp-commons/trunk/src/commons/streamwriter.h =================================================================== --- cpp-commons/trunk/src/commons/streamwriter.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/streamwriter.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -23,7 +23,7 @@ char *mark_; char *p_; boost::function<void(void*, size_t)> flushcb; - char *reserve(int n, char *p) { + char *reserve(size_t 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()); @@ -51,15 +51,16 @@ size_t pos() { return p_ - mark_; } size_t size() { return a_.size(); } void mark() { + ASSERT(p_ >= mark_); if (p_ > mark_) { // prefix last segment with its length - *reinterpret_cast<uint32_t*>(prefix()) = uint32_t(p_ - mark_); + *reinterpret_cast<uint32_t*>(prefix()) = htonl(uint32_t(pos())); // start new segment mark_ = (p_ += sizeof(uint32_t)); } } void reset() { p_ = mark_; } - void reserve(int n) { reserve(n, p_); } + void reserve(size_t n) { reserve(n, p_); } void mark_and_flush() { mark(); flush(); @@ -71,7 +72,8 @@ unsent_ = prefix(); } } - template<typename T> void skip() { reserve(sizeof(T)); p_ += sizeof(T); } + void skip(size_t n) { reserve(n); p_ += n; } + template<typename T> void skip() { skip(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); } void show() { Modified: cpp-commons/trunk/src/commons/time.h =================================================================== --- cpp-commons/trunk/src/commons/time.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/time.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -7,6 +7,8 @@ #include <sys/time.h> #include <time.h> +#include <commons/check.h> + namespace commons { @@ -19,15 +21,9 @@ inline long long current_time_millis() { - long long t; struct timeval tv; - - gettimeofday(&tv, 0); - - t = tv.tv_sec; - t = (t *1000) + (tv.tv_usec/1000); - - return t; + check0x(gettimeofday(&tv, 0)); + return 1000LL * tv.tv_sec + tv.tv_usec / 1000; } /** Modified: cpp-commons/trunk/src/commons/utility.h =================================================================== --- cpp-commons/trunk/src/commons/utility.h 2009-03-18 09:36:57 UTC (rev 1302) +++ cpp-commons/trunk/src/commons/utility.h 2009-03-18 09:58:41 UTC (rev 1303) @@ -11,4 +11,8 @@ * expand the given type. */ #define EXPAND(type) +/** To avoid indentation. */ +#define BEGIN_NAMESPACE(ns) namespace ns { +#define END_NAMESPACE } + #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |