ShadowTreeRegistry.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include "ShadowTreeRegistry.h"
  8. namespace facebook {
  9. namespace react {
  10. ShadowTreeRegistry::~ShadowTreeRegistry() {
  11. assert(
  12. registry_.size() == 0 &&
  13. "Deallocation of non-empty `ShadowTreeRegistry`.");
  14. }
  15. void ShadowTreeRegistry::add(std::unique_ptr<ShadowTree> &&shadowTree) const {
  16. std::unique_lock<better::shared_mutex> lock(mutex_);
  17. registry_.emplace(shadowTree->getSurfaceId(), std::move(shadowTree));
  18. }
  19. std::unique_ptr<ShadowTree> ShadowTreeRegistry::remove(
  20. SurfaceId surfaceId) const {
  21. std::unique_lock<better::shared_mutex> lock(mutex_);
  22. auto iterator = registry_.find(surfaceId);
  23. auto shadowTree = std::unique_ptr<ShadowTree>(iterator->second.release());
  24. registry_.erase(iterator);
  25. return shadowTree;
  26. }
  27. bool ShadowTreeRegistry::visit(
  28. SurfaceId surfaceId,
  29. std::function<void(const ShadowTree &shadowTree)> callback) const {
  30. std::shared_lock<better::shared_mutex> lock(mutex_);
  31. auto iterator = registry_.find(surfaceId);
  32. if (iterator == registry_.end()) {
  33. return false;
  34. }
  35. callback(*iterator->second);
  36. return true;
  37. }
  38. void ShadowTreeRegistry::enumerate(
  39. std::function<void(const ShadowTree &shadowTree, bool &stop)> callback)
  40. const {
  41. std::shared_lock<better::shared_mutex> lock(mutex_);
  42. bool stop = false;
  43. for (auto const &pair : registry_) {
  44. callback(*pair.second, stop);
  45. if (stop) {
  46. break;
  47. }
  48. }
  49. }
  50. } // namespace react
  51. } // namespace facebook