blob: 80ab01ce1bdc15325abb444da9b65f185874d4ea [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"
David Beck111b5d92018-11-12 14:59:37 +00007#include <armnn/Exceptions.hpp>
David Beck32cbb0c2018-10-09 15:46:08 +01008
9namespace armnn
10{
11
David Beck3e9e1152018-10-17 14:17:50 +010012BackendRegistry& BackendRegistryInstance()
David Beck32cbb0c2018-10-09 15:46:08 +010013{
14 static BackendRegistry instance;
15 return instance;
16}
17
David Beck111b5d92018-11-12 14:59:37 +000018void BackendRegistry::Register(const BackendId& id, BackendRegistry::FactoryFunction factory)
19{
20 if (m_Factories.count(id) > 0)
21 {
22 throw InvalidArgumentException(
23 std::string(id) + " already registered as IBackend factory",
24 CHECK_LOCATION());
25 }
26
27 m_Factories[id] = factory;
28}
29
30bool BackendRegistry::IsBackendRegistered(const BackendId& id) const
31{
32 return (m_Factories.find(id) != m_Factories.end());
33}
34
35BackendRegistry::FactoryFunction BackendRegistry::GetFactory(const BackendId& id) const
36{
37 auto it = m_Factories.find(id);
38 if (it == m_Factories.end())
39 {
40 throw InvalidArgumentException(
41 std::string(id) + " has no IBackend factory registered",
42 CHECK_LOCATION());
43 }
44
45 return it->second;
46}
47
48size_t BackendRegistry::Size() const
49{
50 return m_Factories.size();
51}
52
53BackendIdSet BackendRegistry::GetBackendIds() const
54{
55 BackendIdSet result;
56 for (const auto& it : m_Factories)
57 {
58 result.insert(it.first);
59 }
60 return result;
61}
62
63std::string BackendRegistry::GetBackendIdsAsString() const
64{
65 static const std::string delimitator = ", ";
66
67 std::stringstream output;
68 for (auto& backendId : GetBackendIds())
69 {
70 if (output.tellp() != std::streampos(0))
71 {
72 output << delimitator;
73 }
74 output << backendId;
75 }
76
77 return output.str();
78}
79
80void BackendRegistry::Swap(BackendRegistry& instance, BackendRegistry::FactoryStorage& other)
81{
82 std::swap(instance.m_Factories, other);
83}
84
85
David Beck9df2d952018-10-10 15:11:44 +010086} // namespace armnn