Loading...
Searching...
No Matches
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"
39
40#include <map>
41#include <string>
42#include <vector>
43
44PXR_NAMESPACE_OPEN_SCOPE
45
46class ArAssetInfo;
47class SdfSchemaBase;
48class SdfLayerHints;
49
50SDF_DECLARE_HANDLES(SdfLayer);
51SDF_DECLARE_HANDLES(SdfSpec);
54
55#define SDF_FILE_FORMAT_TOKENS \
56 ((TargetArg, "target"))
57
58TF_DECLARE_PUBLIC_TOKENS(SdfFileFormatTokens, SDF_API, SDF_FILE_FORMAT_TOKENS);
59
65 : public TfRefBase
66 , public TfWeakBase
67{
68public:
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
137 SDF_API
138 SdfAbstractDataRefPtr InitDetachedData(
139 const FileFormatArguments& args) const;
140
142 SDF_API
143 SdfLayerRefPtr NewLayer(const SdfFileFormatConstPtr &fileFormat,
144 const std::string &identifier,
145 const std::string &realPath,
146 const ArAssetInfo& assetInfo,
147 const FileFormatArguments &args) const;
148
151 SDF_API bool ShouldSkipAnonymousReload() const;
152
165 SDF_API bool ShouldReadAnonymousLayers() const;
166
168 SDF_API
169 virtual bool CanRead(
170 const std::string& file) const = 0;
171
182 SDF_API
183 virtual bool Read(
184 SdfLayer* layer,
185 const std::string& resolvedPath,
186 bool metadataOnly) const = 0;
187
200 SDF_API
202 SdfLayer* layer,
203 const std::string& resolvedPath,
204 bool metadataOnly) const;
205
218 SDF_API
219 virtual bool WriteToFile(
220 const SdfLayer& layer,
221 const std::string& filePath,
222 const std::string& comment = std::string(),
223 const FileFormatArguments& args = FileFormatArguments()) const;
224
237 SDF_API
238 virtual bool SaveToFile(
239 const SdfLayer& layer,
240 const std::string& filePath,
241 const std::string& comment = std::string(),
242 const FileFormatArguments& args = FileFormatArguments()) const;
243
247 SDF_API
248 virtual bool ReadFromString(
249 SdfLayer* layer,
250 const std::string& str) const;
251
253 SDF_API
254 virtual bool WriteToStream(
255 const SdfSpecHandle &spec,
256 std::ostream& out,
257 size_t indent) const;
258
262 SDF_API
263 virtual bool WriteToString(
264 const SdfLayer& layer,
265 std::string* str,
266 const std::string& comment = std::string()) const;
267
280 SDF_API
281 virtual std::set<std::string> GetExternalAssetDependencies(
282 const SdfLayer& layer) const;
283
284
288 SDF_API bool SupportsReading() const;
289
290 // Returns true if this file format supports writing.
293 SDF_API bool SupportsWriting() const;
294
295 // Returns true if this file format supports editing.
298 SDF_API bool SupportsEditing() const;
299
302 SDF_API static std::string GetFileExtension(const std::string& s);
303
306 SDF_API static std::set<std::string> FindAllFileFormatExtensions();
307
312 SDF_API static std::set<std::string> FindAllDerivedFileFormatExtensions(
313 const TfType& baseType);
314
322 SDF_API
324 const std::string& extension,
325 const std::string& target = std::string());
326
334 SDF_API
336 const std::string& extension,
337 const std::string& target = std::string());
338
346 SDF_API
348 const std::string& extension,
349 const std::string& target = std::string());
350
354 SDF_API
355 static SdfFileFormatConstPtr FindById(
356 const TfToken& formatId);
357
367 SDF_API
368 static SdfFileFormatConstPtr FindByExtension(
369 const std::string& path,
370 const std::string& target = std::string());
371
378 SDF_API
379 static SdfFileFormatConstPtr FindByExtension(
380 const std::string& path,
381 const FileFormatArguments& args);
382
383protected:
386 const TfToken& formatId,
387 const TfToken& versionString,
388 const TfToken& target,
389 const std::string& extension);
390
394 const TfToken& formatId,
395 const TfToken& versionString,
396 const TfToken& target,
397 const std::string& extension,
398 const SdfSchemaBase& schema);
399
402 const TfToken& formatId,
403 const TfToken& versionString,
404 const TfToken& target,
405 const std::string& extension,
406 const SdfSchemaBase&& schema) = delete;
407
410 const TfToken& formatId,
411 const TfToken& versionString,
412 const TfToken& target,
413 const std::vector<std::string> &extensions);
414
418 const TfToken& formatId,
419 const TfToken& versionString,
420 const TfToken& target,
421 const std::vector<std::string> &extensions,
422 const SdfSchemaBase& schema);
423
426 const TfToken& formatId,
427 const TfToken& versionString,
428 const TfToken& target,
429 const std::vector<std::string> &extensions,
430 const SdfSchemaBase&& schema) = delete;
431
433 SDF_API virtual ~SdfFileFormat();
434
435 //
436 // Minimally break layer encapsulation with the following methods. These
437 // methods are also intended to limit the need for SdfLayer friendship with
438 // SdfFileFormat child classes.
439 //
440
445 SDF_API
446 static void _SetLayerData(
447 SdfLayer* layer, SdfAbstractDataRefPtr& data);
448
453 SDF_API
454 static void _SetLayerData(
455 SdfLayer* layer, SdfAbstractDataRefPtr& data,
456 SdfLayerHints hints);
457
459 SDF_API
460 static SdfAbstractDataConstPtr _GetLayerData(const SdfLayer& layer);
461
476 SDF_API
478 SdfLayer* layer,
479 const std::string& resolvedPath,
480 bool metadataOnly,
481 bool* didCopyData = nullptr) const;
482
483protected:
484 SDF_API
485 virtual SdfLayer *_InstantiateNewLayer(
486 const SdfFileFormatConstPtr &fileFormat,
487 const std::string &identifier,
488 const std::string &realPath,
489 const ArAssetInfo& assetInfo,
490 const FileFormatArguments &args) const;
491
492 // File format subclasses may override this if they prefer not to skip
493 // reloading anonymous layers. Default implementation returns true.
494 SDF_API
495 virtual bool _ShouldSkipAnonymousReload() const;
496
501 SDF_API
502 virtual bool _ShouldReadAnonymousLayers() const;
503
510 SDF_API
511 virtual SdfAbstractDataRefPtr _InitDetachedData(
512 const FileFormatArguments& args) const;
513
525 SDF_API
526 virtual bool _ReadDetached(
527 SdfLayer* layer,
528 const std::string& resolvedPath,
529 bool metadataOnly) const;
530
531private:
532 const SdfSchemaBase& _schema;
533 const TfToken _formatId;
534 const TfToken _target;
535 const std::string _cookie;
536 const TfToken _versionString;
537 const std::vector<std::string> _extensions;
538 const bool _isPrimaryFormat;
539};
540
541// Base file format factory.
542class Sdf_FileFormatFactoryBase : public TfType::FactoryBase {
543public:
544 SDF_API virtual ~Sdf_FileFormatFactoryBase();
545 virtual SdfFileFormatRefPtr New() const = 0;
546};
547
548// Default file format factory.
549template <typename T>
550class Sdf_FileFormatFactory : public Sdf_FileFormatFactoryBase {
551public:
552 virtual SdfFileFormatRefPtr New() const
553 {
554 return TfCreateRefPtr(new T);
555 }
556};
557
579#ifdef doxygen
580#define SDF_DEFINE_FILE_FORMAT(FileFormatClass, BaseClass1, ...)
581#else
582#define SDF_DEFINE_FILE_FORMAT(...) SdfDefineFileFormat<__VA_ARGS__>()
583
584template <class FileFormat, class ...BaseFormats>
585void SdfDefineFileFormat()
586{
587 TfType::Define<FileFormat, TfType::Bases<BaseFormats...>>()
588 .template SetFactory<Sdf_FileFormatFactory<FileFormat>>();
589}
590#endif // doxygen
591
613#ifdef doxygen
614#define SDF_DEFINE_ABSTRACT_FILE_FORMAT(FileFormatClass, BaseClass1, ...)
615#else
616#define SDF_DEFINE_ABSTRACT_FILE_FORMAT(...) \
617 SdfDefineAbstractFileFormat<__VA_ARGS__>()
618
619template <class FileFormat, class ...BaseFormats>
620void SdfDefineAbstractFileFormat()
621{
622 TfType::Define<FileFormat, TfType::Bases<BaseFormats...>>();
623}
624#endif //doxygen
625
643#ifdef doxygen
644#define SDF_FILE_FORMAT_FACTORY_ACCESS
645#else
646#define SDF_FILE_FORMAT_FACTORY_ACCESS \
647 template<typename T> friend class Sdf_FileFormatFactory
648#endif //doxygen
649
650PXR_NAMESPACE_CLOSE_SCOPE
651
652#endif
Contains information about a resolved asset.
Definition: assetInfo.h:42
Interface for scene description data storage.
Definition: abstractData.h:74
Base class for file format implementations.
Definition: fileFormat.h:67
SdfFileFormat(const TfToken &formatId, const TfToken &versionString, const TfToken &target, const std::string &extension, const SdfSchemaBase &&schema)=delete
Disallow temporary SdfSchemaBase objects being passed to the c'tor.
static SDF_API SdfAbstractDataConstPtr _GetLayerData(const SdfLayer &layer)
Get the internal data for layer.
virtual SDF_API bool _ShouldReadAnonymousLayers() const
File format subclasses may override this to specify whether Read should be called when creating,...
SDF_API bool ReadDetached(SdfLayer *layer, const std::string &resolvedPath, bool metadataOnly) const
Reads scene description from the asset specified by resolvedPath into the detached layer layer.
SDF_API const TfToken & GetVersionString() const
Returns the current version of this file format.
std::map< std::string, std::string > FileFormatArguments
Type for specifying additional file format-specific arguments to the various API below.
Definition: fileFormat.h:119
SDF_API const TfToken & GetTarget() const
Returns the target for this file format.
SDF_API SdfFileFormat(const TfToken &formatId, const TfToken &versionString, const TfToken &target, const std::string &extension)
Constructor.
SDF_API bool SupportsWriting() const
This is a convenience method for invoking FormatSupportsWriting with this format's extension and targ...
virtual SDF_API bool SaveToFile(const SdfLayer &layer, const std::string &filePath, const std::string &comment=std::string(), const FileFormatArguments &args=FileFormatArguments()) const
Write the content in layer to the file at filePath, which is the backing store for layer itself.
SDF_API const SdfSchemaBase & GetSchema() const
Returns the schema for this format.
SDF_API const std::string & GetFileCookie() const
Returns the cookie to be used when writing files with this format.
static SDF_API void _SetLayerData(SdfLayer *layer, SdfAbstractDataRefPtr &data, SdfLayerHints hints)
Set the internal data for layer to data, possibly transferring ownership of data.
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 WriteToStream(const SdfSpecHandle &spec, std::ostream &out, size_t indent) const
Write the provided spec to out indented indent levels.
SDF_API bool _ReadAndCopyLayerDataToMemory(SdfLayer *layer, const std::string &resolvedPath, bool metadataOnly, bool *didCopyData=nullptr) const
Helper function for _ReadDetached.
SDF_API bool ShouldReadAnonymousLayers() const
Returns true if anonymous layer identifiers should be passed to Read when a layer is opened or reload...
SDF_API bool SupportsReading() const
Returns true if this file format supports reading.
virtual SDF_API bool IsPackage() const
Returns true if this file format is a package containing other assets.
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 ~SdfFileFormat()
Destructor.
static SDF_API SdfFileFormatConstPtr FindById(const TfToken &formatId)
Returns the file format instance with the specified formatId identifier.
SDF_API SdfFileFormat(const TfToken &formatId, const TfToken &versionString, const TfToken &target, const std::string &extension, const SdfSchemaBase &schema)
Constructor.
SdfFileFormat(const TfToken &formatId, const TfToken &versionString, const TfToken &target, const std::vector< std::string > &extensions, const SdfSchemaBase &&schema)=delete
Disallow temporary SdfSchemaBase objects being passed to the c'tor.
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.
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 FileFormatArguments GetDefaultFileFormatArguments() const
Returns the FileFormatArguments that correspond to the default behavior of this file format when no F...
SDF_API bool SupportsEditing() const
This is a convenience method for invoking FormatSupportsEditing with this format's extension and targ...
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.
static SDF_API void _SetLayerData(SdfLayer *layer, SdfAbstractDataRefPtr &data)
Set the internal data for layer to data, possibly transferring ownership of data.
virtual SDF_API bool _ReadDetached(SdfLayer *layer, const std::string &resolvedPath, bool metadataOnly) const
static SDF_API std::set< std::string > FindAllFileFormatExtensions()
Returns a set containing the extension(s) corresponding to all registered file formats.
static SDF_API std::set< std::string > FindAllDerivedFileFormatExtensions(const TfType &baseType)
Returns a set containing the extension(s) corresponding to all registered file formats that derive fr...
virtual SDF_API SdfAbstractDataRefPtr _InitDetachedData(const FileFormatArguments &args) const
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 std::string GetFileExtension(const std::string &s)
Returns the file extension for path or file name s, without the leading dot character.
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 & GetFormatId() const
Returns the format identifier.
SDF_API const std::vector< std::string > & GetFileExtensions() const
Returns a list of extensions that this format supports.
static SDF_API bool FormatSupportsEditing(const std::string &extension, const std::string &target=std::string())
Returns true if the file format for the supplied extension and target pair supports editing.
SDF_API const std::string & GetPrimaryFileExtension() const
Returns the primary file extension for this format.
static SDF_API SdfFileFormatConstPtr FindByExtension(const std::string &path, const FileFormatArguments &args)
Returns a file format instance that supports the extension for path and whose target matches one of t...
SDF_API bool IsPrimaryFormatForExtensions() const
Returns true if this file format is the primary format for the extensions it handles.
static SDF_API bool FormatSupportsReading(const std::string &extension, const std::string &target=std::string())
Returns true if the file format for the supplied extension and target pair supports reading.
SDF_API SdfAbstractDataRefPtr InitDetachedData(const FileFormatArguments &args) const
Returns a new SdfAbstractData providing access to the layer's data.
static SDF_API bool FormatSupportsWriting(const std::string &extension, const std::string &target=std::string())
Returns true if the file format for the supplied extension and target pair supports writing.
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 ...
SDF_API bool IsSupportedExtension(const std::string &extension) const
Returns true if extension matches one of the extensions returned by GetFileExtensions.
SDF_API bool ShouldSkipAnonymousReload() const
Return true if this file format prefers to skip reloading anonymous layers.
SDF_API SdfFileFormat(const TfToken &formatId, const TfToken &versionString, const TfToken &target, const std::vector< std::string > &extensions, const SdfSchemaBase &schema)
Constructor.
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.
SDF_API SdfFileFormat(const TfToken &formatId, const TfToken &versionString, const TfToken &target, const std::vector< std::string > &extensions)
Constructor.
virtual SDF_API bool CanRead(const std::string &file) const =0
Returns true if file can be read by this format.
Contains hints about layer contents that may be used to accelerate certain composition operations.
Definition: layerHints.h:34
A scene description container that can combine with other such containers to form simple component as...
Definition: layer.h:100
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:50
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:73
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Base class of all factory types.
Definition: type.h:73
TfType represents a dynamic runtime type.
Definition: type.h:65
static TfType const & Define()
Define a TfType with the given C++ type T and C++ base types B.
A type-list of C++ base types.
Definition: type.h:100
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
Standard pointer typedefs.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:98
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...