HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FS_FileResolver.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: FS_FileResolver.h (FS Library, C++)
7  *
8  * COMMENTS: This class resolves the data specified by its name. The name can
9  * a file path or an "op:" path.
10  *
11  * In applications that can cook the op data (eg, Houdini and
12  * Batch), the resolver instance actually searches out the node and
13  * asks it for the data, then it creates a stream and returns it as
14  * a file reader object. To release this temporarily created
15  * stream, the requester needs to let the resolver know so that
16  * it can relinquish it.
17  *
18  * In applications that don't have access to nodes (eg, Mantra),
19  * the resolver instance needs to rely on the data given to
20  * it (eg, inside the IFD stream). Since other entities (eg,
21  * shaders) still refer to the data as "op:", the resolver
22  * needs to figure out which data to return for each request.
23  * The data is returned in a form of FS_Reader, but the actual
24  * data may be in memory, in a temp file on disk, or elsewhere.
25  * The requester may notify the resolver that it is done with the
26  * data and to relinquish it, but the resolver will probably need
27  * to keep the data around for the future requests, since there is
28  * no source for that data (no nodes, and IFD has been already
29  * sourced before).
30  *
31  */
32 
33 #ifndef __FS_FileResolver__
34 #define __FS_FileResolver__
35 
36 #include "FS_API.h"
37 #include <UT/UT_NonCopyable.h>
38 #include <UT/UT_String.h>
39 #include <UT/UT_StringHolder.h>
40 #include <UT/UT_SymbolTable.h>
41 #include <SYS/SYS_Types.h>
42 
43 class UT_IStream;
44 class FS_Reader;
45 
46 
47 //=============================================================================
48 /// Provides various options for creating and obtaining the file data.
50 {
51 public:
52  /// Constructor.
55 
56  /// Assignment operator.
57  const FS_FileResolverOptions & operator=(
58  const FS_FileResolverOptions & src );
59 
60  /// Gets the time at which a node should be evaluated to obtain the data.
61  fpreal getTime() const
62  { return myTime; }
64  { myTime = time; }
65 
66  /// Gets the extension for the file that will contain the resolved data.
67  /// It indirectly determines the data type for stream readers and writers.
69  { return myFileExtension; }
71  { myFileExtension = ext; }
72 
73  /// Checks if the file should be treated as an environment map.
74  bool getIsEnvMap() const
75  { return myIsEnvMap; }
76  void setIsEnvMap( bool env_map )
77  { myIsEnvMap = env_map; }
78 
79 
80 private:
81  UT_StringHolder myFileExtension; // extension of the data file
82  fpreal myTime; // node evaluation time
83  bool myIsEnvMap; // is environment map
84 };
85 
86 
87 //=============================================================================
88 /// Maps the operator paths (eg, "op:/path"), and other data sources,
89 /// to file readers that access their data.
91 {
92 public:
93  /// Constructor.
95  /// Destructor.
96  virtual ~FS_FileResolver();
97 
98  /// Sets the instance that will resolve the operator paths to data files.
99  static void setResolver( FS_FileResolver * resolver );
100 
101  /// Returns the currently set resolver.
102  static FS_FileResolver * getResolver();
103 
104 
105  /// Checks if the resolver can provide the file data for the given name.
106  // TODO: this method may need to take FS_FileResolverOptions argument
107  virtual bool getCanResolve( const char * name ) = 0;
108 
109  /// @brief Creates a new reader object given the file (or data) name.
110  ///
111  /// The returned reader can be used to read the data, and when finished,
112  /// it should be freed by passing it to the FS_FileResolver::deleteReader()
113  /// method.
114  ///
115  /// @param opts The options that indicate how the data should be
116  /// generated, if applicable. Eg, it contains the evaluation
117  /// time for nodes or the file format in which the data
118  /// should be available.
119  ///
120  /// @param name The name of the data to be read. Can be a file
121  /// specified with "file:/path" or an operator node
122  /// data specified with "op:/path", or any other standard
123  /// way of specifying data in Houdini.
124  //
125  /// @return The reader for the specified data.
126  ///
127  virtual FS_Reader * newReader( const FS_FileResolverOptions & opts,
128  const char * name ) = 0;
129 
130 
131  /// Closes stream (if they are still open) and deletes the reader.
132  /// After this call, the reader pointer will no longer be valid.
133  ///
134  /// @param reader The reader to be freed.
135  ///
136  virtual void deleteReader( FS_Reader * reader ) = 0;
137 
138 
139  /// Obtains the file path to the data of a given name. On success,
140  /// 'file_path' will contain a non-epnty file path. This file path
141  /// should be then released using FS_FileResolver::relinquishFilePath().
142  ///
143  /// @param opts The options that describe the data.
144  /// @param name The name of the data available in the file.
145  /// @param file_path The output parameter which will have the
146  /// path of the file that contains the data. If
147  /// the request fails, the path will be an empty string.
148  virtual void acquireFilePath(
149  const FS_FileResolverOptions & opts,
150  const char * name,
151  UT_String & file_path ) = 0;
152 
153  /// Releases the file path and allows the resolver to (optionally) free
154  /// the file resources.
155  ///
156  /// @param file_path The file path previously obtained with
157  /// FS_FileResolver::acquireFilePath().
158  virtual void relinquishFilePath( const char * file_path ) = 0;
159 
160  /// Adds the data to the resolver, so that the resolver can handle
161  /// future requests for the data given this name.
162  ///
163  /// @param opts Options describe how the data should be interpreted.
164  ///
165  /// @param name The name of the resource. Future requests for this
166  /// resource using FS_FileResolver::newReader() will returno
167  /// a reader that provides this data.
168  ///
169  /// @param data The stream containing the data. The resolver will
170  /// read in and store this data for future use.
171  virtual void registerFile(const FS_FileResolverOptions &opts,
172  const UT_StringHolder &name,
173  UT_IStream & data );
174 
175  /// Returns an instance-unique identifier for a path that can be used to
176  /// check if the underlying resource data has changed, and should be
177  /// re-read by the clients of this class.
178 
179  /// @param name The name of the data to get the identifier for.
180  ///
181  /// @return The revision id of the data. Returns 0 if the resource
182  /// does not exist or revision IDs are not supported.
183  virtual int getRevisionID(const char *name) = 0;
184 protected:
185 
186  /// Contains details about the data that the resolver can provide.
188  {
189  public:
190  // constructor
192  const UT_StringHolder &file_name,
193  const FS_FileResolverOptions &opts);
194 
195  // Accessors.
196  const UT_StringHolder &getName() const { return myName; }
197  const UT_StringHolder &getFileName() const { return myFileName; }
198 
200  { return myOptions; }
201 
202  private:
203  /// Name of the resource this entry represents.
204  UT_StringHolder myName;
205 
206  /// The file name in which the resource data is saved.
207  UT_StringHolder myFileName;
208 
209  /// Options with which the file has been crated.
210  FS_FileResolverOptions myOptions;
211  };
212 
213 
214  /// Adds the file to the table.
215  /// The resolver takes ownership of the file and will delete it
216  /// in the destructor, unless it is unregistered from the table beforehand,
217  /// with FS_FileResolver::unregisterFile().
218  void registerFile(
219  const FS_FileResolverOptions &opts,
220  const UT_StringHolder &name,
221  const UT_StringHolder &file_path );
222 
223  /// Removes the file from table, and deletes the file if 'delete_file' is
224  /// true.
225  void unregisterFile( const UT_StringRef &name,
226  bool delete_file );
227 
228  /// Looks up the table entry for the given name key and returns the
229  /// entry. If not found, returns NULL.
230  FS_ResolverFileEntry * findFileInTable( const char * name );
231 
232 private:
233  /// Adds the file to the table.
234  void addFileToTable(const UT_StringHolder &name,
235  FS_ResolverFileEntry *entry);
236 
237  /// Removes the file entry from the table. If 'delete_file' is true,
238  /// the file on disk is also deleted.
239  void removeFileFromTable(const UT_StringRef &name,
240  bool delete_file);
241 
242 
243  /// Deletes the entry and any temporary files that hold the resource data.
244  /// If 'delete_file' is true, the file on disk is also deleted.
245  void deleteEntry(FS_ResolverFileEntry *entry,
246  bool delete_file );
247 
248 
249 private:
250  /// The current resolver.
251  static FS_FileResolver *theResolver;
252 
253  /// The table that maps the data name to the file containing that data.
255 };
256 
257 
258 //=============================================================================
259 /// Utility class for auto destruction and cleanup of the file resolver.
261 {
262 public:
263  /// Constructor
265 
266  /// Destructor. Deletes the resolver it holds (if not NULL), and if
267  /// it is the same as the current global resolver, it resets it to NULL.
269 
271 
272  /// Accessor for the resolver it holds.
273  FS_FileResolver * getResolver() const
274  { return myResolver; }
275 
276  /// Sets the resolver it cleans up after.
277  void setResolver( FS_FileResolver * resolver )
278  { myResolver = resolver; }
279 
280  /// Destroys the resolver right away rather than waiting for the
281  /// destructor to be invoked.
282  void destroyResolver();
283 
284 private:
285  // the resolver to clean up after
286  FS_FileResolver * myResolver;
287 
288 };
289 
290 #endif
291 
const UT_StringHolder & getFileExtension() const
Class for reading files.
Definition: FS_Reader.h:33
void setFileExtension(const UT_StringHolder &ext)
GT_API const UT_StringHolder time
void setTime(fpreal time)
Utility class for auto destruction and cleanup of the file resolver.
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
const UT_StringHolder & getFileName() const
GLuint const GLchar * name
Definition: glcorearb.h:786
fpreal64 fpreal
Definition: SYS_Types.h:277
Provides various options for creating and obtaining the file data.
void setResolver(FS_FileResolver *resolver)
Sets the resolver it cleans up after.
bool getIsEnvMap() const
Checks if the file should be treated as an environment map.
Contains details about the data that the resolver can provide.
void setIsEnvMap(bool env_map)
const UT_StringHolder & getName() const
Definition: format.h:895
#define FS_API
Definition: FS_API.h:10
fpreal getTime() const
Gets the time at which a node should be evaluated to obtain the data.
GLenum src
Definition: glcorearb.h:1793
const FS_FileResolverOptions & getOptions() const