David Beck | 32cbb0c | 2018-10-09 15:46:08 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "BackendRegistry.hpp" |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 7 | #include <armnn/Exceptions.hpp> |
David Beck | 32cbb0c | 2018-10-09 15:46:08 +0100 | [diff] [blame] | 8 | |
| 9 | namespace armnn |
| 10 | { |
| 11 | |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 12 | BackendRegistry& BackendRegistryInstance() |
David Beck | 32cbb0c | 2018-10-09 15:46:08 +0100 | [diff] [blame] | 13 | { |
| 14 | static BackendRegistry instance; |
| 15 | return instance; |
| 16 | } |
| 17 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 18 | void BackendRegistry::Register(const BackendId& id, BackendRegistry::FactoryFunction factory) |
| 19 | { |
Matteo Martincigh | 4e73b42 | 2019-08-08 13:46:32 +0100 | [diff] [blame] | 20 | if (m_Factories.find(id) != m_Factories.end()) |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 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 | |
| 30 | bool BackendRegistry::IsBackendRegistered(const BackendId& id) const |
| 31 | { |
| 32 | return (m_Factories.find(id) != m_Factories.end()); |
| 33 | } |
| 34 | |
| 35 | BackendRegistry::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 | |
| 48 | size_t BackendRegistry::Size() const |
| 49 | { |
| 50 | return m_Factories.size(); |
| 51 | } |
| 52 | |
| 53 | BackendIdSet 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 | |
| 63 | std::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 | |
| 80 | void BackendRegistry::Swap(BackendRegistry& instance, BackendRegistry::FactoryStorage& other) |
| 81 | { |
| 82 | std::swap(instance.m_Factories, other); |
| 83 | } |
| 84 | |
| 85 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 86 | } // namespace armnn |