summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/Handle.h15
-rw-r--r--src/Handle.cc63
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<uint8_t> readU8();
Future<uint16_t> readU16();
Future<uint32_t> readU32();
Future<uint64_t> readU64();
+ Future<int8_t> readI8();
+ Future<int16_t> readI16();
+ Future<int32_t> readI32();
+ Future<int64_t> readI64();
+ Future<void> writeU8 (uint8_t);
+ Future<void> writeU16(uint16_t);
+ Future<void> writeU32(uint32_t);
+ Future<void> writeU64(uint64_t);
+ Future<void> writeI8 (int8_t);
+ Future<void> writeI16(int16_t);
+ Future<void> writeI32(int32_t);
+ Future<void> 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<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() + ")");