#pragma region CPL License
/*
Nuclex Engine
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#pragma endregion
#include "Nuclex/Audio/AudioServer.h"
#include "Nuclex/Support/Exception.h"
namespace Nuclex { namespace Audio {
// ------------------------------------------------------------------------------------------- //
namespace {
/// Enumerates over all devices registered to the Audio server
class AudioDriverEnumerator : public AudioServer::DriverEnumerator {
/// Type of the driver map the enumerator iterates over
public: typedef std::map > DriverMap;
/// Initializes an instance of AudioDriverEnumerator
/// Drivers to enumerate
public: AudioDriverEnumerator(DriverMap &Drivers) :
m_Drivers(Drivers),
m_DriverIt(Drivers.begin()),
m_DriverEnd(Drivers.end()) {}
/// Immediately releases all resources used by the enumerator
public: virtual ~AudioDriverEnumerator() {}
//
// AudioDriverEnumerator implementation
//
///
/// Returns the current renderer being enumerated and advances to the next
///
/// The currently enumerated renderer
/// If no more renderers are remaning, NULL is returned
public: bool next() {
if(m_DriverIt == m_DriverEnd) {
m_DriverIt = m_Drivers.begin();
} else {
++m_DriverIt;
}
return (m_DriverIt != m_DriverEnd);
}
public :const shared_ptr &get() const {
if(m_DriverIt == m_DriverEnd) {
throw FailedException(
"AudioDriverEnumerator::get()", "Enumerator is in empty cycle position"
);
}
return m_DriverIt->second;
}
/// Map containing the drivers being enumerated
private: DriverMap &m_Drivers;
/// Map iterator pointing to the enumerator's current position
private: DriverMap::const_iterator m_DriverIt; ///< Current renderer
/// Map iterator pointing to the end of the map
private: DriverMap::const_iterator m_DriverEnd;
};
} // anonymous namespace
// ------------------------------------------------------------------------------------------- //
AudioServer::~AudioServer() {
clearDrivers();
}
// ------------------------------------------------------------------------------------------- //
const shared_ptr &AudioServer::getDriver(const string &sName) const {
DriverMap::const_iterator DriverIt = m_Drivers.find(sName);
if(DriverIt == m_Drivers.end()) {
throw InvalidArgumentException(
"Nuclex::Audio::AudioServer::getDriver()", string("Driver not found: '") + sName + "'"
);
}
return DriverIt->second;
}
// ------------------------------------------------------------------------------------------- //
void AudioServer::addDriver(const string &sName, const shared_ptr &spDriver) {
DriverMap::iterator DriverIt = m_Drivers.find(sName);
if(DriverIt != m_Drivers.end())
DriverIt->second = spDriver;
else
m_Drivers.insert(DriverMap::value_type(sName, spDriver));
}
// ------------------------------------------------------------------------------------------- //
void AudioServer::removeDriver(const string &sName) {
DriverMap::iterator DriverIt = m_Drivers.find(sName);
if(DriverIt != m_Drivers.end()) {
m_Drivers.erase(DriverIt);
} else {
throw InvalidArgumentException(
"Nuclex::Audio::AudioServer::getDriver()", string("Driver not found: '") + sName + "'"
);
}
}
// ------------------------------------------------------------------------------------------- //
void AudioServer::clearDrivers() {
m_Drivers.clear();
}
// ------------------------------------------------------------------------------------------- //
shared_ptr AudioServer::enumDrivers() {
return shared_ptr(new AudioDriverEnumerator(m_Drivers));
}
// ------------------------------------------------------------------------------------------- //
}} // namespace Nuclex::Audio