summaryrefslogtreecommitdiff
path: root/src/engine/registry/DataRegistry.h
blob: 2b3b9c9acfff4bb09d45103754ec89a1dd58f492 (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
#pragma once
#include <string>
#include <unordered_map>
#include <memory>
#include <cstdint>

template<typename TObject>
class DataRegistry {
public:
    // Add object by copy or move; overwrites if exists
    void Add(const std::string& resourceName, TObject&& object) {
        mMap[resourceName] = std::make_unique<TObject>(std::forward<TObject>(object));
    }

    void Add(const std::string& resourceName, const TObject& object) {
        mMap[resourceName] = std::make_unique<TObject>(object);
    }

    // Get pointer to object (nullptr if not found)
    TObject* Get(const std::string& resourceName) {
        auto it = mMap.find(resourceName);
        return it != mMap.end() ? it->second.get() : nullptr;
    }

    const TObject* Get(const std::string& resourceName) const {
        auto it = mMap.find(resourceName);
        return it != mMap.end() ? it->second.get() : nullptr;
    }

    bool Remove(const std::string& resourceName) {
        return mMap.erase(resourceName) > 0;
    }

    size_t Size() const {
        return mMap.size();
    }

    void Clear() {
        mMap.clear();
    }

private:
    std::unordered_map<std::string, std::unique_ptr<TObject>> mMap;
};