summaryrefslogtreecommitdiff
path: root/deimos/core/api_registry.cpp
blob: 24ea2019717660aeeab34fff3b633574d1e2e649 (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
#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 RegisterLogApi(ApiRegistry*);

struct ApiEntry
{
    const ApiEntry* next{};
    IdName name;
    void* impl;

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

class ApiRegistryImpl: public ApiRegistry
{
    Allocator* m_allocator;
    const ApiEntry* m_head{};

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

    void Set(const IdName& name, void* impl) final
    {
        auto* 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();

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

    api_registry->Set(g_allocator_api);

    RegisterOsApi(api_registry);
    RegisterLogApi(api_registry);

    return api_registry;
}

} // namespace deimos