Tweaks and fixes for ConstString.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-20 19:38:45 +02:00
parent 7d98d5b467
commit 79384efb85
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 10 additions and 10 deletions

View File

@ -13,15 +13,18 @@ namespace Arbutils {
char* _value; char* _value;
std::atomic<size_t> _references; std::atomic<size_t> _references;
__ConstStringCharHolder(const __ConstStringCharHolder& o) = delete;
__ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete;
public: 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); strncpy(_value, value, size + 1);
} }
~__ConstStringCharHolder() { delete[] _value; } ~__ConstStringCharHolder() { delete[] _value; }
inline void RemoveReference() { inline void RemoveReference() {
if (--_references == 0) { if (--_references <= 0) {
delete this; delete this;
} }
} }

View File

@ -11,23 +11,19 @@
namespace Arbutils { \ namespace Arbutils { \
class name { \ class name { \
private: \ private: \
__ConstStringCharHolder* _str; \ __ConstStringCharHolder* _str = nullptr; \
size_t _length; \ size_t _length; \
uint32_t _hash; \ uint32_t _hash; \
static __ConstStringCharHolder* _emptyString; \
\ \
hashFunction; \ hashFunction; \
\ \
inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \ 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: \ 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->AddReference(); \ name() : _str(_emptyString), _length(0), _hash(Hash("")) { _emptyString->AddReference(); }; \
} \
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) { \
@ -73,6 +69,7 @@
inline static constexpr uint32_t GetHash(const char* val) { return Hash(val); } \ 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()); } \ 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 { \ class name##_Literal { \
private: \ private: \