From c02158839891bd19218dddb66d0d74353ef4afb7 Mon Sep 17 00:00:00 2001 From: Andrew Madsen Date: Wed, 25 Feb 2015 10:51:04 -0700 Subject: [PATCH] Issue #57: Added mechanism to allow MIKMIDIEvent subclasses to specify the minimum internal data size they require. --- Source/MIKMIDIEvent.m | 12 +++++++++--- Source/MIKMIDIEvent_SubclassMethods.h | 8 ++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Source/MIKMIDIEvent.m b/Source/MIKMIDIEvent.m index 26bb70b2..f20ca1d5 100644 --- a/Source/MIKMIDIEvent.m +++ b/Source/MIKMIDIEvent.m @@ -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 { @@ -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; } diff --git a/Source/MIKMIDIEvent_SubclassMethods.h b/Source/MIKMIDIEvent_SubclassMethods.h index 81e7896b..9fc62047 100644 --- a/Source/MIKMIDIEvent_SubclassMethods.h +++ b/Source/MIKMIDIEvent_SubclassMethods.h @@ -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