blob: 1360168b9f552ec8f7578d34cd1bea0b50290b19 [file] [log] [blame]
David Beck32cbb0c2018-10-09 15:46:08 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "BackendRegistry.hpp"
7#include <armnn/Exceptions.hpp>
8
9namespace armnn
10{
11
12BackendRegistry& BackendRegistry::Instance()
13{
14 static BackendRegistry instance;
15 return instance;
16}
17
David Beck9df2d952018-10-10 15:11:44 +010018void BackendRegistry::Register(const BackendId& id, FactoryFunction factory)
David Beck32cbb0c2018-10-09 15:46:08 +010019{
David Beck9df2d952018-10-10 15:11:44 +010020 if (m_BackendFactories.count(id) > 0)
David Beck32cbb0c2018-10-09 15:46:08 +010021 {
David Beck3cc9a622018-10-12 10:38:31 +010022 throw InvalidArgumentException(std::string(id) + " already registered as backend",
23 CHECK_LOCATION());
David Beck32cbb0c2018-10-09 15:46:08 +010024 }
25
David Beck9df2d952018-10-10 15:11:44 +010026 m_BackendFactories[id] = factory;
David Beck32cbb0c2018-10-09 15:46:08 +010027}
28
David Beck9df2d952018-10-10 15:11:44 +010029BackendRegistry::FactoryFunction BackendRegistry::GetFactory(const BackendId& id) const
David Beck32cbb0c2018-10-09 15:46:08 +010030{
David Beck9df2d952018-10-10 15:11:44 +010031 auto it = m_BackendFactories.find(id);
David Beck32cbb0c2018-10-09 15:46:08 +010032 if (it == m_BackendFactories.end())
33 {
David Beck3cc9a622018-10-12 10:38:31 +010034 throw InvalidArgumentException(std::string(id) + " has no backend factory registered",
35 CHECK_LOCATION());
David Beck32cbb0c2018-10-09 15:46:08 +010036 }
37
38 return it->second;
39}
40
41void BackendRegistry::Swap(BackendRegistry::FactoryStorage& other)
42{
43 BackendRegistry& instance = Instance();
44 std::swap(instance.m_BackendFactories, other);
45}
46
David Beck9df2d952018-10-10 15:11:44 +010047BackendIdSet BackendRegistry::GetBackendIds() const
48{
49 BackendIdSet result;
50 for (const auto& it : m_BackendFactories)
51 {
52 result.insert(it.first);
53 }
54 return result;
David Beck32cbb0c2018-10-09 15:46:08 +010055}
David Beck9df2d952018-10-10 15:11:44 +010056
57} // namespace armnn