Replace StringView with shared_ptr.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
5994299baa
commit
d3aacee0ef
|
@ -1,3 +1,7 @@
|
|||
#include "StringView.hpp"
|
||||
#include <memory>
|
||||
|
||||
ArbUt::__ConstStringCharHolder* ArbUt::StringView::__emptyString = new __ConstStringCharHolder("", 0);
|
||||
namespace ArbUt {
|
||||
std::shared_ptr<__ConstStringCharHolder> StringView::__emptyString =
|
||||
std::make_shared<__ConstStringCharHolder>("", 0);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "BasicStringView.hpp"
|
||||
|
||||
|
@ -17,26 +18,15 @@
|
|||
namespace ArbUt {
|
||||
class __ConstStringCharHolder {
|
||||
char* _value;
|
||||
std::atomic<size_t> _references;
|
||||
|
||||
__ConstStringCharHolder(const __ConstStringCharHolder& o) = delete;
|
||||
__ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete;
|
||||
|
||||
public:
|
||||
__ConstStringCharHolder(const char* value, size_t length) noexcept
|
||||
: _value(new char[length + 1]), _references(1) {
|
||||
__ConstStringCharHolder(const char* value, size_t length) noexcept : _value(new char[length + 1]) {
|
||||
strncpy(_value, value, length + 1);
|
||||
}
|
||||
|
||||
~__ConstStringCharHolder() noexcept { delete[] _value; }
|
||||
|
||||
inline void RemoveReference() noexcept {
|
||||
if (--_references <= 0) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
inline void AddReference() noexcept { _references++; }
|
||||
inline constexpr const char* GetValue() const noexcept { return _value; }
|
||||
};
|
||||
}
|
||||
|
@ -50,43 +40,33 @@ static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(
|
|||
namespace ArbUt {
|
||||
class StringView final : public BasicStringView {
|
||||
private:
|
||||
static __ConstStringCharHolder* __emptyString;
|
||||
static inline __ConstStringCharHolder* GetEmptyString() { return __emptyString; }
|
||||
static std::shared_ptr<__ConstStringCharHolder> __emptyString;
|
||||
static inline const std::shared_ptr<__ConstStringCharHolder>& GetEmptyString() { return __emptyString; }
|
||||
|
||||
__ConstStringCharHolder* _str;
|
||||
|
||||
inline __ConstStringCharHolder* CloneHolder() const noexcept {
|
||||
_str->AddReference();
|
||||
return _str;
|
||||
}
|
||||
std::shared_ptr<__ConstStringCharHolder> _str;
|
||||
|
||||
public:
|
||||
StringView(const char* str) noexcept
|
||||
: BasicStringView(CalcLength(str), Hash(str)), _str(new __ConstStringCharHolder(str, CalcLength(str))) {}
|
||||
StringView(const char* str, size_t length) noexcept
|
||||
: BasicStringView(length, Hash(str)), _str(new __ConstStringCharHolder(str, length)) {}
|
||||
StringView() noexcept : BasicStringView(0, Hash("")), _str(GetEmptyString()) { _str->AddReference(); }
|
||||
StringView() noexcept : BasicStringView(0, Hash("")), _str(GetEmptyString()) {}
|
||||
|
||||
/* Copy operators */
|
||||
StringView(const StringView& other) noexcept
|
||||
: BasicStringView(other._length, other._hash), _str(other.CloneHolder()) {}
|
||||
StringView(const StringView& other) noexcept : BasicStringView(other._length, other._hash), _str(other._str) {}
|
||||
StringView(const BasicStringView& other, const char* c, size_t length) noexcept
|
||||
: BasicStringView(length, other.GetHash()), _str(new __ConstStringCharHolder(c, length)) {}
|
||||
|
||||
StringView& operator=(const StringView& other) noexcept {
|
||||
if (_str == other._str) {
|
||||
_str->AddReference();
|
||||
return *this;
|
||||
}
|
||||
_str->RemoveReference();
|
||||
_str = other.CloneHolder();
|
||||
_str = other._str;
|
||||
_length = other._length;
|
||||
_hash = other._hash;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~StringView() noexcept { _str->RemoveReference(); }
|
||||
|
||||
[[nodiscard]] inline constexpr const char* c_str() const noexcept override { return _str->GetValue(); }
|
||||
[[nodiscard]] inline std::string_view std_str() const noexcept override { return _str->GetValue(); }
|
||||
|
||||
|
@ -99,8 +79,11 @@ namespace ArbUt {
|
|||
inline constexpr bool operator==(const char* rhs) const noexcept override { return _hash == Hash(rhs); }
|
||||
inline constexpr bool operator!=(const char* rhs) const noexcept override { return _hash != Hash(rhs); }
|
||||
|
||||
inline static constexpr uint32_t CalculateHash(const char* val) noexcept { return Hash(val); }
|
||||
inline static constexpr uint32_t CalculateHash(const std::string_view& val) noexcept {
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr uint32_t CalculateHash(const char* val) noexcept {
|
||||
return Hash(val);
|
||||
}
|
||||
[[maybe_unused]] [[nodiscard]] inline static constexpr uint32_t
|
||||
CalculateHash(const std::string_view& val) noexcept {
|
||||
return Hash(val.data());
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue