summaryrefslogtreecommitdiff
path: root/src/mbgl/style/class_dictionary.cpp
blob: 53ea5c1484b3267fb8f9dbb8d32897b6c461d253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <mbgl/style/class_dictionary.hpp>

#include <pthread.h>

namespace mbgl {

ClassDictionary::ClassDictionary() {}

ClassDictionary &ClassDictionary::Get() {
    static pthread_once_t store_once = PTHREAD_ONCE_INIT;
    static pthread_key_t store_key;

    // Create the key.
    pthread_once(&store_once, []() {
        pthread_key_create(&store_key, [](void *ptr) {
            delete reinterpret_cast<ClassDictionary *>(ptr);
        });
    });

    ClassDictionary *ptr = reinterpret_cast<ClassDictionary *>(pthread_getspecific(store_key));
    if (ptr == nullptr) {
        ptr = new ClassDictionary();
        pthread_setspecific(store_key, ptr);
    }

    return *ptr;
}

ClassID ClassDictionary::lookup(const std::string &class_name) {
    auto it = store.find(class_name);
    if (it == store.end()) {
        // Insert the class name into the store.
        ClassID id = ClassID(uint32_t(ClassID::Named) + offset++);
        store.emplace(class_name, id);
        return id;
    } else {
        return it->second;
    }
}

ClassID ClassDictionary::normalize(ClassID id) {
    if (id >= ClassID::Named) {
        return ClassID::Named;
    } else {
        return id;
    }
}

} // namespace mbgl