1 // Scintilla source code edit control 2 /** @file UniqueString.h 3 ** Define UniqueString, a unique_ptr based string type for storage in containers 4 ** and an allocator for UniqueString. 5 ** Define UniqueStringSet which holds a set of strings, used to avoid holding many copies 6 ** of font names. 7 **/ 8 // Copyright 2017 by Neil Hodgson <[email protected]> 9 // The License.txt file describes the conditions under which this software may be distributed. 10 11 #ifndef UNIQUESTRING_H 12 #define UNIQUESTRING_H 13 14 namespace Scintilla { 15 IsNullOrEmpty(const char * text)16constexpr bool IsNullOrEmpty(const char *text) noexcept { 17 return text == nullptr || *text == '\0'; 18 } 19 20 using UniqueString = std::unique_ptr<const char[]>; 21 22 /// Equivalent to strdup but produces a std::unique_ptr<const char[]> allocation to go 23 /// into collections. 24 UniqueString UniqueStringCopy(const char *text); 25 26 // A set of strings that always returns the same pointer for each string. 27 28 class UniqueStringSet { 29 private: 30 std::vector<UniqueString> strings; 31 public: 32 UniqueStringSet() noexcept; 33 // UniqueStringSet objects can not be copied. 34 UniqueStringSet(const UniqueStringSet &) = delete; 35 UniqueStringSet &operator=(const UniqueStringSet &) = delete; 36 // UniqueStringSet objects can be moved. 37 UniqueStringSet(UniqueStringSet &&) = default; 38 UniqueStringSet &operator=(UniqueStringSet &&) = default; 39 ~UniqueStringSet(); 40 void Clear() noexcept; 41 const char *Save(const char *text); 42 }; 43 44 } 45 46 #endif 47