diff --git a/include/mbgl/util/string_indexer.hpp b/include/mbgl/util/string_indexer.hpp index 7a0582369f6..de7d88b8486 100644 --- a/include/mbgl/util/string_indexer.hpp +++ b/include/mbgl/util/string_indexer.hpp @@ -14,9 +14,10 @@ namespace mbgl { using StringIdentity = std::size_t; class StringIndexer { -public: +protected: StringIndexer(); +public: StringIndexer(StringIndexer const&) = delete; StringIndexer(StringIndexer&&) = delete; void operator=(StringIndexer const&) = delete; @@ -29,6 +30,8 @@ class StringIndexer { size_t size(); protected: + friend StringIndexer& stringIndexer(); + std::unordered_map stringToIdentity; std::vector identityToString; std::vector buffer; diff --git a/src/mbgl/util/string_indexer.cpp b/src/mbgl/util/string_indexer.cpp index 0ead2f6a6b6..974ec8f55fb 100644 --- a/src/mbgl/util/string_indexer.cpp +++ b/src/mbgl/util/string_indexer.cpp @@ -76,6 +76,8 @@ size_t StringIndexer::size() { } StringIndexer& stringIndexer() { + // This is a static local rather than a global or member of `StringIndexer` so + // that static initializers can use it without worrying about ordering. static StringIndexer inst; return inst; } diff --git a/test/util/string_indexer.test.cpp b/test/util/string_indexer.test.cpp index 60ed60f6513..1fabe5d48ea 100644 --- a/test/util/string_indexer.test.cpp +++ b/test/util/string_indexer.test.cpp @@ -4,7 +4,13 @@ #include -using namespace mbgl; +using mbgl::stringIndexer; + +// Allow public default construction +class StringIndexer : public mbgl::StringIndexer { +public: + StringIndexer() {} +}; TEST(StringIndexer, SingletonStringIndexer) { EXPECT_GE(stringIndexer().size(), 0);