From 52b37cac974eb150d49eee1f0b5ba3654f6d7716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= Date: Mon, 9 May 2016 11:09:06 +0200 Subject: Instrument Vector using UniquePtr --- src/drumkit.cc | 5 ----- src/drumkitloader.cc | 8 ++++---- src/drumkitparser.cc | 13 +++++++------ src/instrument.h | 3 ++- src/memchecker.cc | 6 +++--- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/drumkit.cc b/src/drumkit.cc index f25a6ea..1dadf04 100644 --- a/src/drumkit.cc +++ b/src/drumkit.cc @@ -39,11 +39,6 @@ DrumKit::~DrumKit() void DrumKit::clear() { - for(auto& instrument : instruments) - { - delete instrument; - } - instruments.clear(); channels.clear(); diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index 94daf79..f2ef61a 100644 --- a/src/drumkitloader.cc +++ b/src/drumkitloader.cc @@ -133,15 +133,15 @@ void DrumKitLoader::loadKit(DrumKit *kit) // Count total number of files that need loading: settings.number_of_files.store(0); - for(auto instr : kit->instruments) + for(auto& instr_ptr: kit->instruments) { - settings.number_of_files.fetch_add(instr->audiofiles.size()); + settings.number_of_files.fetch_add(instr_ptr->audiofiles.size()); } // Now actually queue them for loading: - for(auto instr : kit->instruments) + for(auto& instr_ptr: kit->instruments) { - for(auto audiofile : instr->audiofiles) + for(auto& audiofile: instr_ptr->audiofiles) { load_queue.push_back(audiofile); } diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc index 073d240..cd1c32d 100644 --- a/src/drumkitparser.cc +++ b/src/drumkitparser.cc @@ -30,6 +30,7 @@ #include #include +#include "cpp11fix.h" #include "instrumentparser.h" #include "path.h" #include "drumgizmo.h" @@ -184,14 +185,14 @@ void DrumKitParser::endTag(const std::string& name) { if(name == "instrument") { - Instrument* instrument = new Instrument(settings, rand); - instrument->setGroup(instr_group); - - InstrumentParser parser(*instrument); + auto ptr = std::make_unique(settings, rand); + ptr->setGroup(instr_group); + + InstrumentParser parser(*ptr); parser.parseFile(path + "/" + instr_file); // Transfer ownership to the DrumKit object. - kit.instruments.push_back(instrument); + kit.instruments.push_back(std::move(ptr)); // Assign kit channel numbers to instruments channels. std::vector::iterator ic = parser.channellist.begin(); @@ -216,7 +217,7 @@ void DrumKitParser::endTag(const std::string& name) if(c->num == NO_CHANNEL) { ERR(kitparser, "Missing channel '%s' in instrument '%s'\n", - c->name.c_str(), instrument->getName().c_str()); + c->name.c_str(), ptr->getName().c_str()); } else { diff --git a/src/instrument.h b/src/instrument.h index 621dddb..ee67b3f 100644 --- a/src/instrument.h +++ b/src/instrument.h @@ -28,6 +28,7 @@ #include #include +#include #include "rangemap.h" #include "powerlist.h" @@ -83,4 +84,4 @@ private: }; // typedef std::map< std::string, Instrument > Instruments; -typedef std::vector Instruments; +using Instruments = std::vector>; diff --git a/src/memchecker.cc b/src/memchecker.cc index 81c9da7..6d66965 100644 --- a/src/memchecker.cc +++ b/src/memchecker.cc @@ -73,12 +73,12 @@ uint64_t MemChecker::calcNeededMemory(const DrumKit& drumkit) const uint64_t needed_memory = 0; // Calculate memory usage of all instruments of drumkit. - for(auto instrument : drumkit.instruments) + for(auto& instr_ptr: drumkit.instruments) { - const auto& audiofiles = instrument->audiofiles; + const auto& audiofiles = instr_ptr->audiofiles; // Calculate memory usage of all audiofiles. - for(auto audiofile : audiofiles) + for(auto& audiofile: audiofiles) { needed_memory += calcBytesPerChannel(audiofile->filename); } -- cgit v1.2.3