Use std::unique_ptr<> throughout the audio system
authorIngo Ruhnke <grumbel@gmail.com>
Tue, 12 Aug 2014 23:11:48 +0000 (01:11 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Tue, 12 Aug 2014 23:11:48 +0000 (01:11 +0200)
20 files changed:
src/audio/dummy_sound_source.cpp
src/audio/dummy_sound_source.hpp
src/audio/openal_sound_source.cpp
src/audio/openal_sound_source.hpp
src/audio/sound_file.cpp
src/audio/sound_file.hpp
src/audio/sound_manager.cpp
src/audio/sound_manager.hpp
src/audio/sound_source.hpp
src/audio/stream_sound_source.cpp
src/audio/stream_sound_source.hpp
src/badguy/bomb.cpp
src/badguy/dart.cpp
src/badguy/flame.cpp
src/badguy/goldbomb.cpp
src/badguy/haywire.cpp
src/badguy/treewillowisp.cpp
src/badguy/willowisp.cpp
src/object/ambient_sound.cpp
src/object/ambient_sound.hpp

index 178baa7..97aec96 100644 (file)
 //  You should have received a copy of the GNU General Public License
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include "audio/dummy_sound_source.hpp"
+
+#include <memory>
+
 #include "audio/sound_source.hpp"
 
 class DummySoundSource : public SoundSource
@@ -70,11 +74,15 @@ public:
 
 private:
   bool is_playing;
+
+private:
+  DummySoundSource(const DummySoundSource&) = delete;
+  DummySoundSource& operator=(const DummySoundSource&) = delete;
 };
 
-SoundSource* create_dummy_sound_source()
+std::unique_ptr<SoundSource> create_dummy_sound_source()
 {
-  return new DummySoundSource();
+  return std::unique_ptr<SoundSource>(new DummySoundSource);
 }
 
 /* EOF */
index 5162f1d..12dd5de 100644 (file)
 #ifndef HEADER_SUPERTUX_AUDIO_DUMMY_SOUND_SOURCE_HPP
 #define HEADER_SUPERTUX_AUDIO_DUMMY_SOUND_SOURCE_HPP
 
+#include <memory>
+
 class SoundSource;
 
-SoundSource* create_dummy_sound_source();
+std::unique_ptr<SoundSource> create_dummy_sound_source();
 
 #endif
 
index 52cf090..b30b445 100644 (file)
@@ -15,6 +15,7 @@
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "audio/openal_sound_source.hpp"
+
 #include "audio/sound_manager.hpp"
 
 OpenALSoundSource::OpenALSoundSource() :
index 42e1279..8725d59 100644 (file)
@@ -45,6 +45,10 @@ protected:
   friend class SoundManager;
 
   ALuint source;
+
+private:
+  OpenALSoundSource(const OpenALSoundSource&) = delete;
+  OpenALSoundSource& operator=(const OpenALSoundSource&) = delete;
 };
 
 #endif
index 576ab85..ebe7ea3 100644 (file)
@@ -32,7 +32,7 @@
 #include "util/file_system.hpp"
 #include "util/log.hpp"
 
-SoundFile* load_music_file(const std::string& filename)
+std::unique_ptr<SoundFile> load_music_file(const std::string& filename)
 {
   lisp::Parser parser(false);
   const lisp::Lisp* root = parser.parse(filename);
@@ -62,10 +62,10 @@ SoundFile* load_music_file(const std::string& filename)
     throw SoundError(msg.str());
   }
 
-  return new OggSoundFile(file, loop_begin, loop_at);
+  return std::unique_ptr<SoundFile>(new OggSoundFile(file, loop_begin, loop_at));
 }
 
-SoundFile* load_sound_file(const std::string& filename)
+std::unique_ptr<SoundFile> load_sound_file(const std::string& filename)
 {
   if(filename.length() > 6
      && filename.compare(filename.length()-6, 6, ".music") == 0) {
@@ -85,9 +85,9 @@ SoundFile* load_sound_file(const std::string& filename)
       throw SoundError("Couldn't read magic, file too short");
     PHYSFS_seek(file, 0);
     if(strncmp(magic, "RIFF", 4) == 0)
-      return new WavSoundFile(file);
+      return std::unique_ptr<SoundFile>(new WavSoundFile(file));
     else if(strncmp(magic, "OggS", 4) == 0)
-      return new OggSoundFile(file, 0, -1);
+      return std::unique_ptr<SoundFile>(new OggSoundFile(file, 0, -1));
     else
       throw SoundError("Unknown file format");
   } catch(std::exception& e) {
index 2fc1d34..5a84cff 100644 (file)
@@ -18,6 +18,7 @@
 #define HEADER_SUPERTUX_AUDIO_SOUND_FILE_HPP
 
 #include <iostream>
+#include <memory>
 
 class SoundFile
 {
@@ -40,9 +41,13 @@ public:
   int bits_per_sample;
   /// size in bytes
   size_t size;
+
+private:
+  SoundFile(const SoundFile&) = delete;
+  SoundFile& operator=(const SoundFile&) = delete;
 };
 
-SoundFile* load_sound_file(const std::string& filename);
+std::unique_ptr<SoundFile> load_sound_file(const std::string& filename);
 
 #endif
 
index b51c0e4..dd8fdce 100644 (file)
@@ -34,7 +34,7 @@ SoundManager::SoundManager() :
   buffers(),
   sources(),
   update_list(),
-  music_source(0),
+  music_source(),
   music_enabled(false),
   current_music()
 {
@@ -69,11 +69,8 @@ SoundManager::SoundManager() :
 
 SoundManager::~SoundManager()
 {
-  delete music_source;
-
-  for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
-    delete *i;
-  }
+  music_source.reset();
+  sources.clear();
 
   for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
     ALuint buffer = i->second;
@@ -91,34 +88,28 @@ SoundManager::~SoundManager()
 }
 
 ALuint
-SoundManager::load_file_into_buffer(SoundFile* file)
+SoundManager::load_file_into_buffer(SoundFile& file)
 {
   ALenum format = get_sample_format(file);
   ALuint buffer;
   alGenBuffers(1, &buffer);
   check_al_error("Couldn't create audio buffer: ");
-  char* samples = new char[file->size];
-  try {
-    file->read(samples, file->size);
-    alBufferData(buffer, format, samples,
-                 static_cast<ALsizei> (file->size),
-                 static_cast<ALsizei> (file->rate));
-    check_al_error("Couldn't fill audio buffer: ");
-  } catch(...) {
-    delete[] samples;
-    throw;
-  }
-  delete[] samples;
+  std::unique_ptr<char[]> samples(new char[file.size]);
+  file.read(samples.get(), file.size);
+  alBufferData(buffer, format, samples.get(),
+               static_cast<ALsizei>(file.size),
+               static_cast<ALsizei>(file.rate));
+  check_al_error("Couldn't fill audio buffer: ");
 
   return buffer;
 }
 
-OpenALSoundSource*
+std::unique_ptr<OpenALSoundSource>
 SoundManager::intern_create_sound_source(const std::string& filename)
 {
   assert(sound_enabled);
 
-  std::unique_ptr<OpenALSoundSource> source (new OpenALSoundSource());
+  std::unique_ptr<OpenALSoundSource> source(new OpenALSoundSource);
 
   ALuint buffer;
 
@@ -128,25 +119,25 @@ SoundManager::intern_create_sound_source(const std::string& filename)
     buffer = i->second;
   } else {
     // Load sound file
-    std::unique_ptr<SoundFile> file (load_sound_file(filename));
+    std::unique_ptr<SoundFile> file(load_sound_file(filename));
 
     if(file->size < 100000) {
-      buffer = load_file_into_buffer(file.get());
+      buffer = load_file_into_buffer(*file);
       buffers.insert(std::make_pair(filename, buffer));
     } else {
-      StreamSoundSource* source = new StreamSoundSource();
-      source->set_sound_file(file.release());
-      return source;
+      std::unique_ptr<StreamSoundSource> source(new StreamSoundSource);
+      source->set_sound_file(std::move(file));
+      return std::move(source);
     }
 
     log_debug << "Uncached sound \"" << filename << "\" requested to be played" << std::endl;
   }
 
   alSourcei(source->source, AL_BUFFER, buffer);
-  return source.release();
+  return std::move(source);
 }
 
-SoundSource*
+std::unique_ptr<SoundSource>
 SoundManager::create_sound_source(const std::string& filename)
 {
   if(!sound_enabled)
@@ -176,7 +167,7 @@ SoundManager::preload(const std::string& filename)
     if(file->size >= 100000)
       return;
 
-    ALuint buffer = load_file_into_buffer(file.get());
+    ALuint buffer = load_file_into_buffer(*file);
     buffers.insert(std::make_pair(filename, buffer));
   } catch(std::exception& e) {
     log_warning << "Error while preloading sound file: " << e.what() << std::endl;
@@ -190,8 +181,7 @@ SoundManager::play(const std::string& filename, const Vector& pos)
     return;
 
   try {
-    std::unique_ptr<OpenALSoundSource> source
-      (intern_create_sound_source(filename));
+    std::unique_ptr<OpenALSoundSource> source(intern_create_sound_source(filename));
 
     if(pos.x < 0 || pos.y < 0) {
       source->set_relative(true);
@@ -199,33 +189,37 @@ SoundManager::play(const std::string& filename, const Vector& pos)
       source->set_position(pos);
     }
     source->play();
-    sources.push_back(source.release());
+    sources.push_back(std::move(source));
   } catch(std::exception& e) {
     log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
   }
 }
 
 void
-SoundManager::manage_source(SoundSource* source)
+SoundManager::manage_source(std::unique_ptr<SoundSource> source)
 {
-  assert(source != NULL);
-
-  OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
-  if(openal_source != NULL) {
-    sources.push_back(openal_source);
+  assert(source);
+  if (dynamic_cast<OpenALSoundSource*>(source.get()))
+  {
+    std::unique_ptr<OpenALSoundSource> openal_source(dynamic_cast<OpenALSoundSource*>(source.release()));
+    sources.push_back(std::move(openal_source));
   }
 }
 
 void
-SoundManager::register_for_update( StreamSoundSource* sss ){
-  if( sss != NULL ){
-    update_list.push_back( sss );
+SoundManager::register_for_update(StreamSoundSource* sss)
+{
+  if (sss)
+  {
+    update_list.push_back(sss);
   }
 }
 
 void
-SoundManager::remove_from_update( StreamSoundSource* sss  ){
-  if( sss != NULL ){
+SoundManager::remove_from_update(StreamSoundSource* sss)
+{
+  if (sss)
+  {
     StreamSoundSources::iterator i = update_list.begin();
     while( i != update_list.end() ){
       if( *i == sss ){
@@ -257,8 +251,7 @@ SoundManager::enable_music(bool enable)
     play_music(current_music);
   } else {
     if(music_source) {
-      delete music_source;
-      music_source = NULL;
+      music_source.reset();
     }
   }
 }
@@ -271,8 +264,7 @@ SoundManager::stop_music(float fadetime)
        && music_source->get_fade_state() != StreamSoundSource::FadingOff)
       music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
   } else {
-    delete music_source;
-    music_source = NULL;
+    music_source.reset();
   }
   current_music = "";
 }
@@ -287,8 +279,7 @@ SoundManager::play_music(const std::string& filename, bool fade)
     return;
 
   if(filename == "") {
-    delete music_source;
-    music_source = NULL;
+    music_source.reset();
     return;
   }
 
@@ -301,8 +292,7 @@ SoundManager::play_music(const std::string& filename, bool fade)
       newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
     newmusic->play();
 
-    delete music_source;
-    music_source = newmusic.release();
+    music_source = std::move(newmusic);
   } catch(std::exception& e) {
     log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
     // When this happens, previous music continued playing, stop it, just in case.
@@ -341,12 +331,11 @@ SoundManager::update()
 
   // update and check for finished sound sources
   for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
-    OpenALSoundSource* source = *i;
+    auto& source = *i;
 
     source->update();
 
     if(!source->playing()) {
-      delete source;
       i = sources.erase(i);
     } else {
       ++i;
@@ -372,20 +361,20 @@ SoundManager::update()
 }
 
 ALenum
-SoundManager::get_sample_format(SoundFile* file)
+SoundManager::get_sample_format(const SoundFile& file)
 {
-  if(file->channels == 2) {
-    if(file->bits_per_sample == 16) {
+  if(file.channels == 2) {
+    if(file.bits_per_sample == 16) {
       return AL_FORMAT_STEREO16;
-    } else if(file->bits_per_sample == 8) {
+    } else if(file.bits_per_sample == 8) {
       return AL_FORMAT_STEREO8;
     } else {
       throw std::runtime_error("Only 16 and 8 bit samples supported");
     }
-  } else if(file->channels == 1) {
-    if(file->bits_per_sample == 16) {
+  } else if(file.channels == 1) {
+    if(file.bits_per_sample == 16) {
       return AL_FORMAT_MONO16;
-    } else if(file->bits_per_sample == 8) {
+    } else if(file.bits_per_sample == 8) {
       return AL_FORMAT_MONO8;
     } else {
       throw std::runtime_error("Only 16 and 8 bit samples supported");
index 3744c62..da042c6 100644 (file)
@@ -18,6 +18,7 @@
 #define HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP
 
 #include <map>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -44,7 +45,7 @@ public:
    * sound).
    * This function never throws exceptions, but might return a DummySoundSource
    */
-  SoundSource* create_sound_source(const std::string& filename);
+  std::unique_ptr<SoundSource> create_sound_source(const std::string& filename);
   /**
    * Convenience function to simply play a sound at a given position.
    */
@@ -53,7 +54,7 @@ public:
    * Adds the source to the list of managed sources (= the source gets deleted
    * when it finished playing)
    */
-  void manage_source(SoundSource* source);
+  void manage_source(std::unique_ptr<SoundSource> source);
   /// preloads a sound, so that you don't get a lag later when playing it
   void preload(const std::string& name);
 
@@ -87,9 +88,9 @@ private:
   friend class StreamSoundSource;
 
   /** creates a new sound source, might throw exceptions, never returns NULL */
-  OpenALSoundSource* intern_create_sound_source(const std::string& filename);
-  static ALuint load_file_into_buffer(SoundFile* file);
-  static ALenum get_sample_format(SoundFile* file);
+  std::unique_ptr<OpenALSoundSource> intern_create_sound_source(const std::string& filename);
+  static ALuint load_file_into_buffer(SoundFile& file);
+  static ALenum get_sample_format(const SoundFile& file);
 
   void print_openal_version();
   void check_alc_error(const char* message);
@@ -101,13 +102,13 @@ private:
 
   typedef std::map<std::string, ALuint> SoundBuffers;
   SoundBuffers buffers;
-  typedef std::vector<OpenALSoundSource*> SoundSources;
+  typedef std::vector<std::unique_ptr<OpenALSoundSource> > SoundSources;
   SoundSources sources;
 
   typedef std::vector<StreamSoundSource*> StreamSoundSources;
   StreamSoundSources update_list;
 
-  StreamSoundSource* music_source;
+  std::unique_ptr<StreamSoundSource> music_source;
 
   bool music_enabled;
   std::string current_music;
index e473e04..065bfcb 100644 (file)
@@ -27,8 +27,8 @@ class Vector;
 class SoundSource
 {
 public:
-  virtual ~SoundSource()
-  }
+  SoundSource() {}
+  virtual ~SoundSource() {}
 
   virtual void play() = 0;
   virtual void stop() = 0;
@@ -42,6 +42,10 @@ public:
   virtual void set_position(const Vector& position) = 0;
   virtual void set_velocity(const Vector& velocity) = 0;
   virtual void set_reference_distance(float distance) = 0;
+
+private:
+  SoundSource(const SoundSource&) = delete;
+  SoundSource& operator=(const SoundSource&) = delete;
 };
 
 #endif
index fceab80..2645e0d 100644 (file)
@@ -21,7 +21,7 @@
 #include "util/log.hpp"
 
 StreamSoundSource::StreamSoundSource() :
-  file(0),
+  file(),
   fade_state(NoFading),
   fade_start_time(),
   fade_time(),
@@ -37,17 +37,16 @@ StreamSoundSource::~StreamSoundSource()
 {
   //don't update me any longer
   sound_manager->remove_from_update( this );
-  delete file;
+  file.reset();
   stop();
   alDeleteBuffers(STREAMFRAGMENTS, buffers);
   SoundManager::check_al_error("Couldn't delete audio buffers: ");
 }
 
 void
-StreamSoundSource::set_sound_file(SoundFile* newfile)
+StreamSoundSource::set_sound_file(std::unique_ptr<SoundFile> newfile)
 {
-  delete file;
-  file = newfile;
+  file = std::move(newfile);
 
   ALint queued;
   alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
@@ -111,10 +110,10 @@ bool
 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
 {
   // fill buffer
-  char* bufferdata = new char[STREAMFRAGMENTSIZE];
+  std::unique_ptr<char[]> bufferdata(new char[STREAMFRAGMENTSIZE]);
   size_t bytesread = 0;
   do {
-    bytesread += file->read(bufferdata + bytesread,
+    bytesread += file->read(bufferdata.get() + bytesread,
                             STREAMFRAGMENTSIZE - bytesread);
     // end of sound file
     if(bytesread < STREAMFRAGMENTSIZE) {
@@ -126,14 +125,13 @@ StreamSoundSource::fillBufferAndQueue(ALuint buffer)
   } while(bytesread < STREAMFRAGMENTSIZE);
 
   if(bytesread > 0) {
-    ALenum format = SoundManager::get_sample_format(file);
-    alBufferData(buffer, format, bufferdata, bytesread, file->rate);
+    ALenum format = SoundManager::get_sample_format(*file);
+    alBufferData(buffer, format, bufferdata.get(), bytesread, file->rate);
     SoundManager::check_al_error("Couldn't refill audio buffer: ");
 
     alSourceQueueBuffers(source, 1, &buffer);
     SoundManager::check_al_error("Couldn't queue audio buffer: ");
   }
-  delete[] bufferdata;
 
   // return false if there aren't more buffers to fill
   return bytesread >= STREAMFRAGMENTSIZE;
index d3b3e94..1dbc63b 100644 (file)
@@ -27,7 +27,7 @@ public:
   StreamSoundSource();
   virtual ~StreamSoundSource();
 
-  void set_sound_file(SoundFile* file);
+  void set_sound_file(std::unique_ptr<SoundFile> newfile);
 
   enum FadeState { NoFading, FadingOn, FadingOff };
 
@@ -54,7 +54,7 @@ private:
   = STREAMBUFFERSIZE / STREAMFRAGMENTS;
 
   bool fillBufferAndQueue(ALuint buffer);
-  SoundFile* file;
+  std::unique_ptr<SoundFile> file;
   ALuint buffers[STREAMFRAGMENTS];
 
   FadeState fade_state;
index b0d43c0..efd0433 100644 (file)
@@ -32,7 +32,7 @@ Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "imag
   set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
   countMe = false;
 
-  ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
+  ticking = sound_manager->create_sound_source("sounds/fizz.wav");
   ticking->set_position(get_pos());
   ticking->set_looping(true);
   ticking->set_gain(2.0);
index deac0ff..47889d9 100644 (file)
@@ -75,7 +75,7 @@ Dart::initialize()
 void
 Dart::activate()
 {
-  sound_source.reset(sound_manager->create_sound_source(DART_SOUND));
+  sound_source = sound_manager->create_sound_source(DART_SOUND);
   sound_source->set_position(get_pos());
   sound_source->set_looping(true);
   sound_source->set_gain(1.0);
index 6df88e0..0539577 100644 (file)
@@ -85,7 +85,7 @@ Flame::draw(DrawingContext& context)
 void
 Flame::activate()
 {
-  sound_source.reset(sound_manager->create_sound_source(FLAME_SOUND));
+  sound_source = sound_manager->create_sound_source(FLAME_SOUND);
   sound_source->set_position(get_pos());
   sound_source->set_looping(true);
   sound_source->set_gain(2.0);
index 6b626a6..1fbe184 100644 (file)
@@ -114,7 +114,7 @@ GoldBomb::collision_squished(GameObject& object)
     if (player)
       player->bounce(*this);
 
-    ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
+    ticking = sound_manager->create_sound_source("sounds/fizz.wav");
     ticking->set_position(get_pos());
     ticking->set_looping(true);
     ticking->set_gain(2.0);
index 87b2cf1..9620b70 100644 (file)
@@ -96,12 +96,12 @@ Haywire::collision_squished(GameObject& object)
     time_until_explosion = TIME_EXPLOSION;
     is_exploding = true;
 
-    ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
+    ticking = sound_manager->create_sound_source("sounds/fizz.wav");
     ticking->set_position(get_pos());
     ticking->set_looping(true);
     ticking->set_reference_distance(32);
     ticking->play();
-    grunting.reset(sound_manager->create_sound_source("sounds/grunts.ogg"));
+    grunting = sound_manager->create_sound_source("sounds/grunts.ogg");
     grunting->set_position(get_pos());
     grunting->set_looping(true);
     grunting->set_reference_distance(32);
index 4888a2e..a4d3d8d 100644 (file)
@@ -58,7 +58,7 @@ TreeWillOWisp::~TreeWillOWisp()
 void
 TreeWillOWisp::activate()
 {
-  sound_source.reset(sound_manager->create_sound_source(TREEWILLOSOUND));
+  sound_source = sound_manager->create_sound_source(TREEWILLOSOUND);
   sound_source->set_position(get_pos());
   sound_source->set_looping(true);
   sound_source->set_gain(2.0);
index 88e28d8..76298b3 100644 (file)
@@ -159,7 +159,7 @@ WillOWisp::active_update(float elapsed_time)
 void
 WillOWisp::activate()
 {
-  sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
+  sound_source = sound_manager->create_sound_source(SOUNDFILE);
   sound_source->set_position(get_pos());
   sound_source->set_looping(true);
   sound_source->set_gain(2.0);
index f397264..c2e9f55 100644 (file)
@@ -88,7 +88,7 @@ AmbientSound::AmbientSound(const Reader& lisp) :
 
   lisp.get("silence_distance",silence_distance);
 
-  sound_source = 0; // not playing at the beginning
+  sound_source.reset(); // not playing at the beginning
   sound_manager->preload(sample);
   latency=0;
 }
@@ -144,8 +144,7 @@ AmbientSound::hit(Player& )
 void
 AmbientSound::stop_playing()
 {
-  delete sound_source;
-  sound_source = 0;
+  sound_source.reset();
 }
 
 void
@@ -162,8 +161,7 @@ AmbientSound::start_playing()
     sound_source->play();
   } catch(std::exception& e) {
     log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
-    delete sound_source;
-    sound_source = 0;
+    sound_source.reset();
     remove_me();
   }
 }
index 1f059ef..e438ea9 100644 (file)
@@ -91,7 +91,7 @@ private:
   Vector dimension;
 
   std::string sample;
-  SoundSource* sound_source;
+  std::unique_ptr<SoundSource> sound_source;
   int latency;
 
   float distance_factor;  /// distance scaling