Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] use macros to reduce repetitiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Dec 19, 2016
1 parent e11ec31 commit e7fe451
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 182 deletions.
203 changes: 21 additions & 182 deletions platform/darwin/src/MGLFeature.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,9 @@ @implementation MGLPointFeature
@synthesize identifier;
@synthesize attributes;

- (instancetype)initWithCoder:(NSCoder *)decoder {
if ([super initWithCoder:decoder]) {
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]];
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"];
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:identifier forKey:@"identifier"];
[coder encodeObject:attributes forKey:@"attributes"];
}

- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[MGLPointFeature class]]) return NO;

MGLPointFeature *otherPointFeature = other;
return [super isEqual:other] && [self geoJSONObject] == [otherPointFeature geoJSONObject];
}

- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
MGLFeatureInitCoding();
MGLFeatureEncode();
MGLFeatureIsEqual(MGLPointFeature);

- (id)attributeForKey:(NSString *)key {
return self.attributes[key];
Expand All @@ -75,32 +52,9 @@ @implementation MGLPolylineFeature
@synthesize identifier;
@synthesize attributes;

- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]];
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"];
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:identifier forKey:@"identifier"];
[coder encodeObject:attributes forKey:@"attributes"];
}

- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[MGLPolylineFeature class]]) return NO;

MGLPolylineFeature *otherPointFeature = other;
return [super isEqual:other] && [self geoJSONObject] == [otherPointFeature geoJSONObject];
}

- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
MGLFeatureInitCoding();
MGLFeatureEncode();
MGLFeatureIsEqual(MGLPolylineFeature);

- (id)attributeForKey:(NSString *)key {
return self.attributes[key];
Expand All @@ -124,32 +78,9 @@ @implementation MGLPolygonFeature
@synthesize identifier;
@synthesize attributes;

- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]];
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"];
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:identifier forKey:@"identifier"];
[coder encodeObject:attributes forKey:@"attributes"];
}

- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[MGLPolygonFeature class]]) return NO;

MGLPolygonFeature *otherPolygonFeature = other;
return [super isEqual:other] && [self geoJSONObject] == [otherPolygonFeature geoJSONObject];
}

- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
MGLFeatureInitCoding();
MGLFeatureEncode();
MGLFeatureIsEqual(MGLPolygonFeature);

- (id)attributeForKey:(NSString *)key {
return self.attributes[key];
Expand All @@ -173,32 +104,9 @@ @implementation MGLPointCollectionFeature
@synthesize identifier;
@synthesize attributes;

- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]];
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"];
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:identifier forKey:@"identifier"];
[coder encodeObject:attributes forKey:@"attributes"];
}

- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[MGLPointCollectionFeature class]]) return NO;

MGLPointCollectionFeature *otherPointFeature = other;
return [super isEqual:other] && [self geoJSONObject] == [otherPointFeature geoJSONObject];
}

- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
MGLFeatureInitCoding();
MGLFeatureEncode();
MGLFeatureIsEqual(MGLPointCollectionFeature);

- (id)attributeForKey:(NSString *)key {
return self.attributes[key];
Expand All @@ -222,32 +130,9 @@ @implementation MGLMultiPolylineFeature
@synthesize identifier;
@synthesize attributes;

- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]];
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"];
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:identifier forKey:@"identifier"];
[coder encodeObject:attributes forKey:@"attributes"];
}

- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[MGLMultiPolylineFeature class]]) return NO;

MGLMultiPolylineFeature *otherMultiPolylineFeature = other;
return [super isEqual:other] && [self geoJSONObject] == [otherMultiPolylineFeature geoJSONObject];
}

- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
MGLFeatureInitCoding();
MGLFeatureEncode();
MGLFeatureIsEqual(MGLMultiPolylineFeature);

- (id)attributeForKey:(NSString *)key {
return self.attributes[key];
Expand All @@ -271,32 +156,9 @@ @implementation MGLMultiPolygonFeature
@synthesize identifier;
@synthesize attributes;

- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]];
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"];
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:identifier forKey:@"identifier"];
[coder encodeObject:attributes forKey:@"attributes"];
}

- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[MGLMultiPolygonFeature class]]) return NO;

MGLMultiPolygonFeature *otherMultiPolygonFeature = other;
return [super isEqual:other] && [self geoJSONObject] == [otherMultiPolygonFeature geoJSONObject];
}

- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
MGLFeatureInitCoding();
MGLFeatureEncode();
MGLFeatureIsEqual(MGLMultiPolygonFeature);

- (id)attributeForKey:(NSString *)key {
return self.attributes[key];
Expand Down Expand Up @@ -326,32 +188,9 @@ + (instancetype)shapeCollectionWithShapes:(NSArray *)shapes {
return [super shapeCollectionWithShapes:shapes];
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]];
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"];
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:identifier forKey:@"identifier"];
[coder encodeObject:attributes forKey:@"attributes"];
}

- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (![other isKindOfClass:[MGLShapeCollectionFeature class]]) return NO;

MGLShapeCollectionFeature *otherShapeCollectionFeature = other;
return [super isEqual:other] && [self geoJSONObject] == [otherShapeCollectionFeature geoJSONObject];
}

- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
MGLFeatureInitCoding();
MGLFeatureEncode();
MGLFeatureIsEqual(MGLShapeCollectionFeature);

- (id)attributeForKey:(NSString *)key {
return self.attributes[key];
Expand Down
28 changes: 28 additions & 0 deletions platform/darwin/src/MGLFeature_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,31 @@ mbgl::Feature mbglFeature(mbgl::Feature feature, id identifier, NSDictionary *at
NS_DICTIONARY_OF(NSString *, id) *NSDictionaryFeatureForGeometry(NSDictionary *geometry, NSDictionary *attributes, id identifier);

NS_ASSUME_NONNULL_END

#define MGLFeatureInitCoding() \
- (instancetype)initWithCoder:(NSCoder *)decoder { \
if (self = [super initWithCoder:decoder]) { \
NSSet<Class> *identifierClasses = [NSSet setWithArray:@[[NSString class], [NSNumber class]]]; \
identifier = [decoder decodeObjectOfClasses:identifierClasses forKey:@"identifier"]; \
attributes = [decoder decodeObjectOfClass:[NSDictionary class] forKey:@"attributes"]; \
} \
return self; \
}

#define MGLFeatureEncode() \
- (void)encodeWithCoder:(NSCoder *)coder { \
[super encodeWithCoder:coder]; \
[coder encodeObject:identifier forKey:@"identifier"]; \
[coder encodeObject:attributes forKey:@"attributes"]; \
}

#define MGLFeatureIsEqual(klass) \
- (BOOL)isEqual:(id)other { \
if (other == self) return YES; \
if (![other isKindOfClass:[self class]]) return NO; \
klass *otherFeature = other; \
return [super isEqual:other] && [self geoJSONObject] == [otherFeature geoJSONObject]; \
} \
- (NSUInteger)hash { \
return [super hash] + [[self geoJSONDictionary] hash]; \
}

0 comments on commit e7fe451

Please sign in to comment.