diff --git a/src/ConstString.hpp b/src/ConstString.hpp index 12121a3..2798990 100644 --- a/src/ConstString.hpp +++ b/src/ConstString.hpp @@ -13,15 +13,18 @@ namespace Arbutils { char* _value; std::atomic _references; + __ConstStringCharHolder(const __ConstStringCharHolder& o) = delete; + __ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete; + public: - __ConstStringCharHolder(const char* value, size_t size) : _value(new char[size + 1]), _references(0) { + __ConstStringCharHolder(const char* value, size_t size) : _value(new char[size + 1]), _references(1) { strncpy(_value, value, size + 1); } ~__ConstStringCharHolder() { delete[] _value; } inline void RemoveReference() { - if (--_references == 0) { + if (--_references <= 0) { delete this; } } diff --git a/src/__ConstStringCore.hpp b/src/__ConstStringCore.hpp index e44dc05..ed1856b 100644 --- a/src/__ConstStringCore.hpp +++ b/src/__ConstStringCore.hpp @@ -11,23 +11,19 @@ namespace Arbutils { \ class name { \ private: \ - __ConstStringCharHolder* _str; \ + __ConstStringCharHolder* _str = nullptr; \ size_t _length; \ uint32_t _hash; \ + static __ConstStringCharHolder* _emptyString; \ \ hashFunction; \ \ inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \ \ - name(const char* str, size_t size, uint32_t hash) \ - : _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(hash) {} \ - \ public: \ name(const char* str, size_t size) \ - : _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) { \ - _str->AddReference(); \ - } \ - name() : name("", 0){}; \ + : _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) {} \ + name() : _str(_emptyString), _length(0), _hash(Hash("")) { _emptyString->AddReference(); }; \ explicit name(const char* str) : name(str, Length(str)){}; \ 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) { \ @@ -73,6 +69,7 @@ inline static constexpr uint32_t GetHash(const char* val) { return Hash(val); } \ inline static STDSTRINGCONSTEXPR uint32_t GetHash(const std::string& val) { return Hash(val.c_str()); } \ }; \ + Arbutils::__ConstStringCharHolder* Arbutils::name::_emptyString = new __ConstStringCharHolder("", 0); \ \ class name##_Literal { \ private: \