Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Audio::TemplateManager Class Reference

#include <TemplateManager.h>

Inheritance diagram for Audio::TemplateManager:
Singleton< TemplateManager >

Public Member Functions

 TemplateManager () throw ()
 
 ~TemplateManager ()
 
void addDefinitionFile (const std::string &path, bool persistent) throw (Exception)
 
void addDefinitionFile (const std::string &path, SharedPtr< XMLDOM::XMLDocument > definition) throw (Exception)
 
SharedPtr< XMLDOM::XMLDocumentgetDefinitionFile (const std::string &path) const throw (ResourceNotLoadedException)
 
SharedPtr< XMLDOM::XMLDocumentgetDefinitionFile (const std::string &path) throw (Exception)
 
void setDefaultDefinitionFile (const std::string &x) throw ()
 
const std::string & getDefaultDefinitionFile () const throw ()
 
SharedPtr< SourceTemplategetSourceTemplate (const std::string &name) throw (Exception)
 
void addSourceTemplate (const std::string &name, SharedPtr< SourceTemplate > tpl, bool perm=true) throw (ResourceAlreadyLoadedException)
 
void addSourceTemplate (const std::string &path, const std::string &name, SharedPtr< SourceTemplate > tpl, bool perm=true) throw (ResourceAlreadyLoadedException)
 

Protected Member Functions

SharedPtr< SourceTemplateloadSourceTemplate (const std::string &name) throw (Exception)
 
- Protected Member Functions inherited from Singleton< TemplateManager >
 ~Singleton ()
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< TemplateManager >
static TemplateManager * getSingleton ()
 
- Static Protected Member Functions inherited from Singleton< TemplateManager >
static void initializeSingleton ()
 
static void deinitializeSingleton ()
 
- Static Protected Attributes inherited from Singleton< TemplateManager >
static TemplateManager * _singletonInstance
 

Detailed Description

Template manager class.

Remarks
Use it to create and manage source templates.
Instead of storing source templates themselves, since they may be shared,
you should store shared pointers to them. The manager will automatically detect and unload unreferenced templates.
Templates are addressed by key - keys are composed in a very specific way.
They are all of the form [path]:[name], where [path] is a source definition xml file and [name] is a source name within the xml file.
Usually xml files are loaded on-demand, and unloaded shortly after. Some
definition files, however, may contain heavily used sources and those are useful to keep laying around, preparsed. You can instruct the manager to do so with addDefinitionFile().
See Also
SourceTemplate

Definition at line 49 of file TemplateManager.h.

Constructor & Destructor Documentation

Audio::TemplateManager::TemplateManager ( )
throw (
)

Construct a new manager

Remarks
End-users of the class shouldn't be using this. Singletons need it.

Definition at line 154 of file TemplateManager.cpp.

154  :
155  data(new TemplateManagerData)
156  {
157  }
Audio::TemplateManager::~TemplateManager ( )

Definition at line 159 of file TemplateManager.cpp.

160  {
161  }

Member Function Documentation

void Audio::TemplateManager::addDefinitionFile ( const std::string &  path,
bool  persistent 
)
throw (Exception
)

Add a definition file, persistent or not

void Audio::TemplateManager::addDefinitionFile ( const std::string &  path,
SharedPtr< XMLDOM::XMLDocument definition 
)
throw (Exception
)

Add a definition document under a specified path, always persistent (as there is no way to reload)

void Audio::TemplateManager::addSourceTemplate ( const std::string &  name,
SharedPtr< SourceTemplate tpl,
bool  perm = true 
)
throw (ResourceAlreadyLoadedException
)

Add a manually-created template

Parameters
namethe name portion of the template's key
tplthe template to be added
permif true, a strong reference will be held and the template will become permanently loaded.
Remarks
The key to the newly added template will always be :[name] (empty path), denoting dynamically-created templates.
Note
Since the manager only holds weak references to templates if perm is not given, you must hold onto a reference at least, or the manager will "forget" you added this resource.
Exceptions
ResourceAlreadyLoadedException,whenthe key already has an associated template.
void Audio::TemplateManager::addSourceTemplate ( const std::string &  path,
const std::string &  name,
SharedPtr< SourceTemplate tpl,
bool  perm = true 
)
throw (ResourceAlreadyLoadedException
)

Add a manually-created template

Parameters
paththe path portion of the template's key
namethe name portion of the template's key
tplthe template to be added
permif true, a strong reference will be held and the template will become permanently loaded.
Remarks
The key to the newly added template will always be [path]:[name]. Using this method is discouraged if collission with filesystem-based templates would be possible (pick paths that don't map to file system paths)
Note
Since the manager only holds weak references to templates if perm is not given, you must hold onto a reference at least, or the manager will "forget" you added this resource.
Exceptions
ResourceAlreadyLoadedException,whenthe key already has an associated template.
const std::string & Audio::TemplateManager::getDefaultDefinitionFile ( ) const
throw (
)

Get the default definition file

See Also
setDefaultDefinitionFile

Definition at line 205 of file TemplateManager.cpp.

207  {
208  return data->defaultDefinitionFile;
209  }
SharedPtr< XMLDOM::XMLDocument > Audio::TemplateManager::getDefinitionFile ( const std::string &  path) const
throw (ResourceNotLoadedException
)

Get an already loaded definition file, fail if not found or not loaded

Definition at line 182 of file TemplateManager.cpp.

184  {
185  return ((const TemplateManagerData &)*data).getDefinitionFile(path);
186  }
SharedPtr< XMLDOM::XMLDocument > Audio::TemplateManager::getDefinitionFile ( const std::string &  path)
throw (Exception
)

Get an already loaded definition file, load if not loaded

Definition at line 188 of file TemplateManager.cpp.

References e.

190  {
191  try {
192  return data->getDefinitionFile(path);
193  } catch(NotFoundException e) {
194  addDefinitionFile(path, false);
195  return data->getDefinitionFile(path);
196  }
197  }
SharedPtr< SourceTemplate > Audio::TemplateManager::getSourceTemplate ( const std::string &  name)
throw (Exception
)

Get a source template by its key

Definition at line 211 of file TemplateManager.cpp.

213  {
214  SharedPtr<SourceTemplate> rv;
215 
216  TemplateManagerData::TemplateMap::const_iterator it = data->loadedTemplates.find(name);
217  if (it != data->loadedTemplates.end())
218  rv = it->second.lock();
219 
220  if (!rv.get()) {
221  rv = loadSourceTemplate(name);
222  data->loadedTemplates[name] = rv;
223  }
224 
225  return rv;
226  }
SharedPtr< SourceTemplate > Audio::TemplateManager::loadSourceTemplate ( const std::string &  name)
throw (Exception
)
protected

Get a source template by its key

Definition at line 228 of file TemplateManager.cpp.

References f, XMLDOM::XMLElement::getAttributeValue(), M_PI, Audio::__impl::parseBool(), Audio::__impl::parseFloat(), and Audio::__impl::parseVSFileType().

230  {
231  string::size_type sep = name.find_first_of(':');
232  SharedPtr<XMLDOM::XMLDocument> def;
233 
234  if (sep != string::npos)
235  def = getDefinitionFile(name.substr(0,sep)); else
237 
238  const XMLDOM::XMLElement *tdef = 0;
239  if (sep != string::npos)
240  tdef = def->getElementByName(name.substr(sep+1, string::npos)); else
241  tdef = def->getElementByName(name);
242 
243  if (!tdef)
244  throw NotFoundException(name);
245 
246  string src =
247  tdef->getAttributeValue("src","");
248 
249  if ( src.empty() )
250  throw InvalidParametersException("Invalid source template: no sound specified");
251 
253  parseVSFileType( tdef->getAttributeValue("type", "unknown") );
254  bool looping =
255  parseBool( tdef->getAttributeValue("looping", "false") );
256 
257  SharedPtr<SourceTemplate> rv(
258  new SourceTemplate(src, type, looping) );
259 
260  rv->setAngleRange( Range<Scalar>(
261  parseFloat( tdef->getAttributeValue("minAngle", "180") ) * M_PI / 180.f,
262  parseFloat( tdef->getAttributeValue("maxAngle", "180") ) * M_PI / 180.f
263  ) );
264 
265  rv->setPerFrequencyRadiusRatios( PerFrequency<Scalar>(
266  parseFloat( tdef->getAttributeValue("lfRadiusRatio", "1") ),
267  parseFloat( tdef->getAttributeValue("hfRadiusRatio", "1") )
268  ) );
269 
270  rv->setPerFrequencyRadiusRatios( PerFrequency<Scalar>(
271  parseFloat( tdef->getAttributeValue("lfReferenceFreq", "250") ),
272  parseFloat( tdef->getAttributeValue("hfReferenceFreq", "5000") )
273  ) );
274 
275  rv->setGain(
276  parseFloat( tdef->getAttributeValue("gain", "1") )
277  );
278 
279  rv->setStreaming(
280  parseBool( tdef->getAttributeValue("streaming", "false") )
281  );
282 
283  return rv;
284  }
void Audio::TemplateManager::setDefaultDefinitionFile ( const std::string &  x)
throw (
)

Set default definition file

Remarks
when a template named without its source definition file is requested, it is assumed to come from this definition file.

Definition at line 199 of file TemplateManager.cpp.

References x.

201  {
202  data->defaultDefinitionFile = x;
203  }

The documentation for this class was generated from the following files: