summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2012-10-22 20:47:08 +0000
committerChris Wilson <chris+github@qwirx.com>2012-10-22 20:47:08 +0000
commit0894ad63bb19d47c3748d81424dbd5e7412b29fa (patch)
treeacd791361a89f44d80adf28f813849956b20801b
parentd11ed0d3d22308babe7430d3ba4b8371fdb104fd (diff)
Add helper method to read a value that might not be present in an Archive
(end of Archive) to avoid duplicating this code many times.
-rw-r--r--lib/common/Archive.h27
1 files changed, 26 insertions, 1 deletions
diff --git a/lib/common/Archive.h b/lib/common/Archive.h
index f2e5bd83..6d5ce88b 100644
--- a/lib/common/Archive.h
+++ b/lib/common/Archive.h
@@ -81,7 +81,7 @@ public:
int privItem;
Read(privItem);
- if (privItem)
+ if(privItem)
{
rItemOut = true;
}
@@ -90,6 +90,12 @@ public:
rItemOut = false;
}
}
+ void ReadIfPresent(bool &rItemOut, bool ValueIfNotPresent)
+ {
+ int privItem;
+ ReadIfPresent(privItem, ValueIfNotPresent ? 1 : 0);
+ rItemOut = privItem ? true : false;
+ }
void ReadExact(uint32_t &rItemOut) { Read((int&)rItemOut); }
void Read(int &rItemOut)
{
@@ -100,6 +106,25 @@ public:
}
rItemOut = ntohl(privItem);
}
+ void ReadIfPresent(int &rItemOut, int ValueIfNotPresent)
+ {
+ int32_t privItem;
+ int bytesRead;
+ if(mrStream.ReadFullBuffer(&privItem, sizeof(privItem), &bytesRead))
+ {
+ rItemOut = ntohl(privItem);
+ }
+ else if(bytesRead == 0)
+ {
+ // item is simply not present
+ rItemOut = ValueIfNotPresent;
+ }
+ else
+ {
+ // bad number of remaining bytes
+ THROW_EXCEPTION(CommonException, ArchiveBlockIncompleteRead)
+ }
+ }
void Read(int64_t &rItemOut)
{
int64_t privItem;