7#ifndef MI_BASE_HANDLE_H
8#define MI_BASE_HANDLE_H
13#ifdef __cpp_variadic_templates
26struct Dup_interface_helper {};
110template <
class Interface>
135 template <
typename I2>
friend class Handle;
149 explicit Handle( Interface* ptr) : m_iptr( ptr) { }
167 : m_iptr( other.m_iptr)
179 template <
class Interface2>
181 : m_iptr( other.
get())
189 : m_iptr( other.m_iptr)
191 other.m_iptr =
nullptr;
195 template <
class Interface2>
197 : m_iptr( other.m_iptr)
205 Interface* tmp_iptr = m_iptr;
206 m_iptr = other.m_iptr;
207 other.m_iptr = tmp_iptr;
214 Self( other).swap( *
this);
225 template <
class Interface2>
228 Self( other).swap( *
this);
235 if(
this != &other) {
238 m_iptr = other.m_iptr;
239 other.m_iptr =
nullptr;
245 template <
class Interface2>
250 m_iptr = other.m_iptr;
263 Self( ptr).swap( *
this);
289 Interface*
get()
const {
return m_iptr; }
296 Interface* ptr = m_iptr;
301#ifdef __cpp_variadic_templates
303 template <
typename... T>
304 Self& emplace(T&&... args)
306 return emplace<Interface>(std::forward<T>(args)...);
314 template <
typename Impl,
typename... T>
315 Self& emplace(T&&... args)
318 m_iptr =
new Impl(std::forward<T>(args)...);
347 template <
class New_
interface>
353 m_iptr->get_interface(
typename New_interface::IID())));
382 return lhs.
get() == rhs;
388 return lhs == rhs.
get();
394 return !( lhs == rhs);
399 return !( lhs == rhs);
404template <
class Interface1,
class Interface2>
407 return lhs.
get() == rhs.
get();
411template <
class Interface1,
class Interface2>
414 return !( lhs == rhs);
422template <
class Interface>
433template <
class Interface>
439#ifdef __cpp_variadic_templates
444template <
typename Impl,
typename... T>
445inline Handle<Impl> construct_handle(T&&... args)
447 return Handle<Impl>{
new Impl(std::forward<T>(args)...)};
Assertions and compile-time assertions.
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:112
#define mi_base_assert_msg(expr, msg)
Base API assertion macro (with message).
Definition: assert.h:103
Handle()
Default constructor, initializes handle to hold an invalid interface.
Definition: handle.h:142
Interface * operator->() const
The arrow operator accesses the interface.
Definition: handle.h:335
Interface & operator*() const
The dereference operator accesses the interface.
Definition: handle.h:326
Handle(const Handle<Interface2> &other)
Copy constructor template which allows the construction from assignment compatible interface pointers...
Definition: handle.h:180
Difference difference_type
Difference type (signed integral type to hold pointer differences).
Definition: handle.h:126
Handle(const Self &other)
Copy constructor, increments reference count if interface is valid.
Definition: handle.h:166
friend bool operator==(const Interface *lhs, const Handle<Interface> &rhs)
Returns true if lhs is equal to the underlying interface pointer of rhs.
Definition: handle.h:386
~Handle()
Destructor, releases the interface if it is valid, which decrements the reference count,...
Definition: handle.h:279
friend bool operator==(const Handle<Interface> &lhs, const Interface *rhs)
Returns true if the underlying interface pointer of lhs is equal to rhs.
Definition: handle.h:380
Handle<Interface> make_handle_dup(Interface *iptr)
Converts passed-in interface pointer to a handle, without taking interface over.
Definition: handle.h:434
void swap(Self &other)
Swap two interfaces.
Definition: handle.h:203
static const Dup_interface DUP_INTERFACE
Symbolic constant to trigger a special constructor in the Handle class.
Definition: handle.h:36
Handle(Interface *ptr, Dup_interface)
Constructor from interface pointer, does not take ownership of interface but duplicates it.
Definition: handle.h:158
bool operator==(const Handle<Interface1> &lhs, const Handle<Interface2> &rhs)
Returns true if the underlying interface pointers are equal.
Definition: handle.h:405
Handle(Handle<Interface2> &&other) noexcept
Converting move constructor.
Definition: handle.h:196
friend bool operator!=(const Handle<Interface> &lhs, const Interface *rhs)
Returns true if the underlying interface pointer of lhs is not equal to rhs.
Definition: handle.h:392
Self & operator=(Self &&other) noexcept
Move assignment operator, releases old interface.
Definition: handle.h:233
Handle<New_interface> get_interface() const
Returns a new handle for a possibly different interface type, similar to a dynamic cast,...
Definition: handle.h:348
Interface value_type
Type of the underlying interface.
Definition: handle.h:123
Handle(Self &&other) noexcept
Move constructor.
Definition: handle.h:188
Handle<Interface> Self
Own type.
Definition: handle.h:115
Handle<Interface> make_handle(Interface *iptr)
Returns a handle that holds the interface pointer passed in as argument.
Definition: handle.h:423
bool(Handle::*)() const bool_conversion_support
Helper typedef.
Definition: handle.h:360
Handle(Interface *ptr)
Constructor from interface pointer, takes ownership of interface.
Definition: handle.h:149
Interface * extract()
Extracts the interface and releases the handle.
Definition: handle.h:294
friend bool operator!=(const Interface *lhs, const Handle<Interface> &rhs)
Returns true if lhs is not equal to the underlying interface pointer of rhs.
Definition: handle.h:398
Self & operator=(const Self &other)
Assignment operator, releases old interface and increments reference count of the new interface if in...
Definition: handle.h:212
Interface Interface_type
Type of the underlying interface.
Definition: handle.h:118
Self & operator=(Interface *ptr)
Assignment operator from interface pointer, releases old interface and assigns new interface ptr,...
Definition: handle.h:261
Interface & reference
Mutable-reference type to underlying interface.
Definition: handle.h:132
Interface * pointer
Mutable-pointer type to underlying interface.
Definition: handle.h:129
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:289
Self & operator=(const Handle<Interface2> &other)
Assignment operator template, releases old interface and increments reference count of the new interf...
Definition: handle.h:226
Self & operator=(Handle<Interface2> &&other) noexcept
Converting move assignment operator, releases old interface.
Definition: handle.h:246
bool operator!=(const Handle<Interface1> &lhs, const Handle<Interface2> &rhs)
Returns true if the underlying interface pointers are not equal.
Definition: handle.h:412
const Dup_interface_helper * Dup_interface
Type for a symbolic constant to trigger a special constructor in the Handle class.
Definition: handle.h:31
void reset()
Releases the current interface, decrementing the reference count.
Definition: handle.h:268
bool is_valid_interface() const
Returns true if the interface is valid.
Definition: handle.h:286
Sint64 Difference
Signed integral type that is large enough to hold the difference of two pointers.
Definition: types.h:122
The basic extensible interface.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5