ComponentBuilder.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <memory>
  9. #include <react/core/ComponentDescriptor.h>
  10. #include <react/core/ShadowNode.h>
  11. #include <react/core/ShadowNodeFamilyFragment.h>
  12. #include <react/core/ShadowNodeFragment.h>
  13. #include <react/uimanager/ComponentDescriptorRegistry.h>
  14. #include <react/element/Element.h>
  15. #include <react/element/ElementFragment.h>
  16. namespace facebook {
  17. namespace react {
  18. /*
  19. * Build `ShadowNode` trees with a given given `Element` trees.
  20. */
  21. class ComponentBuilder final {
  22. public:
  23. ComponentBuilder(
  24. ComponentDescriptorRegistry::Shared const &componentDescriptorRegistry);
  25. /*
  26. * Copyable and movable.
  27. */
  28. ComponentBuilder(ComponentBuilder const &componentBuilder) = default;
  29. ComponentBuilder(ComponentBuilder &&componentBuilder) noexcept = default;
  30. ComponentBuilder &operator=(ComponentBuilder const &other) = default;
  31. ComponentBuilder &operator=(ComponentBuilder &&other) = default;
  32. /*
  33. * Builds a `ShadowNode` tree with given `Element` tree using stored
  34. * `ComponentDescriptorRegistry`.
  35. */
  36. template <typename ShadowNodeT>
  37. std::shared_ptr<ShadowNodeT> build(Element<ShadowNodeT> element) const {
  38. return std::static_pointer_cast<ShadowNodeT>(build(element.fragment_));
  39. }
  40. private:
  41. /*
  42. * Internal, type-erased version of `build`.
  43. */
  44. ShadowNode::Unshared build(ElementFragment const &elementFragment) const;
  45. ComponentDescriptorRegistry::Shared componentDescriptorRegistry_;
  46. };
  47. } // namespace react
  48. } // namespace facebook