All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
staticInterface.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 PLUG_STATICINTERFACE_H
25 #define PLUG_STATICINTERFACE_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/base/plug/api.h"
31 
32 #include <atomic>
33 #include <type_traits>
34 #include <typeinfo>
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 // Base class for common stuff.
39 class Plug_StaticInterfaceBase {
40 public:
44  bool IsInitialized() const
45  {
46  return _initialized;
47  }
48 
49 #if !defined(doxygen)
50  typedef void* Plug_StaticInterfaceBase::*UnspecifiedBoolType;
51 #endif
52 
53 protected:
54  PLUG_API
55  void _LoadAndInstantiate(const std::type_info& type) const;
56 
57 protected:
58  // POD types only!
59  mutable std::atomic<bool> _initialized;
60  mutable void* _ptr;
61 };
62 
159 template <class Interface>
160 class PlugStaticInterface : private Plug_StaticInterfaceBase {
161 public:
162  static_assert(std::is_abstract<Interface>::value,
163  "Interface type must be abstract.");
164 
166 
167  using Plug_StaticInterfaceBase::IsInitialized;
168 
171  operator UnspecifiedBoolType() const
172  {
173  return _GetPtr() ? &This::_ptr : nullptr;
174  }
175 
178  bool operator!() const
179  {
180  return !*this;
181  }
182 
185  Interface* Get() const
186  {
187  return _GetPtr();
188  }
189 
192  Interface* operator->() const
193  {
194  return _GetPtr();
195  }
196 
200  Interface& operator*() const
201  {
202  return *_GetPtr();
203  }
204 
205 private:
206  Interface* _GetPtr() const
207  {
208  if (!_initialized) {
209  _LoadAndInstantiate(typeid(Interface));
210  }
211 
212  // XXX: We must assume _ptr has the right type since we have
213  // no common base class to dynamic_cast<> from.
214  return static_cast<Interface*>(_ptr);
215  }
216 };
217 
218 PXR_NAMESPACE_CLOSE_SCOPE
219 
220 #endif // PLUG_STATICINTERFACE_H
Interface * operator->() const
Returns the interface pointer, loading the plugin if necessary.
Provides access to an interface into a plugin.
bool operator!() const
Load and instantiate then return false if the interface is valid, true otherwise. ...
Interface * Get() const
Returns the interface pointer, loading the plugin if necessary.
Interface & operator*() const
Returns the interface pointer as a reference, loading the plugin if necessary.