From 92970176881fde9fc7062005a3ee56dd9a88191c Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Mon, 6 Feb 2023 01:16:26 -0800 Subject: [PATCH] [util] Improve util::counted usage as a bool I often want to use util::counted as a bool, like with a regular pointer. There was some basic support for that, but it didn't cover every case - now it should. --- src/libraries/util/util/counted.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libraries/util/util/counted.h b/src/libraries/util/util/counted.h index ed59225..7ac2b4b 100644 --- a/src/libraries/util/util/counted.h +++ b/src/libraries/util/util/counted.h @@ -12,8 +12,8 @@ namespace util { template struct counted { - T *pointer; - size_t count; + T *pointer = nullptr; + size_t count = 0; /// Index this object as an array of type T inline T & operator [] (int i) { return pointer[i]; } @@ -21,6 +21,9 @@ struct counted /// Index this object as a const array of type T inline const T & operator [] (int i) const { return pointer[i]; } + operator bool() const { return pointer != nullptr; } + bool operator!() const { return pointer == nullptr; } + using iterator = offset_iterator; using const_iterator = const_offset_iterator; @@ -63,14 +66,17 @@ struct counted template <> struct counted { - const void *pointer; - size_t count; + const void *pointer = nullptr; + size_t count = 0; template static inline counted from(T *p, size_t c) { return {reinterpret_cast(p), c}; } + operator bool() const { return pointer != nullptr; } + bool operator!() const { return pointer == nullptr; } + /// Return a counted advanced by N items inline counted operator+(size_t i) { counted other = *this; @@ -98,8 +104,8 @@ struct counted template <> struct counted { - void *pointer; - size_t count; + void *pointer = nullptr; + size_t count = 0; template static inline counted from(T *p, size_t c) { @@ -108,6 +114,9 @@ struct counted operator counted() const { return {pointer, count}; } + operator bool() const { return pointer != nullptr; } + bool operator!() const { return pointer == nullptr; } + /// Return a counted advanced by N items inline counted operator+(size_t i) { counted other = *this;