summaryrefslogtreecommitdiff
path: root/src/mbgl/util/thread_local.hpp
blob: cf5579a2ba9225a38ecf52bf93c03614081a046e (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
#pragma once

#include <type_traits>

namespace mbgl {
namespace util {
namespace impl {

class ThreadLocalBase {
protected:
    ThreadLocalBase();
    ~ThreadLocalBase();

    void* get();
    void set(void*);

private:
    std::aligned_storage_t<sizeof(void*), alignof(void*)> storage;
};

} // namespace impl

template <class T>
class ThreadLocal : public impl::ThreadLocalBase {
public:
    ThreadLocal() = default;

    ThreadLocal(T* val) {
        set(val);
    }

    T* get() {
        return reinterpret_cast<T*>(impl::ThreadLocalBase::get());
    }

    void set(T* ptr) {
        impl::ThreadLocalBase::set(ptr);
    }
};

} // namespace util
} // namespace mbgl