diff --git a/src/darwin/Framework/CHIP/MTRDefines_Internal.h b/src/darwin/Framework/CHIP/MTRDefines_Internal.h index 14683f34c553f9..5a450ece8ec7f3 100644 --- a/src/darwin/Framework/CHIP/MTRDefines_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDefines_Internal.h @@ -152,3 +152,17 @@ typedef struct {} variable_hidden_by_mtr_hide; #endif #define MTR_YES_NO(x) ((x) ? @"YES" : @"NO") + +#ifdef DEBUG +#define _MTR_ABSTRACT_METHOD_IMPL(message, ...) \ + do { \ + MTR_LOG_ERROR(message, __VA_ARGS__); \ + @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@message, __VA_ARGS__] userInfo:nil]; \ + } while (0) +#else // DEBUG +#define _MTR_ABSTRACT_METHOD_IMPL(message, ...) \ + MTR_LOG_ERROR(message, __VA_ARGS__) +#endif // DEBUG + +#define MTR_ABSTRACT_METHOD() \ + _MTR_ABSTRACT_METHOD_IMPL("%@ or some ancestor must implement %@", self.class, NSStringFromSelector(_cmd)) diff --git a/src/darwin/Framework/CHIP/MTRDevice.mm b/src/darwin/Framework/CHIP/MTRDevice.mm index 46fc8faa40ee1d..cabcb448ec348e 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.mm +++ b/src/darwin/Framework/CHIP/MTRDevice.mm @@ -1248,12 +1248,7 @@ - (NSUInteger)unitTestNonnullDelegateCount attributeID:(NSNumber *)attributeID params:(MTRReadParams * _Nullable)params { -#define MTRDeviceErrorStr "MTRDevice readAttributeWithEndpointID:clusterID:attributeID:params: must be handled by subclasses" - MTR_LOG_ERROR(MTRDeviceErrorStr); -#ifdef DEBUG - NSAssert(NO, @MTRDeviceErrorStr); -#endif // DEBUG -#undef MTRDeviceErrorStr + MTR_ABSTRACT_METHOD(); return nil; } @@ -1264,22 +1259,12 @@ - (void)writeAttributeWithEndpointID:(NSNumber *)endpointID expectedValueInterval:(NSNumber *)expectedValueInterval timedWriteTimeout:(NSNumber * _Nullable)timeout { -#define MTRDeviceErrorStr "MTRDevice writeAttributeWithEndpointID:clusterID:attributeID:value:expectedValueInterval:timedWriteTimeout: must be handled by subclasses" - MTR_LOG_ERROR(MTRDeviceErrorStr); -#ifdef DEBUG - NSAssert(NO, @MTRDeviceErrorStr); -#endif // DEBUG -#undef MTRDeviceErrorStr + MTR_ABSTRACT_METHOD(); } - (NSArray *> *)readAttributePaths:(NSArray *)attributePaths { -#define MTRDeviceErrorStr "MTRDevice readAttributePaths: must be handled by subclasses" - MTR_LOG_ERROR(MTRDeviceErrorStr); -#ifdef DEBUG - NSAssert(NO, @MTRDeviceErrorStr); -#endif // DEBUG -#undef MTRDeviceErrorStr + MTR_ABSTRACT_METHOD(); return [NSArray array]; } @@ -1362,12 +1347,7 @@ - (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID queue:(dispatch_queue_t)queue completion:(MTRDeviceResponseHandler)completion { -#define MTRDeviceErrorStr "MTRDevice _invokeCommandWithEndpointID: must be handled by subclasses" - MTR_LOG_ERROR(MTRDeviceErrorStr); -#ifdef DEBUG - NSAssert(NO, @MTRDeviceErrorStr); -#endif // DEBUG -#undef MTRDeviceErrorStr + MTR_ABSTRACT_METHOD(); } - (void)_invokeKnownCommandWithEndpointID:(NSNumber *)endpointID @@ -1476,12 +1456,7 @@ - (NSDictionary *)_dataValueWithoutDataVersion:(NSDictionary *)attributeValue - (NSArray *> *)getAllAttributesReport { -#define MTRDeviceErrorStr "MTRDevice getAllAttributesReport must be handled by subclasses that support it" - MTR_LOG_ERROR(MTRDeviceErrorStr); -#ifdef DEBUG - NSAssert(NO, @MTRDeviceErrorStr); -#endif // DEBUG -#undef MTRDeviceErrorStr + MTR_ABSTRACT_METHOD(); return nil; }