From cf9874dfa5b528c0d6184aa5bb04b908272f2dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= Date: Tue, 29 Mar 2016 17:37:49 +0200 Subject: API Refactoring for class Sample --- src/sample.cc | 47 +++++++++++++++++++++++++---------------------- src/sample.h | 28 ++++++++++++++-------------- 2 files changed, 39 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/sample.cc b/src/sample.cc index 27382af..66fe3c5 100644 --- a/src/sample.cc +++ b/src/sample.cc @@ -31,42 +31,45 @@ #include -Sample::Sample(std::string name, float power) +Sample::Sample(const std::string& name, float power) + : name{name} + , power{power} + , audiofiles{} { - this->name = name; - this->power = power; } Sample::~Sample() { } -void Sample::addAudioFile(Channel *c, AudioFile *a) +void Sample::addAudioFile(Channel* c, AudioFile* a) { - audiofiles[c] = a; + audiofiles[c] = a; } -AudioFile *Sample::getAudioFile(Channel *c) +AudioFile* Sample::getAudioFile(Channel* c) { - /* - if(audiofiles.find(c) == audiofiles.end()) return NULL; - return audiofiles[c]; - */ + /* + if(audiofiles.find(c) == audiofiles.end()) return nullptr; + return audiofiles[c]; + */ - AudioFiles::iterator i = audiofiles.begin(); - while(i != audiofiles.end()) { - Channel *ch = i->first; - if(c->num == ch->num) return i->second; - i++; - } + // todo: std::find_if ?? + for (auto& pair: audiofiles) + { + if (pair.first->num == c->num) + { + return pair.second; + } + } - return NULL; + return nullptr; } #ifdef TEST_SAMPLE -//deps: channel.cc audiofile.cc -//cflags: $(SNDFILE_CFLAGS) -//libs: $(SNDFILE_LIBS) +// deps: channel.cc audiofile.cc +// cflags: $(SNDFILE_CFLAGS) +// libs: $(SNDFILE_LIBS) #include "test.h" TEST_BEGIN; @@ -78,8 +81,8 @@ AudioFile a("test"); s.addAudioFile(&c, &a); TEST_EQUAL(s.getAudioFile(&c), &a, "?"); -TEST_EQUAL(s.getAudioFile(&c2), NULL, "?"); +TEST_EQUAL(s.getAudioFile(&c2), nullptr, "?"); TEST_END; -#endif/*TEST_SAMPLE*/ +#endif /*TEST_SAMPLE*/ diff --git a/src/sample.h b/src/sample.h index 26c7be2..5b4f3b5 100644 --- a/src/sample.h +++ b/src/sample.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_SAMPLE_H__ -#define __DRUMGIZMO_SAMPLE_H__ +#pragma once #include #include @@ -33,24 +32,26 @@ #include "channel.h" #include "audiofile.h" -typedef std::map< Channel*, AudioFile* > AudioFiles; +typedef std::map AudioFiles; class InstrumentParser; -class Sample { - friend class InstrumentParser; - friend class PowerList; +class Sample +{ + friend class InstrumentParser; + friend class PowerList; + public: - Sample(std::string name, float power); - ~Sample(); + Sample(const std::string& name, float power); + ~Sample(); - AudioFile *getAudioFile(InstrumentChannel *c); + AudioFile* getAudioFile(InstrumentChannel* c); private: - void addAudioFile(InstrumentChannel *c, AudioFile *a); + void addAudioFile(InstrumentChannel* c, AudioFile* a); - std::string name; - float power; - AudioFiles audiofiles; + std::string name; + float power; + AudioFiles audiofiles; }; /* @@ -62,4 +63,3 @@ private: * * */ -#endif/*__DRUMGIZMO_SAMPLE_H__*/ -- cgit v1.2.3