From eaac3b57ee5798693c09c6959e636f321f72ccb5 Mon Sep 17 00:00:00 2001 From: Jonathan Ballerano Date: Thu, 15 Jun 2017 18:55:22 -0700 Subject: [PATCH] Add a type cast to RCT_ENUM_CONVERTER for C++ compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: C++ doesn't provide an implicit cast to an enum value from the enum's backing type. When a `.mm` file calls `RCT_ENUM_CONVERTER`, we end up with the following compiler error: > Error: cannot initialize return object of type `` with an rvalue of type `NSInteger` Since `RCT_ENUM_CONVERTER` is a macro, this error will appear whenever we try to expand the macro in a C++ context. The project compiles and runs as expected when this additional cast is added 😃 Closes https://github.com/facebook/react-native/pull/14408 Reviewed By: javache Differential Revision: D5215584 Pulled By: jballer fbshipit-source-id: 7f4464afd7cd9dc9a014f646278bae20731f08ba --- React/Base/RCTConvert.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/React/Base/RCTConvert.h b/React/Base/RCTConvert.h index dbd4d1f7b..a799d7286 100644 --- a/React/Base/RCTConvert.h +++ b/React/Base/RCTConvert.h @@ -207,6 +207,17 @@ RCT_CUSTOM_CONVERTER(type, name, [json getter]) #define RCT_NUMBER_CONVERTER(type, getter) \ RCT_CUSTOM_CONVERTER(type, type, [RCT_DEBUG ? [self NSNumber:json] : json getter]) +/** + * When using RCT_ENUM_CONVERTER in ObjC, the compiler is OK with us returning + * the underlying NSInteger/NSUInteger. In ObjC++, this is a type mismatch and + * we need to explicitly cast the return value to expected enum return type. + */ +#ifdef __cplusplus +#define _RCT_CAST(type, expr) static_cast(expr) +#else +#define _RCT_CAST(type, expr) expr +#endif + /** * This macro is used for creating converters for enum types. */ @@ -218,7 +229,7 @@ RCT_CUSTOM_CONVERTER(type, type, [RCT_DEBUG ? [self NSNumber:json] : json getter dispatch_once(&onceToken, ^{ \ mapping = values; \ }); \ - return [RCTConvertEnumValue(#type, mapping, @(default), json) getter]; \ + return _RCT_CAST(type, [RCTConvertEnumValue(#type, mapping, @(default), json) getter]); \ } /** @@ -233,7 +244,7 @@ RCT_CUSTOM_CONVERTER(type, type, [RCT_DEBUG ? [self NSNumber:json] : json getter dispatch_once(&onceToken, ^{ \ mapping = values; \ }); \ - return [RCTConvertMultiEnumValue(#type, mapping, @(default), json) getter]; \ + return _RCT_CAST(type, [RCTConvertMultiEnumValue(#type, mapping, @(default), json) getter]); \ } /**