medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 1607e7a0d84b3c5ff1ed0028419e5c2799b23137
parent ada44f748bfff560a2196a8c152faa057a004e5e
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Dec 24 16:25:13 +0200

Add done/checkpoint/reset methods to ReadStream

Diffstat:
stream.h | 31+++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/stream.h b/stream.h @@ -4,6 +4,15 @@ #include "intrinsics.h" +struct ReadStream; + +struct ReadStreamCheckpoint { + const ReadStream * rs; + char * cursor; + bool ok; + bool used; +}; + struct ReadStream { ReadStream( char * buf, size_t len ) { cursor = buf; @@ -14,6 +23,28 @@ struct ReadStream { template< size_t N > ReadStream( const char ( &buf )[ N ] ) : ReadStream( buf, N ) { } + bool done() const { + return ok && cursor == one_past_end; + } + + ReadStreamCheckpoint checkpoint() const { + ReadStreamCheckpoint cp; + cp.rs = this; + cp.cursor = cursor; + cp.ok = ok; + cp.used = false; + return cp; + } + + void reset( ReadStreamCheckpoint * cp ) { + ASSERT( cp->rs == this ); + ASSERT( !cp->used ); + + cursor = cp->cursor; + ok = cp->ok; + cp->used = true; + } + char * cursor; char * one_past_end; bool ok;