From 092dcd174f76bc31a43e03eb4caef4d99cae3aa6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 28 Feb 2015 17:09:29 +0000 Subject: Add StaticStringStorage for storing string literals --- src/shared_string.hh | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/shared_string.hh') diff --git a/src/shared_string.hh b/src/shared_string.hh index 8af9820c..782f1674 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -14,6 +14,9 @@ struct StringStorage : UseMemoryDomain int refcount; int length; + StringStorage() = default; + constexpr StringStorage(int refcount, int length) : refcount(refcount), length(length) {} + [[gnu::always_inline]] char* data() { return reinterpret_cast(this + 1); } [[gnu::always_inline]] @@ -40,8 +43,17 @@ struct StringStorage : UseMemoryDomain StringStorage::operator delete(s, sizeof(StringStorage) + s->length + 1); } - friend void inc_ref_count(StringStorage* s, void*) { ++s->refcount; } - friend void dec_ref_count(StringStorage* s, void*) { if (--s->refcount == 0) StringStorage::destroy(s); } + friend void inc_ref_count(StringStorage* s, void*) + { + if (s->refcount != -1) + ++s->refcount; + } + + friend void dec_ref_count(StringStorage* s, void*) + { + if (s->refcount != -1 and --s->refcount == 0) + StringStorage::destroy(s); + } }; inline RefPtr operator"" _ss(const char* ptr, size_t len) @@ -49,6 +61,22 @@ inline RefPtr operator"" _ss(const char* ptr, size_t len) return StringStorage::create({ptr, (int)len}); } +template +struct StaticStringStorage : StringStorage +{ + template constexpr + StaticStringStorage(const char (&literal)[len], IndexSequence) + : StringStorage{-1, len}, data{literal[I]...} {} + + const char data[len]; +}; + +template +constexpr StaticStringStorage static_storage(const char (&literal)[len]) +{ + return { literal, make_index_sequence() }; +} + class SharedString : public StringView { public: -- cgit v1.2.3