Skip to content

Instantly share code, notes, and snippets.

@orj
Forked from aufflick/XSynthesizeCoders.h
Last active December 16, 2015 10:09
Show Gist options
  • Select an option

  • Save orj/5418430 to your computer and use it in GitHub Desktop.

Select an option

Save orj/5418430 to your computer and use it in GitHub Desktop.

Revisions

  1. orj revised this gist Apr 19, 2013. 2 changed files with 5 additions and 5 deletions.
    8 changes: 4 additions & 4 deletions XSynthesizeCoders.h
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    -(void) encodeWithCoder: (NSCoder*) coder
    {
    #define X(type, ivar) [coder encode##type:ivar forKey:@#ivar];
    #define XX(ivar, encode, decode) do { [coder encodeObject:(encode) forKey:@#ivar]; } while (0);
    #define BOX(type, ivar) [coder encodeObject:([NSValue valueWith##type:ivar]) forKey:@#ivar];
    XEncodeIvars
    #undef XX
    #undef BOX
    #undef X
    }

    -(id) initWithCoder: (NSCoder*) decoder
    {
    #define X(type, ivar) ivar = [decoder decode##type##ForKey:@#ivar];
    #define XX(ivar, encode, decode) do { id x = [decoder decodeObjectForKey:@#ivar]; decode; } while (0);
    #define BOX(type, ivar) ivar = [[decoder decodeObjectForKey:@#ivar] type##Value];
    XEncodeIvars
    #undef XX
    #undef BOX
    #undef X
    return self;
    }
    2 changes: 1 addition & 1 deletion ZFoo.m
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ @implementation Foo
    X(Integer, _anotherInteger) \
    X(Object, _anObject) \
    X(Object, _anotherObject) \
    XX(_location, [NSValue valueWithMKCoordinate:_location], [x MKCoordinateValue])
    BOX(MKCoordinate, _location))

    #include "XSynthesizeCoders.h"

  2. @aufflick aufflick revised this gist Apr 19, 2013. 3 changed files with 8 additions and 2 deletions.
    6 changes: 5 additions & 1 deletion XSynthesizeCoders.h
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,18 @@
    -(void) encodeWithCoder: (NSCoder*) coder
    {
    #define X(type, ivar) [coder encode##type:ivar forKey:@#ivar];
    #define XX(ivar, encode, decode) do { [coder encodeObject:(encode) forKey:@#ivar]; } while (0);
    XEncodeIvars
    #undef XX
    #undef X
    }

    -(id) initWithCoder: (NSCoder*) decoder
    {
    #define X(type, ivar) ivar = [decoder decode##type##ForKey:@#ivar];
    #define XX(ivar, encode, decode) do { id x = [decoder decodeObjectForKey:@#ivar]; decode; } while (0);
    XEncodeIvars
    #undef XX
    #undef X
    return self;
    }
    }
    1 change: 1 addition & 0 deletions ZFoo.h
    Original file line number Diff line number Diff line change
    @@ -6,5 +6,6 @@
    @property NSInteger anotherInteger;
    @property NSObject * anObject;
    @property NSObject * anotherObject;
    @property CLLocationCoordinate2D location;

    @end
    3 changes: 2 additions & 1 deletion ZFoo.m
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,8 @@ @implementation Foo
    X(Integer, _anInteger) \
    X(Integer, _anotherInteger) \
    X(Object, _anObject) \
    X(Object, _anotherObject)
    X(Object, _anotherObject) \
    XX(_location, [NSValue valueWithMKCoordinate:_location], [x MKCoordinateValue])

    #include "XSynthesizeCoders.h"

  3. @aufflick aufflick revised this gist Apr 19, 2013. 3 changed files with 14 additions and 75 deletions.
    67 changes: 5 additions & 62 deletions XSynthesizeCoders.h
    Original file line number Diff line number Diff line change
    @@ -1,71 +1,14 @@
    -(void) encodeWithCoder: (NSCoder*) coder
    {
    #ifdef XEncodeBools
    #define X(ivar) [coder encodeBool:ivar forKey:@"" #ivar];
    XEncodeBools
    #define X(type, ivar) [coder encode##type:ivar forKey:@#ivar];
    XEncodeIvars
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif

    #ifdef XEncodeDoubles
    #define X(ivar) [coder encodeDouble:ivar forKey:@"" #ivar];
    XEncodeDoubles
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif

    #ifdef XEncodeIntegers
    #define X(ivar) [coder encodeInteger:ivar forKey:@"" #ivar];
    XEncodeIntegers
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif

    #ifdef XEncodeObjects
    #define X(ivar) [coder encodeObject:ivar forKey:@"" #ivar];
    XEncodeObjects
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif
    }

    -(id) initWithCoder: (NSCoder*) decoder
    {
    #ifdef XEncodeBools
    #define X(ivar) [decoder decodeBoolForKey:@"" #ivar];
    XEncodeBools
    #undef X
    #endif

    #ifdef XEncodeIntegers
    #define X(ivar) [decoder decodeIntegerForKey:@"" #ivar];
    XEncodeIntegers
    #define X(type, ivar) ivar = [decoder decode##type##ForKey:@#ivar];
    XEncodeIvars
    #undef X
    #endif

    #ifdef XEncodeDoubles
    #define X(ivar) [decoder decodeDoubleForKey:@"" #ivar];
    XEncodeDoubles
    #undef X
    #endif

    #ifdef XEncodeObjects
    #define X(ivar) [decoder decodeObjectForKey:@"" #ivar];
    XEncodeObjects
    #undef X
    #endif

    return self;
    }

    #ifndef XSynthesizeCodersFoundSources
    #warning XSynthesizeCoders found none of XEncodeBools, XEncodeIntegers, XEncodeDoubles or XEncodeObjects
    #endif
    }
    4 changes: 2 additions & 2 deletions ZFoo.h
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    @property BOOL anotherBool;
    @property NSInteger anInteger;
    @property NSInteger anotherInteger;
    @property NSObject * anObject;
    @property NSObject * anotherObject;
    @property NSObject * anObject;
    @property NSObject * anotherObject;

    @end
    18 changes: 7 additions & 11 deletions ZFoo.m
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,12 @@
    @implementation Foo

    #define XEncodeBools \
    X(_aBool) \
    X(_anotherBool)

    #define XEncodeIntegers \
    X(_anInteger) \
    X(_anotherInteger)

    #define XEncodeObjects \
    X(_anObject) \
    X(_anotherObject)
    #define XEncodeIvars \
    X(Bool, _aBool) \
    X(Bool, _anotherBool) \
    X(Integer, _anInteger) \
    X(Integer, _anotherInteger) \
    X(Object, _anObject) \
    X(Object, _anotherObject)

    #include "XSynthesizeCoders.h"

  4. @aufflick aufflick revised this gist Apr 19, 2013. 1 changed file with 45 additions and 1 deletion.
    46 changes: 45 additions & 1 deletion XSynthesizeCoders.h
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,71 @@
    -(void) encodeWithCoder: (NSCoder*) coder
    {
    #ifdef XEncodeBools
    #define X(ivar) [coder encodeBool:ivar forKey:@"" #ivar];
    XEncodeBools
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif

    #ifdef XEncodeDoubles
    #define X(ivar) [coder encodeDouble:ivar forKey:@"" #ivar];
    XEncodeDoubles
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif

    #ifdef XEncodeIntegers
    #define X(ivar) [coder encodeInteger:ivar forKey:@"" #ivar];
    XEncodeIntegers
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif

    #ifdef XEncodeObjects
    #define X(ivar) [coder encodeObject:ivar forKey:@"" #ivar];
    XEncodeObjects
    #undef X
    #ifndef XSynthesizeCodersFoundSources
    #define XSynthesizeCodersFoundSources
    #endif
    #endif
    }

    -(id) initWithCoder: (NSCoder*) decoder
    {
    #ifdef XEncodeBools
    #define X(ivar) [decoder decodeBoolForKey:@"" #ivar];
    XEncodeBools
    #undef X
    #endif

    #ifdef XEncodeIntegers
    #define X(ivar) [decoder decodeIntegerForKey:@"" #ivar];
    XEncodeIntegers
    #undef X
    #endif

    #ifdef XEncodeDoubles
    #define X(ivar) [decoder decodeDoubleForKey:@"" #ivar];
    XEncodeDoubles
    #undef X
    #endif

    #ifdef XEncodeObjects
    #define X(ivar) [decoder decodeObjectForKey:@"" #ivar];
    XEncodeObjects
    #undef X
    #endif

    return self;
    }
    }

    #ifndef XSynthesizeCodersFoundSources
    #warning XSynthesizeCoders found none of XEncodeBools, XEncodeIntegers, XEncodeDoubles or XEncodeObjects
    #endif
  5. @aufflick aufflick revised this gist Apr 19, 2013. 2 changed files with 0 additions and 0 deletions.
    Binary file renamed Foo.h → ZFoo.h
    Binary file not shown.
    File renamed without changes.
  6. @aufflick aufflick created this gist Apr 19, 2013.
    Binary file added Foo.h
    Binary file not shown.
    17 changes: 17 additions & 0 deletions Foo.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    @implementation Foo

    #define XEncodeBools \
    X(_aBool) \
    X(_anotherBool)

    #define XEncodeIntegers \
    X(_anInteger) \
    X(_anotherInteger)

    #define XEncodeObjects \
    X(_anObject) \
    X(_anotherObject)

    #include "XSynthesizeCoders.h"

    @end
    27 changes: 27 additions & 0 deletions XSynthesizeCoders.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    -(void) encodeWithCoder: (NSCoder*) coder
    {
    #define X(ivar) [coder encodeBool:ivar forKey:@"" #ivar];
    XEncodeBools
    #undef X
    #define X(ivar) [coder encodeInteger:ivar forKey:@"" #ivar];
    XEncodeIntegers
    #undef X
    #define X(ivar) [coder encodeObject:ivar forKey:@"" #ivar];
    XEncodeObjects
    #undef X
    }

    -(id) initWithCoder: (NSCoder*) decoder
    {
    #define X(ivar) [decoder decodeBoolForKey:@"" #ivar];
    XEncodeBools
    #undef X
    #define X(ivar) [decoder decodeIntegerForKey:@"" #ivar];
    XEncodeIntegers
    #undef X
    #define X(ivar) [decoder decodeObjectForKey:@"" #ivar];
    XEncodeObjects
    #undef X

    return self;
    }