From a0523da53d4d6ba595c6979e2d7150fed4be04de Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 29 Apr 2019 21:17:40 -0700 Subject: [PATCH] Fabric: Moving RNWrapManagedObject to `react/utils` module Summary: Apparently we can/should not have in RCTConversions because it creates unnecessary dependency to core iOS module. Reviewed By: mdvacca Differential Revision: D15055325 fbshipit-source-id: 507f5a40c03b5c261967de4504297d31ecd02783 --- React/Fabric/RCTConversions.h | 21 ----------- React/Fabric/RCTSurfacePresenter.mm | 6 ++-- ReactCommon/utils/ManagedObjectWrapper.h | 44 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 ReactCommon/utils/ManagedObjectWrapper.h diff --git a/React/Fabric/RCTConversions.h b/React/Fabric/RCTConversions.h index 0c5596889..12f2aa483 100644 --- a/React/Fabric/RCTConversions.h +++ b/React/Fabric/RCTConversions.h @@ -16,27 +16,6 @@ NS_ASSUME_NONNULL_BEGIN -/* - * `RNWrapManagedObject` and `RNUnwrapManagedObject` are wrapper functions that convert ARC-managed objects into - * `std::shared_ptr` and vice-versa. It's a very useful mechanism when we need to pass Objective-C objects through - * pure C++ code, pass blocks into C++ lambdas, and so on. - * - * The idea behind this mechanism is quite simple but tricky: When we instantiate a C++ shared pointer for a managed - * object, we practically call `CFRetain` for it once and then we represent this single retaining operation as a counter - * inside the shared pointer; when the counter became zero, we call `CFRelease` on the object. In this model, one bump - * of ARC-managed counter is represented as multiple bumps of C++ counter, so we can have multiple counters for the same - * object that form some kind of counters tree. - */ -inline std::shared_ptr RNWrapManagedObject(id object) -{ - return std::shared_ptr((__bridge_retained void *)object, CFRelease); -} - -inline id RNUnwrapManagedObject(std::shared_ptr const &object) -{ - return (__bridge id)object.get(); -} - inline NSString *RCTNSStringFromString(const std::string &string, const NSStringEncoding &encoding = NSUTF8StringEncoding) { return [NSString stringWithCString:string.c_str() encoding:encoding]; } diff --git a/React/Fabric/RCTSurfacePresenter.mm b/React/Fabric/RCTSurfacePresenter.mm index d37c0c9b7..3bc666b09 100644 --- a/React/Fabric/RCTSurfacePresenter.mm +++ b/React/Fabric/RCTSurfacePresenter.mm @@ -26,12 +26,14 @@ #import #import #import + #import #import #import #import #import #import +#import #import "MainRunLoopEventBeat.h" #import "RuntimeEventBeat.h" @@ -195,10 +197,10 @@ using namespace facebook::react; return _scheduler; } - auto componentRegistryFactory = [factory = RNWrapManagedObject(self.componentViewFactory)]( + auto componentRegistryFactory = [factory = wrapManagedObject(self.componentViewFactory)]( EventDispatcher::Shared const &eventDispatcher, ContextContainer::Shared const &contextContainer) { - return [(RCTComponentViewFactory *)RNUnwrapManagedObject(factory) + return [(RCTComponentViewFactory *)unwrapManagedObject(factory) createComponentDescriptorRegistryWithParameters:{eventDispatcher, contextContainer}]; }; diff --git a/ReactCommon/utils/ManagedObjectWrapper.h b/ReactCommon/utils/ManagedObjectWrapper.h new file mode 100644 index 000000000..3d2e052b1 --- /dev/null +++ b/ReactCommon/utils/ManagedObjectWrapper.h @@ -0,0 +1,44 @@ +/** + * 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 + +#ifdef __OBJC__ +#ifdef __cplusplus + +#include + +namespace facebook { +namespace react { + +/* + * `wrapManagedObject` and `unwrapManagedObject` are wrapper functions that + * convert ARC-managed objects into `std::shared_ptr` and vice-versa. It's + * a very useful mechanism when we need to pass Objective-C objects through pure + * C++ code, pass blocks into C++ lambdas, and so on. + * + * The idea behind this mechanism is quite simple but tricky: When we + * instantiate a C++ shared pointer for a managed object, we practically call + * `CFRetain` for it once and then we represent this single retaining operation + * as a counter inside the shared pointer; when the counter became zero, we call + * `CFRelease` on the object. In this model, one bump of ARC-managed counter is + * represented as multiple bumps of C++ counter, so we can have multiple + * counters for the same object that form some kind of counters tree. + */ +inline std::shared_ptr wrapManagedObject(id object) { + return std::shared_ptr((__bridge_retained void *)object, CFRelease); +} + +inline id unwrapManagedObject(std::shared_ptr const &object) { + return (__bridge id)object.get(); +} + +} // namespace react +} // namespace facebook + +#endif +#endif