summaryrefslogtreecommitdiff
path: root/deimos/core/api_registry.cpp
blob: 9d0c93f39233073b6ce94b8a38215ec9845372d0 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "deimos/core/api_registry.h"
#include "deimos/core/allocator.h"

static deimos::AllocatorApi* g_allocator_api;

namespace deimos
{

AllocatorApi* BootstrapAllocatorApi();
void RegisterOsApi(ApiRegistry*);
void RegisterTempAllocatorApi(ApiRegistry*);
void RegisterLogApi(ApiRegistry*);
void InitializeStatus(ApiRegistry*);

struct ApiEntry
{
    gsl::owner<const ApiEntry*> next{};
    IdName name;
    void* impl;

    ApiEntry(const IdName& name_, void* impl_) :
        name{name_}, impl{impl_}
    {}
};

class ApiRegistryImpl: public ApiRegistry
{
    Allocator* m_allocator;
    gsl::owner<const ApiEntry*> m_head{};

public:
    explicit ApiRegistryImpl(Allocator* allocator) :
        m_allocator{allocator}
    {}

    void Set(const IdName& name, void* impl) final
    {
        gsl::owner<ApiEntry*> entry = m_allocator->New<ApiEntry>(name, impl);
        entry->next = std::exchange(m_head, entry);
    }

    void* Get(const IdName& name) final
    {
        for (const ApiEntry* it = m_head; it != nullptr; it = it->next)
        {
            if (it->name == name) { return it->impl; }
        }
        return nullptr;
    }
};

ApiRegistry* InitializeGlobalApiRegistry()
{
    g_allocator_api = BootstrapAllocatorApi();

    gsl::owner<Allocator*> allocator = g_allocator_api->CreateChild(g_allocator_api->system, "API Registry");
    gsl::owner<ApiRegistry*> api_registry = allocator->New<ApiRegistryImpl>(allocator);

    api_registry->Set(g_allocator_api);

    InitializeStatus(api_registry);
    RegisterOsApi(api_registry);
    RegisterTempAllocatorApi(api_registry);
    RegisterLogApi(api_registry);

    return api_registry;
}

} // namespace deimos