ComponentDescriptorProvider.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <react/core/ComponentDescriptor.h>
  9. #include <react/core/EventDispatcher.h>
  10. #include <react/utils/ContextContainer.h>
  11. namespace facebook {
  12. namespace react {
  13. /*
  14. * Callable signature that represents the signature of `ComponentDescriptor`
  15. * constructor. The callable returns a unique pointer conveniently represents an
  16. * abstract type and ownership of the newly created object.
  17. */
  18. using ComponentDescriptorConstructor = ComponentDescriptor::Unique(
  19. ComponentDescriptorParameters const &parameters);
  20. /*
  21. * Represents a unified way to construct an instance of a particular stored
  22. * `ComponentDescriptor` class. C++ does not allow to create pointers to
  23. * constructors, so we have to have such data structure to manipulate a
  24. * collection of classes.
  25. *
  26. * Note: The actual values of `handle` and `name` for some components depend on
  27. * `flavor`. The provider is valid if instantiated by `constructor` object with
  28. * given `flavor` exposes the same values of `handle` and `name`.
  29. */
  30. class ComponentDescriptorProvider final {
  31. public:
  32. ComponentHandle handle;
  33. ComponentName name;
  34. ComponentDescriptor::Flavor flavor;
  35. ComponentDescriptorConstructor *constructor;
  36. };
  37. /*
  38. * Creates a `ComponentDescriptor` for given `ComponentDescriptorParameters`.
  39. */
  40. template <typename ComponentDescriptorT>
  41. ComponentDescriptor::Unique concreteComponentDescriptorConstructor(
  42. ComponentDescriptorParameters const &parameters) {
  43. static_assert(
  44. std::is_base_of<ComponentDescriptor, ComponentDescriptorT>::value,
  45. "ComponentDescriptorT must be a descendant of ComponentDescriptor");
  46. return std::make_unique<ComponentDescriptorT const>(parameters);
  47. }
  48. /*
  49. * Creates a `ComponentDescriptorProvider` for given `ComponentDescriptor`
  50. * class.
  51. */
  52. template <typename ComponentDescriptorT>
  53. ComponentDescriptorProvider concreteComponentDescriptorProvider() {
  54. static_assert(
  55. std::is_base_of<ComponentDescriptor, ComponentDescriptorT>::value,
  56. "ComponentDescriptorT must be a descendant of ComponentDescriptor");
  57. return {ComponentDescriptorT::ConcreteShadowNode::Handle(),
  58. ComponentDescriptorT::ConcreteShadowNode::Name(),
  59. nullptr,
  60. &concreteComponentDescriptorConstructor<ComponentDescriptorT>};
  61. }
  62. } // namespace react
  63. } // namespace facebook