nRF Connect SDK API 3.3.99
Loading...
Searching...
No Matches
bridge_util.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#pragma once
8
9#include <lib/support/CHIPMem.h>
10
11#include <cstdint>
12#include <functional>
13#include <map>
14
15namespace Nrf
16{
17
18/*
19 DeviceFactory template container allows to instantiate a map which
20 binds supported Matter device type identifiers (uint16_t) with corresponding
21 creation method (e.g. constructor invocation). DeviceFactory can
22 only be constructed by passing a user-defined initialized list with
23 <uint16_t, ConcreteDeviceCreator> pairs.
24 Then, Create() method can be used to obtain an instance of demanded
25 device type with all passed arguments forwarded to the underlying
26 ConcreteDeviceCreator.
27*/
28template <typename T, typename DeviceType, typename... Args> class DeviceFactory {
29public:
30 using ConcreteDeviceCreator = std::function<T *(Args...)>;
31 using CreationMap = std::map<DeviceType, ConcreteDeviceCreator>;
32
33 DeviceFactory(std::initializer_list<std::pair<DeviceType, ConcreteDeviceCreator>> init)
34 {
35 for (auto &pair : init) {
36 mCreationMap.insert(pair);
37 }
38 }
39
40 DeviceFactory() = delete;
41 DeviceFactory(const DeviceFactory &) = delete;
45 ~DeviceFactory() = default;
46
47 T *Create(DeviceType deviceType, Args... params)
48 {
49 if (mCreationMap.find(deviceType) == mCreationMap.end()) {
50 return nullptr;
51 }
52 return mCreationMap[deviceType](params...);
53 }
54
55private:
56 CreationMap mCreationMap;
57};
58
59} /* namespace Nrf */
DeviceFactory()=delete
DeviceFactory(DeviceFactory &&)=delete
DeviceFactory & operator=(const DeviceFactory &)=delete
DeviceFactory(std::initializer_list< std::pair< DeviceType, ConcreteDeviceCreator > > init)
Definition bridge_util.h:33
std::function< T *(Args...)> ConcreteDeviceCreator
Definition bridge_util.h:30
~DeviceFactory()=default
std::map< DeviceType, ConcreteDeviceCreator > CreationMap
Definition bridge_util.h:31
DeviceFactory & operator=(DeviceFactory &&)=delete
DeviceFactory(const DeviceFactory &)=delete
T * Create(DeviceType deviceType, Args... params)
Definition bridge_util.h:47
Definition bridge_util.h:28
Definition ble_connectivity_manager.h:21