H3D API  2.4.1
H3DImageLoaderNode.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 //
28 //
30 #ifndef __H3DIMAGELOADERNODE_H__
31 #define __H3DIMAGELOADERNODE_H__
32 
33 #include <H3DUtil/Image.h>
34 #include <H3D/Node.h>
35 #include <list>
36 
37 namespace H3D {
38 
48  class H3DAPI_API H3DImageLoaderNode : public Node {
49  public:
50  typedef H3DImageLoaderNode*( *CreateNodeFunc)();
51 
53  typedef bool ( *SupportsFileFunc)( const string &url );
54 
56  typedef bool ( *SupportsStreamFunc)( istream &is );
57 
58  template< class N >
59  static H3DImageLoaderNode *newImageLoaderNode() { return new N; }
60 
62  struct H3DAPI_API FileReaderRegistration{
63  public:
65  FileReaderRegistration( const string &_name,
66  CreateNodeFunc _create,
67  SupportsFileFunc _supports,
68  SupportsStreamFunc _supports_stream= NULL ):
69  name( _name ),
70  create_func( _create ),
71  supports_func( _supports ),
72  supports_stream_func ( _supports_stream ) {
73 
74  if( !H3DImageLoaderNode::initialized ) {
75  H3DImageLoaderNode::registered_file_readers.reset(
76  new list< FileReaderRegistration > );
77  initialized = true;
78  }
80  }
81 
82  string name;
83  CreateNodeFunc create_func;
84  SupportsFileFunc supports_func;
85  SupportsStreamFunc supports_stream_func;
86  };
87 #ifdef __BORLANDC__
88  friend struct FileReaderRegistration;
89 #endif
90 
93  type_name = "H3DImageLoaderNode";
94  }
95 
100  virtual Image *loadImage( const string &url ) = 0;
101 
109  virtual Image *loadImage( istream &/*is*/ ) { return NULL; }
110 
114  virtual string defaultXMLContainerField() {
115  return "imageLoader";
116  }
117 
121  static H3DImageLoaderNode *getSupportedFileReader( const string &url );
122 
126  static H3DImageLoaderNode *getSupportedFileReader( istream &is );
127 
136  static void registerFileReader( const string &name,
137  CreateNodeFunc create,
138  SupportsFileFunc supports,
139  SupportsStreamFunc supports_stream= NULL ) {
140  registerFileReader( FileReaderRegistration( name, create, supports, supports_stream ) );
141  }
142 
145  static void registerFileReader( const FileReaderRegistration &fr ) {
146  registered_file_readers->push_back( fr );
147  }
148 
149  // Creating a new auto_ptr local for this node, because
150  // registrated_file_reader caused a memory leak and because
151  // of the order of setting the static variables the auto_ptr's
152  // constructor resets the auto_ptr to 0 eventhough the
153  // registered_file_readers has been initilazed, and therefore
154  // cause an error making it impossible to use the standard auto_ptr.
155  template<class T>
156  class local_auto_ptr{
157  private:
158  T* ap; // refers to the actual owned object (if any)
159  public:
160  typedef T element_type;
161 
162  // constructor
163  explicit local_auto_ptr (T* ptr = 0) {
164  if(!initialized){
165  ap=ptr;
166  }
167  }
168 
169  // copy constructors (with implicit conversion)
170  // - note: nonconstant parameter
171  local_auto_ptr (local_auto_ptr& rhs) throw() : ap(rhs.release()) { }
172 
173  template<class Y>
174  local_auto_ptr (local_auto_ptr<Y>& rhs) throw() : ap(rhs.release()) { }
175 
176  // assignments (with implicit conversion)
177  // - note: nonconstant parameter
178  local_auto_ptr& operator= (local_auto_ptr& rhs) throw(){
179  if(!initialized){
180  reset(rhs.release());
181  return *this;
182  }
183  }
184  template<class Y>
185  local_auto_ptr& operator= (local_auto_ptr<Y>& rhs) throw(){
186  if(!initialized){
187  reset(rhs.release());
188  return *this;
189  }
190  }
191 
192  // destructor
193  ~local_auto_ptr() throw(){
194  delete ap;
195  }
196 
197  // value access
198  T* get() const throw(){
199  return ap;
200  }
201  T& operator*() const throw(){
202  return *ap;
203  }
204  T* operator->() const throw(){
205  return ap;
206  }
207 
208  // release ownership
209  T* release() throw(){
210  if(!initialized){
211  T* tmp(ap);
212  ap = 0;
213  return tmp;
214  }
215  }
216 
217  // reset value
218  void reset (T* ptr=0) throw(){
219  if(!initialized){
220  if (ap != ptr){
221  delete ap;
222  ap = ptr;
223  }
224  }
225  }
226  };
227 
228  protected:
229  static local_auto_ptr< list< FileReaderRegistration > > registered_file_readers;
230  //static list< FileReaderRegistration > *registered_file_readers;
231  static bool initialized;
232  };
233 }
234 
235 #endif
H3DUTIL_API void reset()
Node base class.
H3DImageLoaderNode is a virtual base class for classes that read an image file format such as PNG or ...
Definition: H3DImageLoaderNode.h:48
virtual Image * loadImage(const string &url)=0
Pure virtual function to load an Image from a url.
static void registerFileReader(const FileReaderRegistration &fr)
Register a file reader that can then be returned by getSupportedFileReader().
Definition: H3DImageLoaderNode.h:145
H3DImageLoaderNode()
Constructor.
Definition: H3DImageLoaderNode.h:92
virtual Image * loadImage(istream &)
Load an image from an istream.
Definition: H3DImageLoaderNode.h:109
static void registerFileReader(const string &name, CreateNodeFunc create, SupportsFileFunc supports, SupportsStreamFunc supports_stream=NULL)
Register a file reader that can then be returned by getSupportedFileReader().
Definition: H3DImageLoaderNode.h:136
virtual string defaultXMLContainerField()
Returns the default xml containerField attribute value.
Definition: H3DImageLoaderNode.h:114
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
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: H3DImageLoaderNode.h:62
FileReaderRegistration(const string &_name, CreateNodeFunc _create, SupportsFileFunc _supports, SupportsStreamFunc _supports_stream=NULL)
Constructor.
Definition: H3DImageLoaderNode.h:65