aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/nu/SafeSize.h
diff options
context:
space:
mode:
Diffstat (limited to 'Src/replicant/nu/SafeSize.h')
-rw-r--r--Src/replicant/nu/SafeSize.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/Src/replicant/nu/SafeSize.h b/Src/replicant/nu/SafeSize.h
new file mode 100644
index 00000000..02c97a07
--- /dev/null
+++ b/Src/replicant/nu/SafeSize.h
@@ -0,0 +1,51 @@
+#pragma once
+
+class SafeSize
+{
+public:
+ SafeSize()
+ {
+ value = 0;
+ overflow = false;
+ }
+
+ void Add(size_t add)
+ {
+ if (!overflow)
+ {
+ value += add;
+ if (value < add)
+ overflow=true;
+ }
+ }
+
+ void AddN(size_t size, size_t count)
+ {
+ if (!overflow)
+ {
+ size_t total = size * count;
+ if (total < size)
+ {
+ overflow = true;
+ }
+ else
+ {
+ Add(total);
+ }
+ }
+ }
+
+ bool Overflowed() const
+ {
+ return overflow;
+ }
+
+ operator size_t ()
+ {
+ return value;
+ }
+
+private:
+ size_t value;
+ bool overflow;
+}; \ No newline at end of file