Handler for when a ConstString is assigned to an already equal ConstString.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-21 13:56:06 +02:00
parent 9bd30cb9c4
commit 96995d071f
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 8 additions and 9 deletions

View File

@ -11,9 +11,11 @@
namespace Arbutils { \
class name { \
private: \
__ConstStringCharHolder* _str = nullptr; \
__ConstStringCharHolder* _str; \
size_t _length; \
uint32_t _hash; \
hashFunction; \
\
static __ConstStringCharHolder* __emptyString; \
static inline __ConstStringCharHolder* GetEmptyString() { \
if (__emptyString == nullptr) \
@ -21,13 +23,12 @@
return __emptyString; \
} \
\
hashFunction; \
\
inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \
\
public: \
name(const char* str, size_t size) \
: _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) {} \
\
name() : _str(GetEmptyString()), _length(0), _hash(Hash("")) { GetEmptyString()->AddReference(); }; \
explicit name(const char* str) : name(str, Length(str)){}; \
explicit name(const std::string& str) : name(str.c_str(), str.size()){}; \
@ -35,8 +36,9 @@
_str->AddReference(); \
} \
name& operator=(const name& other) { \
if (_str != nullptr) \
_str->RemoveReference(); \
if (_str == other._str) \
return *this; \
_str->RemoveReference(); \
_str = other._str; \
_str->AddReference(); \
_length = other._length; \
@ -44,10 +46,7 @@
return *this; \
} \
\
~name() { \
if (_str != nullptr) \
_str->RemoveReference(); \
} \
~name() { _str->RemoveReference(); } \
\
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str->GetValue(); } \
[[nodiscard]] inline std::string std_str() const { return std::string(_str->GetValue(), _length); } \