Skip to content

Commit

Permalink
Clean up the "subclasses must implement this" machinery in MTRDevice. (
Browse files Browse the repository at this point in the history
…#35657)

* Clean up the "subclasses must implement this" machinery in MTRDevice.

We had a lot of copy/paste that we can factor out.

* Address review comments.
  • Loading branch information
bzbarsky-apple authored Sep 19, 2024
1 parent 5dc91f3 commit 3663d42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
14 changes: 14 additions & 0 deletions src/darwin/Framework/CHIP/MTRDefines_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
35 changes: 5 additions & 30 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<NSDictionary<NSString *, id> *> *)readAttributePaths:(NSArray<MTRAttributeRequestPath *> *)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];
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1476,12 +1456,7 @@ - (NSDictionary *)_dataValueWithoutDataVersion:(NSDictionary *)attributeValue

- (NSArray<NSDictionary<NSString *, id> *> *)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;
}

Expand Down

0 comments on commit 3663d42

Please sign in to comment.