From d5bc81cabb4c8ed234053aaf1402406b7923c104 Mon Sep 17 00:00:00 2001 From: John Davies Date: Mon, 9 Apr 2018 10:09:57 +0100 Subject: [PATCH 1/2] add/remove events options to midi event list class --- include/MidiEventList.h | 6 +++--- src-library/MidiEventList.cpp | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/MidiEventList.h b/include/MidiEventList.h index d7a5c3cc..5b9573c1 100644 --- a/include/MidiEventList.h +++ b/include/MidiEventList.h @@ -45,6 +45,9 @@ class MidiEventList { int push_back (MidiEvent& event); int append (MidiEvent& event); + int remove (int index); + int add (int index, MidiEvent& event); + // careful when using these, intended for internal use in MidiFile class: void detach (void); int push_back_no_copy (MidiEvent* event); @@ -58,6 +61,3 @@ class MidiEventList { #endif /* _MIDIEVENTLIST_H_INCLUDED */ - - - diff --git a/src-library/MidiEventList.cpp b/src-library/MidiEventList.cpp index e6b4e6c9..125932aa 100644 --- a/src-library/MidiEventList.cpp +++ b/src-library/MidiEventList.cpp @@ -197,6 +197,38 @@ int MidiEventList::push_back(MidiEvent& event) { return append(event); } +////////////////////////////// +// +// MidiEventList::remove -- deletes a MidiEvent at the specified index. If +// the index is invalid then the request is ignored and -1 is returned. +// If the index is valid then the new size of the list is returned. +// +int MidiEventList::remove(int index) { + if ( ( index >=0 ) && ( index < (int)list.size() ) ) { + list.erase(list.begin() + index); + return (int)list.size()-1; + } else { + return -1; + } +} + + +////////////////////////////// +// +// MidiEventList::add -- inserts a MidiEvent at the specified index. If +// the index is invalid then the request is ignored and -1 is returned. +// If the index is valid then the new size of the list is returned. +// +int MidiEventList::add(int index, MidiEvent& event) { + if ( ( index >=0 ) && ( index < (int)list.size() ) ) { + MidiEvent* ptr = new MidiEvent(event); + list.insert(list.begin()+index, ptr); + return (int)list.size()-1; + } else { + return -1; + } +} + ////////////////////////////// // @@ -404,5 +436,3 @@ MidiEventList& MidiEventList::operator=(MidiEventList other) { list.swap(other.list); return *this; } - - From 68cc0bf913638adc9975b4d4a88610dc68f464e7 Mon Sep 17 00:00:00 2001 From: John Davies Date: Sun, 22 Apr 2018 10:15:03 +0100 Subject: [PATCH 2/2] Fix to memory leak, "add" changed to "insert" --- include/MidiEventList.h | 2 +- src-library/MidiEventList.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/MidiEventList.h b/include/MidiEventList.h index 5b9573c1..b84cb48f 100644 --- a/include/MidiEventList.h +++ b/include/MidiEventList.h @@ -46,7 +46,7 @@ class MidiEventList { int append (MidiEvent& event); int remove (int index); - int add (int index, MidiEvent& event); + int insert (int index, MidiEvent& event); // careful when using these, intended for internal use in MidiFile class: void detach (void); diff --git a/src-library/MidiEventList.cpp b/src-library/MidiEventList.cpp index 125932aa..b5b30478 100644 --- a/src-library/MidiEventList.cpp +++ b/src-library/MidiEventList.cpp @@ -205,6 +205,7 @@ int MidiEventList::push_back(MidiEvent& event) { // int MidiEventList::remove(int index) { if ( ( index >=0 ) && ( index < (int)list.size() ) ) { + delete list[ index ]; list.erase(list.begin() + index); return (int)list.size()-1; } else { @@ -219,7 +220,7 @@ int MidiEventList::remove(int index) { // the index is invalid then the request is ignored and -1 is returned. // If the index is valid then the new size of the list is returned. // -int MidiEventList::add(int index, MidiEvent& event) { +int MidiEventList::insert(int index, MidiEvent& event) { if ( ( index >=0 ) && ( index < (int)list.size() ) ) { MidiEvent* ptr = new MidiEvent(event); list.insert(list.begin()+index, ptr);