summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNicolas Noble <pixel@nobis-crew.org>2013-08-07 17:08:00 -0700
committerNicolas Noble <pixel@nobis-crew.org>2013-08-07 17:08:00 -0700
commit7a64e86e58bca7456ca9b1ff9b5759af0befa184 (patch)
tree67ef0888d87540964edb088483396710c5db6d8b /src
parentdf00882195da3fb970782f21d79a0a10e6b186b3 (diff)
Adding the rest of the helpers, and simplifying their implementations.
Diffstat (limited to 'src')
-rw-r--r--src/Handle.cc63
1 files changed, 29 insertions, 34 deletions
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<uint8_t> Balau::Handle::readU8() {
- IO<Handle> t(this);
- std::shared_ptr<uint8_t> b(new uint8_t);
- return Future<uint8_t>([t, b]() mutable {
- t->read(b.get(), 1);
- return *b;
- });
-}
-
-Balau::Future<uint16_t> Balau::Handle::readU16() {
- IO<Handle> t(this);
- std::shared_ptr<uint16_t> b(new uint16_t);
+template<class T>
+Balau::Future<T> genericRead(Balau::IO<Balau::Handle> t) {
+ std::shared_ptr<T> b(new T);
int c = 0;
- return Future<uint16_t>([t, b, c]() mutable {
+ return Balau::Future<T>([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<uint32_t> Balau::Handle::readU32() {
- IO<Handle> t(this);
- std::shared_ptr<uint32_t> b(new uint32_t);
- int c = 0;
- return Future<uint32_t>([t, b, c]() mutable {
- do {
- int r = t->read(((uint8_t *) b.get()) + c, 4);
- c += r;
- } while (c < 4);
- return *b;
- });
-}
+Balau::Future<uint8_t> Balau::Handle::readU8 () { return genericRead<uint8_t >(this); }
+Balau::Future<uint16_t> Balau::Handle::readU16() { return genericRead<uint16_t>(this); }
+Balau::Future<uint32_t> Balau::Handle::readU32() { return genericRead<uint32_t>(this); }
+Balau::Future<uint64_t> Balau::Handle::readU64() { return genericRead<uint64_t>(this); }
+Balau::Future<int8_t> Balau::Handle::readI8 () { return genericRead<int8_t> (this); }
+Balau::Future<int16_t> Balau::Handle::readI16() { return genericRead<int16_t> (this); }
+Balau::Future<int32_t> Balau::Handle::readI32() { return genericRead<int32_t> (this); }
+Balau::Future<int64_t> Balau::Handle::readI64() { return genericRead<int64_t> (this); }
-Balau::Future<uint64_t> Balau::Handle::readU64() {
- IO<Handle> t(this);
- std::shared_ptr<uint64_t> b(new uint64_t);
+template<class T>
+Balau::Future<void> genericWrite(Balau::IO<Balau::Handle> t, T val) {
+ std::shared_ptr<T> b(new T(val));
int c = 0;
- return Future<uint64_t>([t, b, c]() mutable {
+ return Balau::Future<void>([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<void> Balau::Handle::writeU8 (uint8_t v) { return genericWrite<uint8_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeU16(uint16_t v) { return genericWrite<uint16_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeU32(uint32_t v) { return genericWrite<uint32_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeU64(uint64_t v) { return genericWrite<uint64_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeI8 (int8_t v) { return genericWrite<int8_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeI16(int16_t v) { return genericWrite<int16_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeI32(int32_t v) { return genericWrite<int32_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeI64(int64_t v) { return genericWrite<int64_t >(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() + ")");