Cleanup of ConstString.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
d6b7344dde
commit
de702b5569
|
@ -9,15 +9,28 @@
|
||||||
#include "__ConstStringCore.hpp"
|
#include "__ConstStringCore.hpp"
|
||||||
|
|
||||||
namespace Arbutils {
|
namespace Arbutils {
|
||||||
struct __ConstStringCharHolder {
|
class __ConstStringCharHolder {
|
||||||
char* Value;
|
char* _value;
|
||||||
std::atomic<size_t> References;
|
std::atomic<size_t> _references;
|
||||||
|
|
||||||
__ConstStringCharHolder(const char* value, size_t size) : Value(new char[size + 1]), References(0) {
|
public:
|
||||||
strncpy(Value, value, size + 1);
|
__ConstStringCharHolder(const char* value, size_t size) : _value(new char[size + 1]), _references(0) {
|
||||||
|
strncpy(_value, value, size + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
~__ConstStringCharHolder() { delete[] Value; }
|
~__ConstStringCharHolder() { delete[] _value; }
|
||||||
|
|
||||||
|
inline void RemoveReference(){
|
||||||
|
if (--_references == 0){
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inline void AddReference(){
|
||||||
|
_references++;
|
||||||
|
}
|
||||||
|
inline const char* GetValue(){
|
||||||
|
return _value;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,31 +25,31 @@
|
||||||
public: \
|
public: \
|
||||||
name(const char* str, size_t size) \
|
name(const char* str, size_t size) \
|
||||||
: _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) { \
|
: _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) { \
|
||||||
_str->References++; \
|
_str->AddReference(); \
|
||||||
} \
|
} \
|
||||||
name() : name("", 0){}; \
|
name() : name("", 0){}; \
|
||||||
explicit name(const char* str) : name(str, Length(str)){}; \
|
explicit name(const char* str) : name(str, Length(str)){}; \
|
||||||
explicit name(const std::string& str) : name(str.c_str(), str.size()){}; \
|
explicit name(const std::string& str) : name(str.c_str(), str.size()){}; \
|
||||||
name(const name& other) : _str(other._str), _length(other._length), _hash(other._hash) { \
|
name(const name& other) : _str(other._str), _length(other._length), _hash(other._hash) { \
|
||||||
_str->References++; \
|
_str->AddReference(); \
|
||||||
} \
|
} \
|
||||||
name& operator=(const name& other) { \
|
name& operator=(const name& other) { \
|
||||||
if (--_str->References == 0) \
|
if (_str != nullptr) \
|
||||||
delete _str; \
|
_str->RemoveReference(); \
|
||||||
_str = other._str; \
|
_str = other._str; \
|
||||||
_str->References++; \
|
_str->AddReference(); \
|
||||||
_length = other._length; \
|
_length = other._length; \
|
||||||
_hash = other._hash; \
|
_hash = other._hash; \
|
||||||
return *this; \
|
return *this; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
~name() { \
|
~name() { \
|
||||||
if (--_str->References == 0) \
|
if (_str != nullptr) \
|
||||||
delete _str; \
|
_str->RemoveReference(); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str->Value; } \
|
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str->GetValue(); } \
|
||||||
[[nodiscard]] inline std::string std_str() const { return std::string(_str->Value, _length); } \
|
[[nodiscard]] inline std::string std_str() const { return std::string(_str->GetValue(), _length); } \
|
||||||
\
|
\
|
||||||
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } \
|
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } \
|
||||||
\
|
\
|
||||||
|
|
Loading…
Reference in New Issue