blob: e2c526d293af0bb22dd5ffa040dd8ab6c4744235 [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#pragma once
6
7#include <armnn/Types.hpp>
David Beck32cbb0c2018-10-09 15:46:08 +01008#include <functional>
9#include <memory>
10#include <unordered_map>
11
12namespace armnn
13{
14
15class IBackend;
16
17class BackendRegistry
18{
19public:
20 using FactoryFunction = std::function<IBackendUniquePtr()>;
21
22 static BackendRegistry& Instance();
David Beck9df2d952018-10-10 15:11:44 +010023
24 void Register(const BackendId& id, FactoryFunction factory);
25 FactoryFunction GetFactory(const BackendId& id) const;
David Beck32cbb0c2018-10-09 15:46:08 +010026
27 struct Helper
28 {
David Beck9df2d952018-10-10 15:11:44 +010029 Helper(const BackendId& id, FactoryFunction factory)
David Beck32cbb0c2018-10-09 15:46:08 +010030 {
David Beck9df2d952018-10-10 15:11:44 +010031 BackendRegistry::Instance().Register(id, factory);
David Beck32cbb0c2018-10-09 15:46:08 +010032 }
33 };
34
35 size_t Size() const { return m_BackendFactories.size(); }
David Beck9df2d952018-10-10 15:11:44 +010036 BackendIdSet GetBackendIds() const;
David Beck32cbb0c2018-10-09 15:46:08 +010037
38protected:
David Beck9df2d952018-10-10 15:11:44 +010039 using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>;
David Beck32cbb0c2018-10-09 15:46:08 +010040
41 // For testing only
42 static void Swap(FactoryStorage& other);
43 BackendRegistry() {}
44 ~BackendRegistry() {}
45
46private:
47 BackendRegistry(const BackendRegistry&) = delete;
48 BackendRegistry& operator=(const BackendRegistry&) = delete;
49
50 FactoryStorage m_BackendFactories;
51};
52
53} // namespace armnn