BAN: Rewrite RefPtr,UniqPtr const semantics

const RefPtr<T> now allows accessing T as non-const. Also add default
hash function for RefPtr and UniqPtr based on the pointer value
This commit is contained in:
2026-04-25 21:55:47 +03:00
parent bef53c726b
commit b5647ff258
2 changed files with 28 additions and 36 deletions

View File

@@ -2,9 +2,9 @@
#include <BAN/Atomic.h> #include <BAN/Atomic.h>
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Hash.h>
#include <BAN/Move.h> #include <BAN/Move.h>
#include <BAN/NoCopyMove.h> #include <BAN/NoCopyMove.h>
#include <stdint.h>
namespace BAN namespace BAN
{ {
@@ -129,14 +129,9 @@ namespace BAN
return *this; return *this;
} }
T* ptr() { ASSERT(!empty()); return m_pointer; } T* ptr() const { return m_pointer; }
const T* ptr() const { ASSERT(!empty()); return m_pointer; } T& operator*() const { ASSERT(!empty()); return *ptr(); }
T* operator->() const { ASSERT(!empty()); return ptr(); }
T& operator*() { return *ptr(); }
const T& operator*() const { return *ptr(); }
T* operator->() { return ptr(); }
const T* operator->() const { return ptr(); }
bool operator==(RefPtr other) const { return m_pointer == other.m_pointer; } bool operator==(RefPtr other) const { return m_pointer == other.m_pointer; }
bool operator!=(RefPtr other) const { return m_pointer != other.m_pointer; } bool operator!=(RefPtr other) const { return m_pointer != other.m_pointer; }
@@ -158,4 +153,13 @@ namespace BAN
friend class RefPtr; friend class RefPtr;
}; };
template<typename T>
struct hash<RefPtr<T>>
{
constexpr hash_t operator()(const RefPtr<T>& ptr) const
{
return hash<T*>()(ptr.ptr());
}
};
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Hash.h>
#include <BAN/NoCopyMove.h> #include <BAN/NoCopyMove.h>
namespace BAN namespace BAN
@@ -53,32 +54,12 @@ namespace BAN
return *this; return *this;
} }
T& operator*() T* ptr() const { return m_pointer; }
{ T& operator*() const { ASSERT(!empty()); return *ptr(); }
ASSERT(m_pointer); T* operator->() const { ASSERT(!empty()); return ptr(); }
return *m_pointer;
}
const T& operator*() const bool empty() const { return m_pointer == nullptr; }
{ explicit operator bool() const { return m_pointer; }
ASSERT(m_pointer);
return *m_pointer;
}
T* operator->()
{
ASSERT(m_pointer);
return m_pointer;
}
const T* operator->() const
{
ASSERT(m_pointer);
return m_pointer;
}
T* ptr() { return m_pointer; }
const T* ptr() const { return m_pointer; }
void clear() void clear()
{ {
@@ -87,8 +68,6 @@ namespace BAN
m_pointer = nullptr; m_pointer = nullptr;
} }
operator bool() const { return m_pointer != nullptr; }
private: private:
T* m_pointer = nullptr; T* m_pointer = nullptr;
@@ -96,4 +75,13 @@ namespace BAN
friend class UniqPtr; friend class UniqPtr;
}; };
template<typename T>
struct hash<UniqPtr<T>>
{
constexpr hash_t operator()(const UniqPtr<T>& ptr) const
{
return hash<T*>()(ptr.ptr());
}
};
} }