-
Notifications
You must be signed in to change notification settings - Fork 24k
/
ComponentDescriptorProvider.h
81 lines (70 loc) · 2.55 KB
/
ComponentDescriptorProvider.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/core/ComponentDescriptor.h>
#include <react/core/EventDispatcher.h>
#include <react/utils/ContextContainer.h>
namespace facebook {
namespace react {
/*
* Represents a collection of arguments that sufficient to construct a
* `ComponentDescriptor`.
*/
class ComponentDescriptorParameters {
public:
EventDispatcher::Shared eventDispatcher;
ContextContainer::Shared contextContainer;
};
/*
* Callable signature that represents the signature of `ComponentDescriptor`
* constructor. The callable returns a unique pointer conveniently represents an
* abstract type and ownership of the newly created object.
*/
using ComponentDescriptorConstructor = ComponentDescriptor::Unique(
EventDispatcher::Shared const &eventDispatcher,
ContextContainer::Shared const &contextContainer);
/*
* Represents a unified way to construct an instance of a particular stored
* `ComponentDescriptor` class. C++ does not allow to create pointers to
* constructors, so we have to have such data structure to manipulate a
* collection of classes.
*/
class ComponentDescriptorProvider final {
public:
ComponentHandle handle;
ComponentName name;
ComponentDescriptorConstructor *constructor;
};
/*
* Creates a `ComponentDescriptorConstructor` for given `ComponentDescriptor`
* class.
*/
template <typename ComponentDescriptorT>
ComponentDescriptor::Unique concreteComponentDescriptorConstructor(
EventDispatcher::Shared const &eventDispatcher,
ContextContainer::Shared const &contextContainer) {
static_assert(
std::is_base_of<ComponentDescriptor, ComponentDescriptorT>::value,
"ComponentDescriptorT must be a descendant of ComponentDescriptor");
return std::make_unique<ComponentDescriptorT const>(
eventDispatcher, contextContainer);
}
/*
* Creates a `ComponentDescriptorProvider` for given `ComponentDescriptor`
* class.
*/
template <typename ComponentDescriptorT>
ComponentDescriptorProvider concreteComponentDescriptorProvider() {
static_assert(
std::is_base_of<ComponentDescriptor, ComponentDescriptorT>::value,
"ComponentDescriptorT must be a descendant of ComponentDescriptor");
return {ComponentDescriptorT::ConcreteShadowNode::Handle(),
ComponentDescriptorT::ConcreteShadowNode::Name(),
&concreteComponentDescriptorConstructor<ComponentDescriptorT>};
}
} // namespace react
} // namespace facebook