H3D API  2.4.1
H3DSoundFileNode.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __H3DSOUNDFILENODE_H__
30 #define __H3DSOUNDFILENODE_H__
31 
32 #include <H3D/H3DSoundStreamNode.h>
33 #include <list>
34 
35 namespace H3D {
36 
46  class H3DAPI_API H3DSoundFileNode : public H3DSoundStreamNode {
47  public:
49  typedef H3DSoundFileNode*( *CreateNodeFunc)();
50 
52  typedef bool ( *SupportsFileFunc)( const string &url );
53 
54  template< class N >
55  static H3DSoundFileNode *newSoundFileNode() { return new N; }
56 
58  struct H3DAPI_API FileReaderRegistration{
59  public:
61  FileReaderRegistration( const string &_name,
62  CreateNodeFunc _create,
63  SupportsFileFunc _supports ):
64  name( _name ),
65  create_func( _create ),
66  supports_func( _supports ) {
67  if( !H3DSoundFileNode::initialized) {
68  H3DSoundFileNode::registered_file_readers.reset(
69  new list< FileReaderRegistration > );
70  initialized = true;
71  }
73  }
74  string name;
75  CreateNodeFunc create_func;
76  SupportsFileFunc supports_func;
77  };
78 #ifdef __BORLANDC__
79  friend struct FileReaderRegistration;
80 #endif
83  virtual unsigned int load( const string &_url ) = 0;
84 
88  static H3DSoundFileNode *getSupportedFileReader( const string &url );
89 
96  static void registerFileReader( const string &name,
97  CreateNodeFunc create,
98  SupportsFileFunc supports ) {
99  registerFileReader( FileReaderRegistration( name, create, supports ) );
100  }
101 
104  static void registerFileReader( const FileReaderRegistration &fr ) {
105  registered_file_readers->push_back( fr );
106  }
107 
108  // Creating a new auto_ptr local for this node, because
109  // registrated_file_reader caused a memory leak and because
110  // of the order of setting the static variables the autp_ptr's
111  // constructor resets the auto_ptr to 0 eventhough the
112  // registrated_file_reader has been initilazed, and therefore
113  // cause an error making it imposible to use the standard auto_ptr.
114  template<class T>
115  class local_auto_ptr{
116  private:
117  T* ap; // refers to the actual owned object (if any)
118  public:
119  typedef T element_type;
120 
121  // constructor
122  explicit local_auto_ptr (T* ptr = 0) {
123  if(!initialized){
124  ap=ptr;
125  }
126  }
127 
128  // copy constructors (with implicit conversion)
129  // - note: nonconstant parameter
130  local_auto_ptr (local_auto_ptr& rhs) throw() : ap(rhs.release()) { }
131 
132  template<class Y>
133  local_auto_ptr (local_auto_ptr<Y>& rhs) throw() : ap(rhs.release()) { }
134 
135  // assignments (with implicit conversion)
136  // - note: nonconstant parameter
137  local_auto_ptr& operator= (local_auto_ptr& rhs) throw(){
138  if(!initialized){
139  reset(rhs.release());
140  return *this;
141  }
142  }
143  template<class Y>
144  local_auto_ptr& operator= (local_auto_ptr<Y>& rhs) throw(){
145  if(!initialized){
146  reset(rhs.release());
147  return *this;
148  }
149  }
150 
151  // destructor
152  ~local_auto_ptr() throw(){
153  delete ap;
154  }
155 
156  // value access
157  T* get() const throw(){
158  return ap;
159  }
160  T& operator*() const throw(){
161  return *ap;
162  }
163  T* operator->() const throw(){
164  return ap;
165  }
166 
167  // release ownership
168  T* release() throw(){
169  if(!initialized){
170  T* tmp(ap);
171  ap = 0;
172  return tmp;
173  }
174  }
175 
176  // reset value
177  void reset (T* ptr=0) throw(){
178  if(!initialized){
179  if (ap != ptr){
180  delete ap;
181  ap = ptr;
182  }
183  }
184  }
185  };
186 
187  protected:
188  static local_auto_ptr<list< FileReaderRegistration > > registered_file_readers;
189  static bool initialized;
190  };
191 }
192 
193 #endif
Header file for H3DSoundStreamNode, X3D scene-graph node.
H3DUTIL_API void reset()
This abstract node type is used to derive node types that can stream PCM sound data from a file.
Definition: H3DSoundFileNode.h:46
virtual unsigned int load(const string &_url)=0
Load a sound file from the given url that will be used to generate PCM data.
static void registerFileReader(const string &name, CreateNodeFunc create, SupportsFileFunc supports)
Register a file reader that can then be returned by getSupportedFileReader().
Definition: H3DSoundFileNode.h:96
static void registerFileReader(const FileReaderRegistration &fr)
Register a file reader that can then be returned by getSupportedFileReader().
Definition: H3DSoundFileNode.h:104
This abstract node type is used to derive node types that can stream PCM sound data.
Definition: H3DSoundStreamNode.h:42
DualQuaternion operator*(const DualQuaternion &q1, const DualQuaternion &q2)
H3D API namespace.
Definition: Anchor.h:38
Class used to register a class to the registered file readers.
Definition: H3DSoundFileNode.h:58
FileReaderRegistration(const string &_name, CreateNodeFunc _create, SupportsFileFunc _supports)
Constructor.
Definition: H3DSoundFileNode.h:61