Skip to content

Commit 3663d42

Browse files
Clean up the "subclasses must implement this" machinery in MTRDevice. (#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.
1 parent 5dc91f3 commit 3663d42

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

src/darwin/Framework/CHIP/MTRDefines_Internal.h

+14
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,17 @@ typedef struct {} variable_hidden_by_mtr_hide;
152152
#endif
153153

154154
#define MTR_YES_NO(x) ((x) ? @"YES" : @"NO")
155+
156+
#ifdef DEBUG
157+
#define _MTR_ABSTRACT_METHOD_IMPL(message, ...) \
158+
do { \
159+
MTR_LOG_ERROR(message, __VA_ARGS__); \
160+
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@message, __VA_ARGS__] userInfo:nil]; \
161+
} while (0)
162+
#else // DEBUG
163+
#define _MTR_ABSTRACT_METHOD_IMPL(message, ...) \
164+
MTR_LOG_ERROR(message, __VA_ARGS__)
165+
#endif // DEBUG
166+
167+
#define MTR_ABSTRACT_METHOD() \
168+
_MTR_ABSTRACT_METHOD_IMPL("%@ or some ancestor must implement %@", self.class, NSStringFromSelector(_cmd))

src/darwin/Framework/CHIP/MTRDevice.mm

+5-30
Original file line numberDiff line numberDiff line change
@@ -1248,12 +1248,7 @@ - (NSUInteger)unitTestNonnullDelegateCount
12481248
attributeID:(NSNumber *)attributeID
12491249
params:(MTRReadParams * _Nullable)params
12501250
{
1251-
#define MTRDeviceErrorStr "MTRDevice readAttributeWithEndpointID:clusterID:attributeID:params: must be handled by subclasses"
1252-
MTR_LOG_ERROR(MTRDeviceErrorStr);
1253-
#ifdef DEBUG
1254-
NSAssert(NO, @MTRDeviceErrorStr);
1255-
#endif // DEBUG
1256-
#undef MTRDeviceErrorStr
1251+
MTR_ABSTRACT_METHOD();
12571252
return nil;
12581253
}
12591254

@@ -1264,22 +1259,12 @@ - (void)writeAttributeWithEndpointID:(NSNumber *)endpointID
12641259
expectedValueInterval:(NSNumber *)expectedValueInterval
12651260
timedWriteTimeout:(NSNumber * _Nullable)timeout
12661261
{
1267-
#define MTRDeviceErrorStr "MTRDevice writeAttributeWithEndpointID:clusterID:attributeID:value:expectedValueInterval:timedWriteTimeout: must be handled by subclasses"
1268-
MTR_LOG_ERROR(MTRDeviceErrorStr);
1269-
#ifdef DEBUG
1270-
NSAssert(NO, @MTRDeviceErrorStr);
1271-
#endif // DEBUG
1272-
#undef MTRDeviceErrorStr
1262+
MTR_ABSTRACT_METHOD();
12731263
}
12741264

12751265
- (NSArray<NSDictionary<NSString *, id> *> *)readAttributePaths:(NSArray<MTRAttributeRequestPath *> *)attributePaths
12761266
{
1277-
#define MTRDeviceErrorStr "MTRDevice readAttributePaths: must be handled by subclasses"
1278-
MTR_LOG_ERROR(MTRDeviceErrorStr);
1279-
#ifdef DEBUG
1280-
NSAssert(NO, @MTRDeviceErrorStr);
1281-
#endif // DEBUG
1282-
#undef MTRDeviceErrorStr
1267+
MTR_ABSTRACT_METHOD();
12831268
return [NSArray array];
12841269
}
12851270

@@ -1362,12 +1347,7 @@ - (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID
13621347
queue:(dispatch_queue_t)queue
13631348
completion:(MTRDeviceResponseHandler)completion
13641349
{
1365-
#define MTRDeviceErrorStr "MTRDevice _invokeCommandWithEndpointID: must be handled by subclasses"
1366-
MTR_LOG_ERROR(MTRDeviceErrorStr);
1367-
#ifdef DEBUG
1368-
NSAssert(NO, @MTRDeviceErrorStr);
1369-
#endif // DEBUG
1370-
#undef MTRDeviceErrorStr
1350+
MTR_ABSTRACT_METHOD();
13711351
}
13721352

13731353
- (void)_invokeKnownCommandWithEndpointID:(NSNumber *)endpointID
@@ -1476,12 +1456,7 @@ - (NSDictionary *)_dataValueWithoutDataVersion:(NSDictionary *)attributeValue
14761456

14771457
- (NSArray<NSDictionary<NSString *, id> *> *)getAllAttributesReport
14781458
{
1479-
#define MTRDeviceErrorStr "MTRDevice getAllAttributesReport must be handled by subclasses that support it"
1480-
MTR_LOG_ERROR(MTRDeviceErrorStr);
1481-
#ifdef DEBUG
1482-
NSAssert(NO, @MTRDeviceErrorStr);
1483-
#endif // DEBUG
1484-
#undef MTRDeviceErrorStr
1459+
MTR_ABSTRACT_METHOD();
14851460
return nil;
14861461
}
14871462

0 commit comments

Comments
 (0)