David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include <armnn/BackendId.hpp> |
| 8 | #include <armnn/Exceptions.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 10 | #include <functional> |
| 11 | #include <memory> |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 12 | #include <sstream> |
| 13 | #include <string> |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 14 | #include <unordered_map> |
| 15 | |
| 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | template <typename RegisteredType> |
| 20 | struct RegisteredTypeName |
| 21 | { |
| 22 | static const char * Name() { return "UNKNOWN"; } |
| 23 | }; |
| 24 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 25 | template <typename RegisteredType, typename PointerType> |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 26 | class RegistryCommon |
| 27 | { |
| 28 | public: |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 29 | using FactoryFunction = std::function<PointerType()>; |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 30 | |
| 31 | void Register(const BackendId& id, FactoryFunction factory) |
| 32 | { |
| 33 | if (m_Factories.count(id) > 0) |
| 34 | { |
| 35 | throw InvalidArgumentException( |
| 36 | std::string(id) + " already registered as " + RegisteredTypeName<RegisteredType>::Name() + " factory", |
| 37 | CHECK_LOCATION()); |
| 38 | } |
| 39 | |
| 40 | m_Factories[id] = factory; |
| 41 | } |
| 42 | |
| 43 | FactoryFunction GetFactory(const BackendId& id) const |
| 44 | { |
| 45 | auto it = m_Factories.find(id); |
| 46 | if (it == m_Factories.end()) |
| 47 | { |
| 48 | throw InvalidArgumentException( |
| 49 | std::string(id) + " has no " + RegisteredTypeName<RegisteredType>::Name() + " factory registered", |
| 50 | CHECK_LOCATION()); |
| 51 | } |
| 52 | |
| 53 | return it->second; |
| 54 | } |
| 55 | |
| 56 | size_t Size() const |
| 57 | { |
| 58 | return m_Factories.size(); |
| 59 | } |
| 60 | |
| 61 | BackendIdSet GetBackendIds() const |
| 62 | { |
| 63 | BackendIdSet result; |
| 64 | for (const auto& it : m_Factories) |
| 65 | { |
| 66 | result.insert(it.first); |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 71 | std::string GetBackendIdsAsString() const |
| 72 | { |
| 73 | static const std::string delimitator = ", "; |
| 74 | |
| 75 | std::stringstream output; |
| 76 | for (auto& backendId : GetBackendIds()) |
| 77 | { |
| 78 | if (output.tellp() != std::streampos(0)) |
| 79 | { |
| 80 | output << delimitator; |
| 81 | } |
| 82 | output << backendId; |
| 83 | } |
| 84 | |
| 85 | return output.str(); |
| 86 | } |
| 87 | |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 88 | RegistryCommon() {} |
| 89 | virtual ~RegistryCommon() {} |
| 90 | |
| 91 | protected: |
| 92 | using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>; |
| 93 | |
| 94 | // For testing only |
| 95 | static void Swap(RegistryCommon& instance, FactoryStorage& other) |
| 96 | { |
| 97 | std::swap(instance.m_Factories, other); |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | RegistryCommon(const RegistryCommon&) = delete; |
| 102 | RegistryCommon& operator=(const RegistryCommon&) = delete; |
| 103 | |
| 104 | FactoryStorage m_Factories; |
| 105 | }; |
| 106 | |
| 107 | template <typename RegistryType> |
| 108 | struct StaticRegistryInitializer |
| 109 | { |
| 110 | using FactoryFunction = typename RegistryType::FactoryFunction; |
| 111 | |
| 112 | StaticRegistryInitializer(RegistryType& instance, |
| 113 | const BackendId& id, |
| 114 | FactoryFunction factory) |
| 115 | { |
| 116 | instance.Register(id, factory); |
| 117 | } |
| 118 | }; |
| 119 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 120 | } // namespace armnn |