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 PXR_USD_SDF_FILE_FORMAT_REGISTRY_H
25 #define PXR_USD_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 
66  SdfFileFormatConstPtr FindByExtension(
67  const std::string& s,
68  const std::string& target = std::string());
69 
72  std::set<std::string> FindAllFileFormatExtensions();
73 
76  TfToken GetPrimaryFormatForExtension(const std::string& ext);
77 
78 private:
86  class _Info {
87  public:
88  _Info(const TfToken& formatId,
89  const TfType& type,
90  const TfToken& target,
91  const PlugPluginPtr& plugin)
92  : formatId(formatId)
93  , type(type)
94  , target(target)
95  , _plugin(plugin)
96  , _hasFormat(false)
97  { }
98 
99  // Return this _Info's file format
100  SdfFileFormatRefPtr GetFileFormat() const;
101 
102  const TfToken formatId;
103  const TfType type;
104  const TfToken target;
105 
106  private:
107  const PlugPluginPtr _plugin;
108  mutable std::mutex _formatMutex;
109  mutable std::atomic<bool> _hasFormat;
110  mutable SdfFileFormatRefPtr _format;
111  };
112 
113  typedef boost::shared_ptr<_Info> _InfoSharedPtr;
114  typedef std::vector<_InfoSharedPtr> _InfoSharedPtrVector;
115 
116  // 1-to-1 mapping from file format Id -> file format info
117  typedef TfHashMap<
118  TfToken, _InfoSharedPtr, TfToken::HashFunctor> _FormatInfo;
119 
120  // many-to-1 mapping from file extension -> file format info for primary
121  // format. Each file extension must have one primary file format plugin,
122  // but a file format plugin may be the primary one for multiple extensions.
123  typedef TfHashMap<
124  std::string, _InfoSharedPtr, TfHash> _ExtensionIndex;
125 
126  // many-to-many mapping from file extensions -> file format info
127  // A file with a given extension may be supported by any number of file
128  // formats plugins.
129  typedef TfHashMap<
130  std::string, _InfoSharedPtrVector, TfHash> _FullExtensionIndex;
131 
132  // Populates the _formatInfo structure if it is empty. This causes plugin
133  // discovery to run, but does not load any plugins.
134  void _RegisterFormatPlugins();
135 
136  // Given information about a file format plugin in \p format, load the
137  // associated plugin, instantiate the format, cache the instance and
138  // return it.
139  SdfFileFormatConstPtr _GetFileFormat(const _InfoSharedPtr& format);
140 
141  _FormatInfo _formatInfo;
142  _ExtensionIndex _extensionIndex;
143  _FullExtensionIndex _fullExtensionIndex;
144 
145  std::atomic<bool> _registeredFormatPlugins;
146  std::mutex _mutex;
147 };
148 
149 PXR_NAMESPACE_CLOSE_SCOPE
150 
151 #endif // PXR_USD_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:166
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:447
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Defines an interface to registered plugins.
Definition: plugin.h:58
TfType represents a dynamic runtime type.
Definition: type.h:64
Base class for file format implementations.
Definition: fileFormat.h:64