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. --- src/Handle.cc | 63 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 34 deletions(-) (limited to 'src/Handle.cc') 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