Skip to content

Commit

Permalink
Issue #57: Added mechanism to allow MIKMIDIEvent subclasses to specif…
Browse files Browse the repository at this point in the history
…y the minimum internal data size they require.
  • Loading branch information
armadsen committed Feb 25, 2015
1 parent 5bd27ff commit c021588
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Source/MIKMIDIEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ + (void)registerSubclass:(Class)subclass;
[registeredMIKMIDIEventSubclasses addObject:subclass];
}

+ (BOOL)isMutable { return NO; }

+ (BOOL)supportsMIKMIDIEventType:(MIKMIDIEventType)type { return [[self supportedMIDIEventTypes] containsObject:@(type)]; }
+ (NSArray *)supportedMIDIEventTypes { return @[]; }
+ (Class)immutableCounterpartClass; { return [MIKMIDIEvent class]; }
+ (Class)mutableCounterpartClass; { return [MIKMutableMIDIEvent class]; }
+ (BOOL)isMutable { return NO; }
+ (size_t)minimumDataSize { return 0; }

+ (instancetype)midiEventWithTimeStamp:(MusicTimeStamp)timeStamp eventType:(MusicEventType)eventType data:(NSData *)data
{
Expand Down Expand Up @@ -65,7 +65,13 @@ - (instancetype)initWithTimeStamp:(MusicTimeStamp)timeStamp midiEventType:(MIKMI
if (self) {
_timeStamp = timeStamp;
_eventType = eventType;
self.internalData = [NSMutableData dataWithData:data];

NSMutableData *internalData = [NSMutableData dataWithData:data];
size_t minSize = [[self class] minimumDataSize];
if ([internalData length] < minSize) {
[internalData increaseLengthBy:(minSize - [internalData length])];
}
_internalData = internalData;
}
return self;
}
Expand Down
8 changes: 8 additions & 0 deletions Source/MIKMIDIEvent_SubclassMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
*/
+ (BOOL)isMutable;

/**
* Subclasses of MIKMIDIEvent can override this to specify a minum internal data length
* necessary to hold their contents. For example, MIKMIDINoteEvent returns sizeof(MIDINoteMessage).
*
* @return A size_t value indicating the minimum size in bytes required to hold the receiver's data.
*/
+ (size_t)minimumDataSize;

/**
* This is the property used internally by MIKMIDIEvent to store the raw data for
* a MIDI packet. It is essentially the mutable backing store for MIKMIDIEvent's
Expand Down

0 comments on commit c021588

Please sign in to comment.