fileFormat.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_SDF_FILE_FORMAT_H
25 #define PXR_USD_SDF_FILE_FORMAT_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/ar/ar.h"
31 #include "pxr/usd/sdf/api.h"
34 #include "pxr/base/tf/refBase.h"
36 #include "pxr/base/tf/token.h"
37 #include "pxr/base/tf/type.h"
38 #include "pxr/base/tf/weakBase.h"
39 
40 #include <map>
41 #include <string>
42 #include <vector>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 class ArAssetInfo;
47 class SdfSchemaBase;
48 class SdfLayerHints;
49 
50 SDF_DECLARE_HANDLES(SdfLayer);
51 SDF_DECLARE_HANDLES(SdfSpec);
54 
55 #define SDF_FILE_FORMAT_TOKENS \
56  ((TargetArg, "target"))
57 
58 TF_DECLARE_PUBLIC_TOKENS(SdfFileFormatTokens, SDF_API, SDF_FILE_FORMAT_TOKENS);
59 
65  : public TfRefBase
66  , public TfWeakBase
67 {
68 public:
69  SdfFileFormat(const SdfFileFormat&) = delete;
70  SdfFileFormat& operator=(const SdfFileFormat&) = delete;
71 
73  SDF_API const SdfSchemaBase& GetSchema() const;
74 
76  SDF_API const TfToken& GetFormatId() const;
77 
79  SDF_API const TfToken& GetTarget() const;
80 
82  SDF_API const std::string& GetFileCookie() const;
83 
85  SDF_API const TfToken& GetVersionString() const;
86 
89  SDF_API bool IsPrimaryFormatForExtensions() const;
90 
92  SDF_API const std::vector<std::string>& GetFileExtensions() const;
93 
96  SDF_API const std::string& GetPrimaryFileExtension() const;
97 
100  SDF_API bool IsSupportedExtension(const std::string& extension) const;
101 
104  SDF_API
105  virtual bool IsPackage() const;
106 
113  SDF_API
114  virtual std::string GetPackageRootLayerPath(
115  const std::string& resolvedPath) const;
116 
119  typedef std::map<std::string, std::string> FileFormatArguments;
120 
124  SDF_API
126 
131  SDF_API
132  virtual SdfAbstractDataRefPtr
133  InitData(const FileFormatArguments& args) const;
134 
136  SDF_API
137  SdfLayerRefPtr NewLayer(const SdfFileFormatConstPtr &fileFormat,
138  const std::string &identifier,
139  const std::string &realPath,
140  const ArAssetInfo& assetInfo,
141  const FileFormatArguments &args) const;
142 
145  SDF_API bool ShouldSkipAnonymousReload() const;
146 
159  SDF_API bool ShouldReadAnonymousLayers() const;
160 
162  SDF_API
163  virtual bool CanRead(
164  const std::string& file) const = 0;
165 
176  SDF_API
177  virtual bool Read(
178  SdfLayer* layer,
179  const std::string& resolvedPath,
180  bool metadataOnly) const = 0;
181 
186  SDF_API
187  virtual bool WriteToFile(
188  const SdfLayer& layer,
189  const std::string& filePath,
190  const std::string& comment = std::string(),
191  const FileFormatArguments& args = FileFormatArguments()) const;
192 
196  SDF_API
197  virtual bool ReadFromString(
198  SdfLayer* layer,
199  const std::string& str) const;
200 
202  SDF_API
203  virtual bool WriteToStream(
204  const SdfSpecHandle &spec,
205  std::ostream& out,
206  size_t indent) const;
207 
211  SDF_API
212  virtual bool WriteToString(
213  const SdfLayer& layer,
214  std::string* str,
215  const std::string& comment = std::string()) const;
216 
229  SDF_API
230  virtual std::set<std::string> GetExternalAssetDependencies(
231  const SdfLayer& layer) const;
232 
235  SDF_API static std::string GetFileExtension(const std::string& s);
236 
239  SDF_API static std::set<std::string> FindAllFileFormatExtensions();
240 
244  SDF_API
245  static SdfFileFormatConstPtr FindById(
246  const TfToken& formatId);
247 
257  SDF_API
258  static SdfFileFormatConstPtr FindByExtension(
259  const std::string& path,
260  const std::string& target = std::string());
261 
268  SDF_API
269  static SdfFileFormatConstPtr FindByExtension(
270  const std::string& path,
271  const FileFormatArguments& args);
272 
273 protected:
275  SDF_API SdfFileFormat(
276  const TfToken& formatId,
277  const TfToken& versionString,
278  const TfToken& target,
279  const std::string& extension);
280 
283  SDF_API SdfFileFormat(
284  const TfToken& formatId,
285  const TfToken& versionString,
286  const TfToken& target,
287  const std::string& extension,
288  const SdfSchemaBase& schema);
289 
292  const TfToken& formatId,
293  const TfToken& versionString,
294  const TfToken& target,
295  const std::string& extension,
296  const SdfSchemaBase&& schema) = delete;
297 
299  SDF_API SdfFileFormat(
300  const TfToken& formatId,
301  const TfToken& versionString,
302  const TfToken& target,
303  const std::vector<std::string> &extensions);
304 
307  SDF_API SdfFileFormat(
308  const TfToken& formatId,
309  const TfToken& versionString,
310  const TfToken& target,
311  const std::vector<std::string> &extensions,
312  const SdfSchemaBase& schema);
313 
316  const TfToken& formatId,
317  const TfToken& versionString,
318  const TfToken& target,
319  const std::vector<std::string> &extensions,
320  const SdfSchemaBase&& schema) = delete;
321 
323  SDF_API virtual ~SdfFileFormat();
324 
325  //
326  // Minimally break layer encapsulation with the following methods. These
327  // methods are also intended to limit the need for SdfLayer friendship with
328  // SdfFileFormat child classes.
329  //
330 
335  SDF_API
336  static void _SetLayerData(
337  SdfLayer* layer, SdfAbstractDataRefPtr& data);
338 
343  SDF_API
344  static void _SetLayerData(
345  SdfLayer* layer, SdfAbstractDataRefPtr& data,
346  SdfLayerHints hints);
347 
349  SDF_API
350  static SdfAbstractDataConstPtr _GetLayerData(const SdfLayer& layer);
351 
352 protected:
353  SDF_API
354  virtual SdfLayer *_InstantiateNewLayer(
355  const SdfFileFormatConstPtr &fileFormat,
356  const std::string &identifier,
357  const std::string &realPath,
358  const ArAssetInfo& assetInfo,
359  const FileFormatArguments &args) const;
360 
361  // File format subclasses may override this if they prefer not to skip
362  // reloading anonymous layers. Default implementation returns true.
363  SDF_API
364  virtual bool _ShouldSkipAnonymousReload() const;
365 
370  SDF_API
371  virtual bool _ShouldReadAnonymousLayers() const;
372 
373 private:
374  const SdfSchemaBase& _schema;
375  const TfToken _formatId;
376  const TfToken _target;
377  const std::string _cookie;
378  const TfToken _versionString;
379  const std::vector<std::string> _extensions;
380  const bool _isPrimaryFormat;
381 };
382 
383 // Base file format factory.
384 class Sdf_FileFormatFactoryBase : public TfType::FactoryBase {
385 public:
386  SDF_API virtual ~Sdf_FileFormatFactoryBase();
387  virtual SdfFileFormatRefPtr New() const = 0;
388 };
389 
390 // Default file format factory.
391 template <typename T>
392 class Sdf_FileFormatFactory : public Sdf_FileFormatFactoryBase {
393 public:
394  virtual SdfFileFormatRefPtr New() const
395  {
396  return TfCreateRefPtr(new T);
397  }
398 };
399 
421 #ifdef doxygen
422 #define SDF_DEFINE_FILE_FORMAT(FileFormatClass, BaseClass1, ...)
423 #else
424 #define SDF_DEFINE_FILE_FORMAT(...) SdfDefineFileFormat<__VA_ARGS__>()
425 
426 template <class FileFormat, class ...BaseFormats>
427 void SdfDefineFileFormat()
428 {
429  TfType::Define<FileFormat, TfType::Bases<BaseFormats...>>()
430  .template SetFactory<Sdf_FileFormatFactory<FileFormat>>();
431 }
432 #endif // doxygen
433 
455 #ifdef doxygen
456 #define SDF_DEFINE_ABSTRACT_FILE_FORMAT(FileFormatClass, BaseClass1, ...)
457 #else
458 #define SDF_DEFINE_ABSTRACT_FILE_FORMAT(...) \
459  SdfDefineAbstractFileFormat<__VA_ARGS__>()
460 
461 template <class FileFormat, class ...BaseFormats>
462 void SdfDefineAbstractFileFormat()
463 {
464  TfType::Define<FileFormat, TfType::Bases<BaseFormats...>>();
465 }
466 #endif //doxygen
467 
485 #ifdef doxygen
486 #define SDF_FILE_FORMAT_FACTORY_ACCESS
487 #else
488 #define SDF_FILE_FORMAT_FACTORY_ACCESS \
489  template<typename T> friend class Sdf_FileFormatFactory
490 #endif //doxygen
491 
492 PXR_NAMESPACE_CLOSE_SCOPE
493 
494 #endif
SDF_API const TfToken & GetTarget() const
Returns the target for this file format.
A scene description container that can combine with other such containers to form simple component as...
Definition: layer.h:94
static SDF_API std::set< std::string > FindAllFileFormatExtensions()
Returns a set containing the extension(s) corresponding to all registered file formats.
virtual SDF_API std::string GetPackageRootLayerPath(const std::string &resolvedPath) const
Returns the path of the "root" layer contained in the package layer at resolvedPath produced by this ...
Standard pointer typedefs.
virtual SDF_API bool CanRead(const std::string &file) const =0
Returns true if file can be read by this format.
SDF_API const std::vector< std::string > & GetFileExtensions() const
Returns a list of extensions that this format supports.
SDF_API bool IsPrimaryFormatForExtensions() const
Returns true if this file format is the primary format for the extensions it handles.
SDF_API SdfLayerRefPtr NewLayer(const SdfFileFormatConstPtr &fileFormat, const std::string &identifier, const std::string &realPath, const ArAssetInfo &assetInfo, const FileFormatArguments &args) const
Instantiate a layer.
Generic class that provides information about scene description fields but doesn't actually provide a...
Definition: schema.h:62
Base class for all Sdf spec classes.
Definition: spec.h:51
SDF_API const TfToken & GetFormatId() const
Returns the format identifier.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
virtual SDF_API bool WriteToStream(const SdfSpecHandle &spec, std::ostream &out, size_t indent) const
Write the provided spec to out indented indent levels.
Base class of all factory types.
Definition: type.h:73
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:71
SDF_API const std::string & GetPrimaryFileExtension() const
Returns the primary file extension for this format.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
virtual SDF_API bool Read(SdfLayer *layer, const std::string &resolvedPath, bool metadataOnly) const =0
Reads scene description from the asset specified by resolvedPath into the layer layer.
virtual SDF_API bool IsPackage() const
Returns true if this file format is a package containing other assets.
static SDF_API SdfFileFormatConstPtr FindByExtension(const std::string &path, const std::string &target=std::string())
Returns the file format instance that supports the extension for path.
static SDF_API SdfFileFormatConstPtr FindById(const TfToken &formatId)
Returns the file format instance with the specified formatId identifier.
SDF_API bool IsSupportedExtension(const std::string &extension) const
Returns true if extension matches one of the extensions returned by GetFileExtensions.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
virtual SDF_API bool _ShouldReadAnonymousLayers() const
File format subclasses may override this to specify whether Read should be called when creating,...
A type-list of C++ base types.
Definition: type.h:100
virtual SDF_API ~SdfFileFormat()
Destructor.
SDF_API bool ShouldSkipAnonymousReload() const
Return true if this file format prefers to skip reloading anonymous layers.
virtual SDF_API FileFormatArguments GetDefaultFileFormatArguments() const
Returns the FileFormatArguments that correspond to the default behavior of this file format when no F...
std::map< std::string, std::string > FileFormatArguments
Type for specifying additional file format-specific arguments to the various API below.
Definition: fileFormat.h:119
static TfType const & Define()
Define a TfType with the given C++ type T and C++ base types B.
Definition: type_Impl.h:90
SDF_API bool ShouldReadAnonymousLayers() const
Returns true if anonymous layer identifiers should be passed to Read when a layer is opened or reload...
static SDF_API void _SetLayerData(SdfLayer *layer, SdfAbstractDataRefPtr &data)
Set the internal data for layer to data, possibly transferring ownership of data.
static SDF_API std::string GetFileExtension(const std::string &s)
Returns the file extension for path or file name s, without the leading dot character.
This file defines some macros that are useful for declaring and using static TfTokens.
virtual SDF_API bool ReadFromString(SdfLayer *layer, const std::string &str) const
Reads data in the string str into the layer layer.
virtual SDF_API SdfAbstractDataRefPtr InitData(const FileFormatArguments &args) const
This method allows the file format to bind to whatever data container is appropriate.
virtual SDF_API bool WriteToString(const SdfLayer &layer, std::string *str, const std::string &comment=std::string()) const
Writes the content in layer to the string str.
virtual SDF_API bool WriteToFile(const SdfLayer &layer, const std::string &filePath, const std::string &comment=std::string(), const FileFormatArguments &args=FileFormatArguments()) const
Writes the content in layer into the file at filePath.
Contains hints about layer contents that may be used to accelerate certain composition operations.
Definition: layerHints.h:33
virtual SDF_API std::set< std::string > GetExternalAssetDependencies(const SdfLayer &layer) const
Returns the set of resolved paths to external asset file dependencies for the given layer.
SDF_API const TfToken & GetVersionString() const
Returns the current version of this file format.
SDF_API const std::string & GetFileCookie() const
Returns the cookie to be used when writing files with this format.
static SDF_API SdfAbstractDataConstPtr _GetLayerData(const SdfLayer &layer)
Get the internal data for layer.
Base class for file format implementations.
Definition: fileFormat.h:64
SDF_API const SdfSchemaBase & GetSchema() const
Returns the schema for this format.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
Interface for scene description data storage.
Definition: abstractData.h:73
Contains information about a resolved asset.
Definition: assetInfo.h:40