All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fileFormatRegistry.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 SDF_FILE_FORMAT_REGISTRY_H
25 #define SDF_FILE_FORMAT_REGISTRY_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/base/tf/declarePtrs.h"
31 #include "pxr/base/tf/hash.h"
32 #include "pxr/base/tf/hashmap.h"
33 #include "pxr/base/tf/token.h"
34 #include "pxr/base/tf/type.h"
35 #include "pxr/base/tf/weakBase.h"
36 #include <boost/noncopyable.hpp>
37 #include <boost/shared_ptr.hpp>
38 #include <atomic>
39 #include <mutex>
40 #include <vector>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
46 
53 class Sdf_FileFormatRegistry : boost::noncopyable
54 {
55 public:
57  Sdf_FileFormatRegistry();
58 
60  SdfFileFormatConstPtr FindById(const TfToken& formatId);
61 
65  SdfFileFormatConstPtr FindByExtension(
66  const std::string& s,
67  const std::string& target = std::string());
68 
71  std::set<std::string> FindAllFileFormatExtensions();
72 
75  TfToken GetPrimaryFormatForExtension(const std::string& ext);
76 
77 private:
85  class _Info {
86  public:
87  _Info(const TfToken& formatId,
88  const TfType& type,
89  const TfToken& target,
90  const PlugPluginPtr& plugin)
91  : formatId(formatId)
92  , type(type)
93  , target(target)
94  , _plugin(plugin)
95  , _hasFormat(false)
96  { }
97 
98  // Return this _Info's file format
99  SdfFileFormatRefPtr GetFileFormat() const;
100 
101  const TfToken formatId;
102  const TfType type;
103  const TfToken target;
104 
105  private:
106  const PlugPluginPtr _plugin;
107  mutable std::mutex _formatMutex;
108  mutable std::atomic<bool> _hasFormat;
109  mutable SdfFileFormatRefPtr _format;
110  };
111 
112  typedef boost::shared_ptr<_Info> _InfoSharedPtr;
113  typedef std::vector<_InfoSharedPtr> _InfoSharedPtrVector;
114 
115  // 1-to-1 mapping from file format Id -> file format info
116  typedef TfHashMap<
117  TfToken, _InfoSharedPtr, TfToken::HashFunctor> _FormatInfo;
118 
119  // many-to-1 mapping from file extension -> file format info for primary
120  // format. Each file extension must have one primary file format plugin,
121  // but a file format plugin may be the primary one for multiple extensions.
122  typedef TfHashMap<
123  std::string, _InfoSharedPtr, TfHash> _ExtensionIndex;
124 
125  // many-to-many mapping from file extensions -> file format info
126  // A file with a given extension may be supported by any number of file
127  // formats plugins.
128  typedef TfHashMap<
129  std::string, _InfoSharedPtrVector, TfHash> _FullExtensionIndex;
130 
131  // Populates the _formatInfo structure if it is empty. This causes plugin
132  // discovery to run, but does not load any plugins.
133  void _RegisterFormatPlugins();
134 
135  // Given information about a file format plugin in \p format, load the
136  // associated plugin, instantiate the format, cache the instance and
137  // return it.
138  SdfFileFormatConstPtr _GetFileFormat(const _InfoSharedPtr& format);
139 
140  _FormatInfo _formatInfo;
141  _ExtensionIndex _extensionIndex;
142  _FullExtensionIndex _fullExtensionIndex;
143 
144  std::atomic<bool> _registeredFormatPlugins;
145  std::mutex _mutex;
146 };
147 
148 PXR_NAMESPACE_CLOSE_SCOPE
149 
150 #endif // SDF_FILE_FORMAT_REGISTRY_H
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:62
Functor to use for hash maps from tokens to other things.
Definition: token.h:168
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
Provides hash function on STL string types and other types.
Definition: hash.h:86
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Defines an interface to registered plugins.
Definition: plugin.h:59
TfType represents a dynamic runtime type.
Definition: type.h:70
Base class for file format implementations.
Definition: fileFormat.h:63