Skip to content

Commit

Permalink
Use NSString literal for MTRDeviceTypeData
Browse files Browse the repository at this point in the history
  • Loading branch information
ksperling-apple committed Dec 23, 2024
1 parent 5d02faf commit c0cce65
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 93 deletions.
40 changes: 24 additions & 16 deletions src/darwin/Framework/CHIP/MTRDeviceType.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,32 @@
using namespace chip;

MTR_DIRECT_MEMBERS
@implementation MTRDeviceType
@implementation MTRDeviceType {
const MTRDeviceTypeData * _meta;
}

- (instancetype)initWithDeviceTypeID:(NSNumber *)id name:(NSString *)name isUtility:(BOOL)isUtility
- (instancetype)initWithDeviceTypeData:(const MTRDeviceTypeData *)metaData
{
self = [super init];
_id = id;
_name = name;
_isUtility = isUtility;
_meta = metaData;
return self;
}

- (NSNumber *)id
{
return @(_meta->id);
}

- (NSString *)name
{
return const_cast<NSString *>(_meta->name);
}

- (BOOL)isUtility
{
return _meta->deviceClass != MTRDeviceTypeClass::Simple;
}

+ (nullable MTRDeviceType *)deviceTypeForID:(NSNumber *)deviceTypeID
{
if (!CanCastTo<DeviceTypeId>(deviceTypeID.unsignedLongLongValue)) {
Expand All @@ -50,14 +65,7 @@ + (nullable MTRDeviceType *)deviceTypeForID:(NSNumber *)deviceTypeID
return nil;
}

auto * name = [[NSString alloc] initWithBytesNoCopy:(void *) deviceTypeData->name
length:strlen(deviceTypeData->name)
encoding:NSUTF8StringEncoding
freeWhenDone:NO];

return [[MTRDeviceType alloc] initWithDeviceTypeID:deviceTypeID
name:name
isUtility:(deviceTypeData->deviceClass != MTRDeviceTypeClass::Simple)];
return [[MTRDeviceType alloc] initWithDeviceTypeData:deviceTypeData];
}

- (id)copyWithZone:(NSZone *)zone
Expand All @@ -67,20 +75,20 @@ - (id)copyWithZone:(NSZone *)zone

- (NSUInteger)hash
{
return _id.hash;
return _meta->id;
}

- (BOOL)isEqual:(id)object
{
VerifyOrReturnValue([object class] == [self class], NO);
MTRDeviceType * other = object;
return [_id isEqual:other->_id];
return _meta->id == other->_meta->id;
}

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@ 0x%" PRIx32 " (%@)>",
self.class, _id.unsignedIntValue, _name];
self.class, _meta->id, _meta->name];
}

@end
8 changes: 3 additions & 5 deletions src/darwin/Framework/CHIP/MTRDeviceTypeMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@

NS_ASSUME_NONNULL_BEGIN

enum class MTRDeviceTypeClass
{
enum class MTRDeviceTypeClass {
Utility,
Simple,
Node, // Might not be a real class, but we have it for Root Node for now.
};

struct MTRDeviceTypeData
{
struct MTRDeviceTypeData {
chip::DeviceTypeId id;
MTRDeviceTypeClass deviceClass;
const char * name;
NSString * name;
};

// Returns null for unknown device types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace {
constexpr MTRDeviceTypeData knownDeviceTypes[] = {
{{#zcl_device_types}}
{{#if class}}
{ {{asHex code 8}}, MTRDeviceTypeClass::{{class}}, "{{caption}}" },
{ {{asHex code 8}}, MTRDeviceTypeClass::{{class}}, @"{{caption}}" },
{{/if}}
{{/zcl_device_types}}
};
Expand Down
144 changes: 73 additions & 71 deletions src/darwin/Framework/CHIP/zap-generated/MTRDeviceTypeMetadata.mm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0cce65

Please sign in to comment.