From 9bd30cb9c48d0ee6b7648c1820a523d535ef0af6 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 21 Apr 2020 10:20:37 +0200 Subject: [PATCH] Rework of empty string handler on ConstString. --- src/ConstString.cpp | 6 ++---- src/__ConstStringCore.hpp | 9 +++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ConstString.cpp b/src/ConstString.cpp index 0f81ad9..cddff04 100644 --- a/src/ConstString.cpp +++ b/src/ConstString.cpp @@ -1,6 +1,4 @@ #include "ConstString.hpp" -Arbutils::__ConstStringCharHolder* Arbutils::CaseInsensitiveConstString::_emptyString = - new Arbutils::__ConstStringCharHolder("", 0); - -Arbutils::__ConstStringCharHolder* Arbutils::ConstString::_emptyString = new Arbutils::__ConstStringCharHolder("", 0); \ No newline at end of file +Arbutils::__ConstStringCharHolder* Arbutils::CaseInsensitiveConstString::__emptyString = nullptr; +Arbutils::__ConstStringCharHolder* Arbutils::ConstString::__emptyString = nullptr; \ No newline at end of file diff --git a/src/__ConstStringCore.hpp b/src/__ConstStringCore.hpp index 74b0e96..1866989 100644 --- a/src/__ConstStringCore.hpp +++ b/src/__ConstStringCore.hpp @@ -14,7 +14,12 @@ __ConstStringCharHolder* _str = nullptr; \ size_t _length; \ uint32_t _hash; \ - static __ConstStringCharHolder* _emptyString; \ + static __ConstStringCharHolder* __emptyString; \ + static inline __ConstStringCharHolder* GetEmptyString() { \ + if (__emptyString == nullptr) \ + __emptyString = new __ConstStringCharHolder("", 0); \ + return __emptyString; \ + } \ \ hashFunction; \ \ @@ -23,7 +28,7 @@ public: \ name(const char* str, size_t size) \ : _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) {} \ - name() : _str(_emptyString), _length(0), _hash(Hash("")) { _emptyString->AddReference(); }; \ + 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()){}; \ name(const name& other) : _str(other._str), _length(other._length), _hash(other._hash) { \