mirror of
https://github.com/justinian/jsix.git
synced 2025-12-09 16:04:32 -08:00
[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.
This commit is contained in:
@@ -12,8 +12,8 @@ namespace util {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct counted
|
struct counted
|
||||||
{
|
{
|
||||||
T *pointer;
|
T *pointer = nullptr;
|
||||||
size_t count;
|
size_t count = 0;
|
||||||
|
|
||||||
/// Index this object as an array of type T
|
/// Index this object as an array of type T
|
||||||
inline T & operator [] (int i) { return pointer[i]; }
|
inline T & operator [] (int i) { return pointer[i]; }
|
||||||
@@ -21,6 +21,9 @@ struct counted
|
|||||||
/// Index this object as a const array of type T
|
/// Index this object as a const array of type T
|
||||||
inline const T & operator [] (int i) const { return pointer[i]; }
|
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<T>;
|
using iterator = offset_iterator<T>;
|
||||||
using const_iterator = const_offset_iterator<T>;
|
using const_iterator = const_offset_iterator<T>;
|
||||||
|
|
||||||
@@ -63,14 +66,17 @@ struct counted
|
|||||||
template <>
|
template <>
|
||||||
struct counted<const void>
|
struct counted<const void>
|
||||||
{
|
{
|
||||||
const void *pointer;
|
const void *pointer = nullptr;
|
||||||
size_t count;
|
size_t count = 0;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static inline counted<const void> from(T *p, size_t c) {
|
static inline counted<const void> from(T *p, size_t c) {
|
||||||
return {reinterpret_cast<const void*>(p), c};
|
return {reinterpret_cast<const void*>(p), c};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
operator bool() const { return pointer != nullptr; }
|
||||||
|
bool operator!() const { return pointer == nullptr; }
|
||||||
|
|
||||||
/// Return a counted<T> advanced by N items
|
/// Return a counted<T> advanced by N items
|
||||||
inline counted<const void> operator+(size_t i) {
|
inline counted<const void> operator+(size_t i) {
|
||||||
counted<const void> other = *this;
|
counted<const void> other = *this;
|
||||||
@@ -98,8 +104,8 @@ struct counted<const void>
|
|||||||
template <>
|
template <>
|
||||||
struct counted<void>
|
struct counted<void>
|
||||||
{
|
{
|
||||||
void *pointer;
|
void *pointer = nullptr;
|
||||||
size_t count;
|
size_t count = 0;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static inline counted<void> from(T *p, size_t c) {
|
static inline counted<void> from(T *p, size_t c) {
|
||||||
@@ -108,6 +114,9 @@ struct counted<void>
|
|||||||
|
|
||||||
operator counted<const void>() const { return {pointer, count}; }
|
operator counted<const void>() const { return {pointer, count}; }
|
||||||
|
|
||||||
|
operator bool() const { return pointer != nullptr; }
|
||||||
|
bool operator!() const { return pointer == nullptr; }
|
||||||
|
|
||||||
/// Return a counted<T> advanced by N items
|
/// Return a counted<T> advanced by N items
|
||||||
inline counted<void> operator+(size_t i) {
|
inline counted<void> operator+(size_t i) {
|
||||||
counted<void> other = *this;
|
counted<void> other = *this;
|
||||||
|
|||||||
Reference in New Issue
Block a user