aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/ResourceManagerFactory.h
blob: 8e972f0564083eef3fe1b82f8436d7ae0a71017e (plain) (blame)
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
#pragma once

#include <Common/ErrorCodes.h>
#include <Common/Exception.h>

#include <IO/IResourceManager.h>

#include <boost/noncopyable.hpp>

#include <memory>
#include <mutex>
#include <unordered_map>

namespace DB
{

namespace ErrorCodes
{
    extern const int INVALID_SCHEDULER_NODE;
}

class ResourceManagerFactory : private boost::noncopyable
{
public:
    static ResourceManagerFactory & instance()
    {
        static ResourceManagerFactory ret;
        return ret;
    }

    ResourceManagerPtr get(const String & name)
    {
        std::lock_guard lock{mutex};
        if (auto iter = methods.find(name); iter != methods.end())
            return iter->second();
        throw Exception(ErrorCodes::INVALID_SCHEDULER_NODE, "Unknown scheduler node type: {}", name);
    }

    template <class TDerived>
    void registerMethod(const String & name)
    {
        std::lock_guard lock{mutex};
        methods[name] = [] ()
        {
            return std::make_shared<TDerived>();
        };
    }

private:
    std::mutex mutex;
    using Method = std::function<ResourceManagerPtr()>;
    std::unordered_map<String, Method> methods;
};

}