MDL SDK API nvidia_logo_transpbg.gif Up
interface_implement.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2026 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
6
7#ifndef MI_BASE_INTERFACE_IMPLEMENT_H
8#define MI_BASE_INTERFACE_IMPLEMENT_H
9
10#include <mi/base/types.h>
11#include <mi/base/uuid.h>
12#include <mi/base/atom.h>
13
14namespace mi {
15namespace base {
16
21class IInterface;
22
38template <class I>
39class Interface_implement : public I
40{
41public:
42 using Interface = I;
43
48 : m_refcnt( initial)
49 {
50 }
51
56 : m_refcnt( 1)
57 {
58 // avoid warning
59 (void) other;
60 }
61
66 {
67 // Note: no call of operator= on m_refcount
68 // avoid warning
69 (void) other;
70 return *this;
71 }
72
75 : m_refcnt{other.m_refcnt.swap(0)}
76 {
77 }
78
81 {
82 other.m_refcnt = m_refcnt.swap(other.m_refcnt);
83 return *this;
84 }
85
90 Uint32 retain() const override
91 {
92 return ++m_refcnt;
93 }
94
100 Uint32 release() const override
101 {
102 Uint32 cnt = --m_refcnt;
103 if( !cnt)
104 delete this;
105 return cnt;
106 }
107
118 const IInterface* get_interface( const Uuid& interface_id) const override
119 {
120 return I::get_interface_static( this, interface_id);
121 }
122
133 IInterface* get_interface( const Uuid& interface_id) override
134 {
135 return I::get_interface_static( this, interface_id);
136 }
137
138 using I::get_interface;
139
141 Uuid get_iid() const override
142 {
143 return typename I::IID();
144 }
145
146protected:
148 Atom32& refcount() const { return m_refcnt; }
149
150protected:
151 virtual ~Interface_implement() = default;
152
153private:
154 mutable Atom32 m_refcnt;
155};
156
157
175template <class I1, class I2>
176class Interface_implement_2 : public I1, public I2
177{
178public:
183 : m_refcnt( initial)
184 {
185 }
186
191 : m_refcnt( 1)
192 {
193 // avoid warning
194 (void) other;
195 }
196
201 {
202 // Note: no call of operator= on m_refcount
203 // avoid warning
204 (void) other;
205 return *this;
206 }
207
212 virtual Uint32 retain() const override
213 {
214 return ++m_refcnt;
215 }
216
222 virtual Uint32 release() const override
223 {
224 Uint32 cnt = --m_refcnt;
225 if( !cnt)
226 delete this;
227 return cnt;
228 }
229
240 virtual const IInterface* get_interface( const Uuid& interface_id) const override
241 {
242 const IInterface* iptr = I1::get_interface_static(
243 static_cast<const I1*>( this), interface_id);
244 if( iptr)
245 return iptr;
246
247 return I2::get_interface_static( static_cast<const I2*>( this), interface_id);
248 }
249
260 virtual IInterface* get_interface( const Uuid& interface_id) override
261 {
262 IInterface* iptr = I1::get_interface_static( static_cast<I1*>( this), interface_id);
263 if( iptr)
264 return iptr;
265
266 return I2::get_interface_static( static_cast<I2*>( this), interface_id);
267 }
268
269 using I1::get_interface;
270
272 Uuid get_iid() const override
273 {
274 return typename I1::IID();
275 }
276
277protected:
278 virtual ~Interface_implement_2() = default;
279
280private:
281 mutable Atom32 m_refcnt;
282};
283
284
301template <class I>
303{
304public:
309 Uint32 retain() const override
310 {
311 return 1;
312 }
313
318 Uint32 release() const override
319 {
320 return 1;
321 }
322
333 const IInterface* get_interface( const Uuid& interface_id) const override
334 {
335 return I::get_interface_static( this, interface_id);
336 }
337
348 IInterface* get_interface( const Uuid& interface_id) override
349 {
350 return I::get_interface_static( this, interface_id);
351 }
352
353 using I::get_interface;
354
356 Uuid get_iid() const override
357 {
358 return typename I::IID();
359 }
360
361protected:
362 virtual ~Interface_implement_singleton() = default;
363};
364
365 // end group mi_base_iinterface
367
368} // namespace base
369} // namespace mi
370
371#endif // MI_BASE_INTERFACE_IMPLEMENT_H
32-bit unsigned counter with atomic arithmetic, increments, and decrements.
A 32-bit unsigned counter with atomic arithmetic, increments, and decrements.
Definition: atom.h:43
The basic extensible interface.
Definition: iinterface.h:104
Mixin class template for deriving interface implementations from two interfaces.
Definition: interface_implement.h:177
Mixin class template for deriving singleton interface implementations, where the reference count is f...
Definition: interface_implement.h:303
Mixin class template for deriving interface implementations.
Definition: interface_implement.h:40
Interface_implement(const Interface_implement<I> &other)
Copy constructor.
Definition: interface_implement.h:55
Uint32 release() const override
Returns the fixed reference count of one.
Definition: interface_implement.h:318
Uint32 retain() const override
Returns the fixed reference count of one.
Definition: interface_implement.h:309
virtual IInterface * get_interface(const Uuid &interface_id) override
Acquires a mutable interface.
Definition: interface_implement.h:260
Uint32 release() const override
Decrements the reference count.
Definition: interface_implement.h:100
Atom32 & refcount() const
Get the current refcount.
Definition: interface_implement.h:148
Uuid get_iid() const override
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:141
Interface_implement_2(Uint32 initial=1)
Constructor.
Definition: interface_implement.h:182
const IInterface * get_interface(const Uuid &interface_id) const override
Acquires a const interface.
Definition: interface_implement.h:118
Uuid get_iid() const override
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:272
Interface_implement(Interface_implement &&other)
Move constructor.
Definition: interface_implement.h:74
Interface_implement_2(const Interface_implement_2<I1, I2> &other)
Copy constructor.
Definition: interface_implement.h:190
virtual const IInterface * get_interface(const Uuid &interface_id) const override
Acquires a const interface.
Definition: interface_implement.h:240
const IInterface * get_interface(const Uuid &interface_id) const override
Acquires a const interface.
Definition: interface_implement.h:333
IInterface * get_interface(const Uuid &interface_id) override
Acquires a mutable interface.
Definition: interface_implement.h:348
Interface_implement<I> & operator=(const Interface_implement<I> &other)
Assignment operator.
Definition: interface_implement.h:65
virtual Uint32 release() const override
Decrements the reference count.
Definition: interface_implement.h:222
virtual Uint32 retain() const override
Increments the reference count.
Definition: interface_implement.h:212
Uuid get_iid() const override
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:356
Interface_implement(Uint32 initial=1)
Constructor.
Definition: interface_implement.h:47
IInterface * get_interface(const Uuid &interface_id) override
Acquires a mutable interface.
Definition: interface_implement.h:133
Interface_implement_2<I1, I2> & operator=(const Interface_implement_2<I1, I2> &other)
Assignment operator.
Definition: interface_implement.h:200
Uint32 retain() const override
Increments the reference count.
Definition: interface_implement.h:90
Interface_implement & operator=(Interface_implement &&other)
Move assignment.
Definition: interface_implement.h:80
Uint32 swap(const Uint32 rhs)
Assigns rhs to the counter and returns the old value of counter.
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:49
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
A 128 bit representation of a universally unique identifier (UUID or GUID).
Definition: uuid.h:26
Basic types.
A 128 bit representation of a universally unique identifier (UUID or GUID).