All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
resolverContext.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 AR_RESOLVER_CONTEXT_H
25 #define AR_RESOLVER_CONTEXT_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/ar/api.h"
31 #include "pxr/base/tf/safeTypeCompare.h"
32 
33 #include <boost/utility/enable_if.hpp>
34 
35 #include <memory>
36 #include <string>
37 #include <typeinfo>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
46 template <class T>
48 {
49  static const bool value = false;
50 };
51 
53 template <class Context>
54 std::string ArGetDebugString(const Context& context);
55 
90 {
91 public:
94  {
95  }
96 
99  template <class Context>
101  const Context& context,
102  typename boost::enable_if<ArIsContextObject<Context> >::type* = 0)
103  : _context(new _Typed<Context>(context))
104  {
105  }
106 
108  bool IsEmpty() const
109  {
110  return !_context;
111  }
112 
116  template <class Context>
117  const Context* Get() const
118  {
119  return _context && _context->IsHolding(typeid(Context)) ?
120  &_GetTyped<Context>(*_context)._context : NULL;
121  }
122 
124  std::string GetDebugString() const
125  {
126  return _context ? _context->GetDebugString() : std::string();
127  }
128 
131  bool operator==(const ArResolverContext& rhs) const
132  {
133  if (_context && rhs._context) {
134  return (_context->IsHolding(rhs._context->GetTypeid())
135  && _context->Equals(*rhs._context));
136  }
137  return (!_context && !rhs._context);
138  }
139 
140  bool operator!=(const ArResolverContext& rhs) const
141  {
142  return !(*this == rhs);
143  }
144 
145  bool operator<(const ArResolverContext& rhs) const
146  {
147  if (_context && rhs._context) {
148  if (_context->IsHolding(rhs._context->GetTypeid())) {
149  return _context->LessThan(*rhs._context);
150  }
151  return (std::string(_context->GetTypeid().name()) <
152  std::string(rhs._context->GetTypeid().name()));
153  }
154  else if (_context && !rhs._context) {
155  return false;
156  }
157  else if (!_context && rhs._context) {
158  return true;
159  }
160  return false;
161  }
162 
164 
166  friend size_t hash_value(const ArResolverContext& context)
167  {
168  return context._context ? context._context->Hash() : 0;
169  }
170 
171 private:
172  // Type-erased storage for context objects.
173  struct _Untyped;
174  template <class Context> struct _Typed;
175 
176  template <class Context>
177  static const _Typed<Context>& _GetTyped(const _Untyped& untyped)
178  {
179  return static_cast<const _Typed<Context>&>(untyped);
180  }
181 
182  struct _Untyped
183  {
184  AR_API
185  virtual ~_Untyped();
186 
187  bool IsHolding(const std::type_info& ti) const
188  {
189  return TfSafeTypeCompare(ti, GetTypeid());
190  }
191 
192  virtual const std::type_info& GetTypeid() const = 0;
193  virtual bool LessThan(const _Untyped& rhs) const = 0;
194  virtual bool Equals(const _Untyped& rhs) const = 0;
195  virtual size_t Hash() const = 0;
196  virtual std::string GetDebugString() const = 0;
197  };
198 
199  template <class Context>
200  struct _Typed : public _Untyped
201  {
202  virtual ~_Typed() { }
203 
204  _Typed(const Context& context) : _context(context)
205  {
206  }
207 
208  virtual const std::type_info& GetTypeid() const
209  {
210  return typeid(Context);
211  }
212 
213  virtual bool LessThan(const _Untyped& rhs) const
214  {
215  return _context < _GetTyped<Context>(rhs)._context;
216  }
217 
218  virtual bool Equals(const _Untyped& rhs) const
219  {
220  return _context == _GetTyped<Context>(rhs)._context;
221  }
222 
223  virtual size_t Hash() const
224  {
225  return hash_value(_context);
226  }
227 
228  virtual std::string GetDebugString() const
229  {
230  return ArGetDebugString(_context);
231  }
232 
233  Context _context;
234  };
235 
236  std::shared_ptr<_Untyped> _context;
237 };
238 
239 
240 // Default implementation for streaming out held contexts.
241 AR_API
242 std::string Ar_GetDebugString(const std::type_info&, void const*);
243 
244 template <class Context>
245 std::string ArGetDebugString(const Context& context)
246 {
247  return Ar_GetDebugString(typeid(Context),
248  static_cast<void const*>(&context));
249 }
250 
251 PXR_NAMESPACE_CLOSE_SCOPE
252 
253 #endif // AR_RESOLVER_CONTEXT_H
ArResolverContext(const Context &context, typename boost::enable_if< ArIsContextObject< Context > >::type *=0)
Construct a resolver context using the context object context.
friend size_t hash_value(const ArResolverContext &context)
Returns hash value for this asset resolver context.
ArResolverContext()
Construct an empty asset resolver context.
bool IsEmpty() const
Returns whether this context object is empty.
std::string ArGetDebugString(const Context &context)
Default implementation for providing debug info on the contained context.
Metafunction to determine whether the templated object type is a valid context object.
An asset resolver context allows clients to provide additional data to the resolver for use during re...
const Context * Get() const
Return pointer to the context object held in this asset resolver context if the context is holding an...
std::string GetDebugString() const
Returns a debug string representing the contained context.
bool TfSafeTypeCompare(const std::type_info &t1, const std::type_info &t2)
Safely compare std::type_info structures.