ShadowTreeRegistry.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 <better/map.h>
  9. #include <better/mutex.h>
  10. #include <react/core/ReactPrimitives.h>
  11. #include <react/mounting/ShadowTree.h>
  12. namespace facebook {
  13. namespace react {
  14. /*
  15. * Owning registry of `ShadowTree`s.
  16. */
  17. class ShadowTreeRegistry final {
  18. public:
  19. ShadowTreeRegistry() = default;
  20. ~ShadowTreeRegistry();
  21. /*
  22. * Adds a `ShadowTree` instance to the registry.
  23. * The ownership of the instance is also transferred to the registry.
  24. * Can be called from any thread.
  25. */
  26. void add(std::unique_ptr<ShadowTree> &&shadowTree) const;
  27. /*
  28. * Removes a `ShadowTree` instance with given `surfaceId` from the registry
  29. * and returns it as a result.
  30. * The ownership of the instance is also transferred to the caller.
  31. * Can be called from any thread.
  32. */
  33. std::unique_ptr<ShadowTree> remove(SurfaceId surfaceId) const;
  34. /*
  35. * Finds a `ShadowTree` instance with a given `surfaceId` in the registry and
  36. * synchronously calls the `callback` with a reference to the instance while
  37. * the mutex is being acquired.
  38. * Returns `true` if the registry has `ShadowTree` instance with corresponding
  39. * `surfaceId`, otherwise returns `false` without calling the `callback`.
  40. * Can be called from any thread.
  41. */
  42. bool visit(
  43. SurfaceId surfaceId,
  44. std::function<void(const ShadowTree &shadowTree)> callback) const;
  45. /*
  46. * Enumerates all stored shadow trees.
  47. * Set `stop` to `true` to interrupt the enumeration.
  48. * Can be called from any thread.
  49. */
  50. void enumerate(std::function<void(const ShadowTree &shadowTree, bool &stop)>
  51. callback) const;
  52. private:
  53. mutable better::shared_mutex mutex_;
  54. mutable better::map<SurfaceId, std::unique_ptr<ShadowTree>>
  55. registry_; // Protected by `mutex_`.
  56. };
  57. } // namespace react
  58. } // namespace facebook