Skip to content

Commit

Permalink
Make the StringIndexer default constructor protected (#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored Nov 9, 2023
1 parent c8010db commit a880247
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion include/mbgl/util/string_indexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,8 @@ class StringIndexer {
size_t size();

protected:
friend StringIndexer& stringIndexer();

std::unordered_map<std::string_view, StringIdentity> stringToIdentity;
std::vector<StringIdentity> identityToString;
std::vector<char> buffer;
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/util/string_indexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion test/util/string_indexer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

#include <cstdint>

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);
Expand Down

0 comments on commit a880247

Please sign in to comment.