aboutsummaryrefslogtreecommitdiff
path: root/src/backends/BackendRegistry.cpp
blob: 1360168b9f552ec8f7578d34cd1bea0b50290b19 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "BackendRegistry.hpp"
#include <armnn/Exceptions.hpp>

namespace armnn
{

BackendRegistry& BackendRegistry::Instance()
{
    static BackendRegistry instance;
    return instance;
}

void BackendRegistry::Register(const BackendId& id, FactoryFunction factory)
{
    if (m_BackendFactories.count(id) > 0)
    {
        throw InvalidArgumentException(std::string(id) + " already registered as backend",
                                       CHECK_LOCATION());
    }

    m_BackendFactories[id] = factory;
}

BackendRegistry::FactoryFunction BackendRegistry::GetFactory(const BackendId& id) const
{
    auto it = m_BackendFactories.find(id);
    if (it == m_BackendFactories.end())
    {
        throw InvalidArgumentException(std::string(id) + " has no backend factory registered",
                                       CHECK_LOCATION());
    }

    return it->second;
}

void BackendRegistry::Swap(BackendRegistry::FactoryStorage& other)
{
    BackendRegistry& instance = Instance();
    std::swap(instance.m_BackendFactories, other);
}

BackendIdSet BackendRegistry::GetBackendIds() const
{
    BackendIdSet result;
    for (const auto& it : m_BackendFactories)
    {
        result.insert(it.first);
    }
    return result;
}

} // namespace armnn