HDK
 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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_PLUG_STATIC_INTERFACE_H
8 #define PXR_BASE_PLUG_STATIC_INTERFACE_H
9 
10 /// \file plug/staticInterface.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/plug/api.h"
14 
15 #include <atomic>
16 #include <type_traits>
17 #include <typeinfo>
18 
20 
21 // Base class for common stuff.
23 public:
24  /// Returns \c true if we've tried to initialize the interface pointer,
25  /// even if we failed. This will not attempt to load the plugin or
26  /// initialize the interface pointer.
27  bool IsInitialized() const
28  {
29  return _initialized;
30  }
31 
32 #if !defined(doxygen)
34 #endif
35 
36 protected:
37  PLUG_API
38  void _LoadAndInstantiate(const std::type_info& type) const;
39 
40 protected:
41  // POD types only!
42  mutable std::atomic<bool> _initialized;
43  mutable void* _ptr;
44 };
45 
46 /// \class PlugStaticInterface
47 ///
48 /// Provides access to an interface into a plugin.
49 ///
50 /// A plugin can provide one or more interface types through which clients
51 /// can access the plugin's full functionality without needing to link
52 /// against the plugin (if you had to link against it, it wouldn't be a
53 /// plugin). This is a convenience; you can achieve the same effect
54 /// with TfType::GetFactory().
55 ///
56 /// Typical usage is:
57 /// \code
58 /// #include "path/to/SomePlugin.h"
59 /// #include "pxr/base/plug/staticInterface.h"
60 ///
61 /// static PlugStaticInterface<SomePluginInterface> ptr;
62 ///
63 /// void MyFunction() {
64 /// if (ptr) {
65 /// // Plugin is available.
66 /// ptr->MakePluginDoSomething();
67 /// }
68 /// else {
69 /// // Plugin is not available. (An error will have been reported
70 /// // the first time through.)
71 /// }
72 /// }
73 /// \endcode
74 ///
75 /// The interface must be defined correctly. In particular, it must have
76 ///
77 /// \li no data members (static or otherwise),
78 /// \li no static member functions,
79 /// \li no non-virtual member functions,
80 /// \li only pure virtual member functions,
81 /// \li no constructors except an inline protected default that does nothing
82 /// (or no constructors at all,)
83 /// \li a virtual destructor as the first member that \e must be defined
84 /// \e inline with an empty body even though inline virtual destructors
85 /// are contrary to conventional practice.
86 ///
87 /// The last requirement causes the compiler to emit a copy of the interface
88 /// typeinfo into clients of the interface. This typeinfo is required by
89 /// PlugStaticInterface internals to perform the appropriate plugin metadata
90 /// search for the interface type. Note that due to limitations in the GCC
91 /// C++ ABI an inline virtual destructor may prevent dynamic_cast<> and typeid()
92 /// from working correctly; do not use those on the interface type.
93 ///
94 /// For example:
95 ///
96 /// \code
97 /// class SomePluginInterface {
98 /// public:
99 /// virtual ~SomePluginInterface() { }
100 ///
101 /// virtual bool MakePluginDoSomething() const = 0;
102 ///
103 /// protected:
104 /// SomePluginInterface() { }
105 /// };
106 /// \endcode
107 /// Note that interface types do not share a common base class.
108 ///
109 /// For the plugin to work, there must be a concrete implementation of the
110 /// interface type, the interface type must be in plugInfo file, and the
111 /// interface type must be registered with TfType using
112 /// \c PLUG_REGISTER_INTERFACE_SINGLETON_TYPE:
113 /// \code
114 /// #include "path/to/SomePlugin.h"
115 /// #include "pxr/base/plug/interfaceFactory.h"
116 /// #include <iostream>
117 /// class SomePluginImplementation : public SomePluginInterface {
118 /// public:
119 /// virtual bool MakePluginDoSomething() const {
120 /// return std::cout << "Plugin did something" << std::endl;
121 /// }
122 /// };
123 /// PLUG_REGISTER_INTERFACE_SINGLETON_TYPE(SomePluginInterface,
124 /// SomePluginImplementation)
125 /// \endcode
126 /// This causes TfType::Find<SomePluginInterface>::Manufacture() to return
127 /// a pointer to a singleton instance of SomePluginImplementation.
128 ///
129 /// Note that only SomePluginInterface needs to be registered in the plugInfo
130 /// file and with TfType; other types provided by the plugin need only be
131 /// defined in SomePlugin.h. In addition, SomePluginInterface can provide
132 /// access to free functions in SomePlugin; clients would otherwise have to use
133 /// \c dlsym() to access free functions in the plugin.
134 ///
135 /// Warning: the \c PlugStaticInterface construct relies upon zero-initialization
136 /// of global data: therefore, you can only use this structure for static data
137 /// member of classes, variables declared at file-scope, or variables declared
138 /// static within a function. Do \e not declare a \c PlugStaticInterface object
139 /// as a local variable, as a member of a class or structure, or as a function
140 /// parameter.
141 ///
142 template <class Interface>
144 public:
146  "Interface type must be abstract.");
147 
149 
151 
152  /// Load and instantiate then return \c true if the interface is valid,
153  /// \c false otherwise.
154  operator UnspecifiedBoolType() const
155  {
156  return _GetPtr() ? &This::_ptr : nullptr;
157  }
158 
159  /// Load and instantiate then return \c false if the interface is valid,
160  /// \c true otherwise.
161  bool operator!() const
162  {
163  return !*this;
164  }
165 
166  /// Returns the interface pointer, loading the plugin if necessary.
167  /// Returns \c nullptr if the interface could not be initialized.
168  Interface* Get() const
169  {
170  return _GetPtr();
171  }
172 
173  /// Returns the interface pointer, loading the plugin if necessary.
174  /// Returns \c nullptr if the interface could not be initialized.
175  Interface* operator->() const
176  {
177  return _GetPtr();
178  }
179 
180  /// Returns the interface pointer as a reference, loading the plugin
181  /// if necessary. Returns \c nullptr if the interface could not be
182  /// initialized.
183  Interface& operator*() const
184  {
185  return *_GetPtr();
186  }
187 
188 private:
189  Interface* _GetPtr() const
190  {
191  if (!_initialized) {
192  _LoadAndInstantiate(typeid(Interface));
193  }
194 
195  // XXX: We must assume _ptr has the right type since we have
196  // no common base class to dynamic_cast<> from.
197  return static_cast<Interface*>(_ptr);
198  }
199 };
200 
202 
203 #endif // PXR_BASE_PLUG_STATIC_INTERFACE_H
PlugStaticInterface< Interface > This
GLsizei const GLfloat * value
Definition: glcorearb.h:824
bool operator!() const
Interface * operator->() const
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
void *Plug_StaticInterfaceBase::* UnspecifiedBoolType
PLUG_API void _LoadAndInstantiate(const std::type_info &type) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
Interface & operator*() const
#define PLUG_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::atomic< bool > _initialized
Interface * Get() const