From 7a64e86e58bca7456ca9b1ff9b5759af0befa184 Mon Sep 17 00:00:00 2001 From: Nicolas Noble Date: Wed, 7 Aug 2013 17:08:00 -0700 Subject: Adding the rest of the helpers, and simplifying their implementations. --- includes/Handle.h | 15 +++++++++++-- src/Handle.cc | 63 +++++++++++++++++++++++++------------------------------ 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/includes/Handle.h b/includes/Handle.h index 06b1723..fdadef5 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -56,17 +56,28 @@ class Handle { virtual bool isPendingComplete() { return true; } // helpers - ssize_t write(const String & str) { return write(str.to_charp(), str.strlen()); } off_t tell() { return rtell(); } void seek(off_t offset, int whence = SEEK_SET) { rseek(offset, whence); } - // there need to be more of these Future readU8(); Future readU16(); Future readU32(); Future readU64(); + Future readI8(); + Future readI16(); + Future readI32(); + Future readI64(); + Future writeU8 (uint8_t); + Future writeU16(uint16_t); + Future writeU32(uint32_t); + Future writeU64(uint64_t); + Future writeI8 (int8_t); + Future writeI16(int16_t); + Future writeI32(int32_t); + Future writeI64(int64_t); // these need to be changed into Future<>s + ssize_t write(const String & str) { return write(str.to_charp(), str.strlen()); } void writeString(const char * str, ssize_t len) { if (len < 0) len = strlen(str); forceWrite(str, len); } void writeString(const String & str) { forceWrite(str.to_charp(), str.strlen()); } ssize_t forceRead(void * buf, size_t count, Events::BaseEvent * evt = NULL) throw (GeneralException); diff --git a/src/Handle.cc b/src/Handle.cc index 2a03f8c..1a5255d 100644 --- a/src/Handle.cc +++ b/src/Handle.cc @@ -95,54 +95,49 @@ ssize_t Balau::Handle::forceWrite(const void * _buf, size_t count, Events::BaseE return total; } -Balau::Future Balau::Handle::readU8() { - IO t(this); - std::shared_ptr b(new uint8_t); - return Future([t, b]() mutable { - t->read(b.get(), 1); - return *b; - }); -} - -Balau::Future Balau::Handle::readU16() { - IO t(this); - std::shared_ptr b(new uint16_t); +template +Balau::Future genericRead(Balau::IO t) { + std::shared_ptr b(new T); int c = 0; - return Future([t, b, c]() mutable { + return Balau::Future([t, b, c]() mutable { do { - int r = t->read(((uint8_t *) b.get()) + c, 2); + int r = t->read(((uint8_t *) b.get()) + c, sizeof(T)); c += r; - } while (c < 2); + } while (c < sizeof(T)); return *b; }); } -Balau::Future Balau::Handle::readU32() { - IO t(this); - std::shared_ptr b(new uint32_t); - int c = 0; - return Future([t, b, c]() mutable { - do { - int r = t->read(((uint8_t *) b.get()) + c, 4); - c += r; - } while (c < 4); - return *b; - }); -} +Balau::Future Balau::Handle::readU8 () { return genericRead(this); } +Balau::Future Balau::Handle::readU16() { return genericRead(this); } +Balau::Future Balau::Handle::readU32() { return genericRead(this); } +Balau::Future Balau::Handle::readU64() { return genericRead(this); } +Balau::Future Balau::Handle::readI8 () { return genericRead (this); } +Balau::Future Balau::Handle::readI16() { return genericRead (this); } +Balau::Future Balau::Handle::readI32() { return genericRead (this); } +Balau::Future Balau::Handle::readI64() { return genericRead (this); } -Balau::Future Balau::Handle::readU64() { - IO t(this); - std::shared_ptr b(new uint64_t); +template +Balau::Future genericWrite(Balau::IO t, T val) { + std::shared_ptr b(new T(val)); int c = 0; - return Future([t, b, c]() mutable { + return Balau::Future([t, b, c]() mutable { do { - int r = t->read(((uint8_t *) b.get()) + c, 8); + int r = t->write(((uint8_t *) b.get()) + c, sizeof(T)); c += r; - } while (c < 8); - return *b; + } while (c < sizeof(T)); }); } +Balau::Future Balau::Handle::writeU8 (uint8_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeU16(uint16_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeU32(uint32_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeU64(uint64_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeI8 (int8_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeI16(int16_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeI32(int32_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeI64(int64_t v) { return genericWrite(this, v); } + void Balau::Handle::rseek(off_t offset, int whence) throw (GeneralException) { if (canSeek()) throw GeneralException(String("Handle ") + getName() + " can seek, but rseek() not implemented (missing in class " + ClassName(this).c_str() + ")"); -- cgit v1.2.3