MDL SDK API nvidia_logo_transpbg.gif Up
interface_implement.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2025 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
21// Forward declaration
22class IInterface;
23
39template <class I>
40class Interface_implement : public I
41{
42public:
43 using Interface = I;
44
49 : m_refcnt( initial)
50 {
51 }
52
57 : m_refcnt( 1)
58 {
59 // avoid warning
60 (void) other;
61 }
62
67 {
68 // Note: no call of operator= on m_refcount
69 // avoid warning
70 (void) other;
71 return *this;
72 }
73
76 : m_refcnt{other.m_refcnt.swap(0)}
77 {
78 }
79
82 {
83 other.m_refcnt = m_refcnt.swap(other.m_refcnt);
84 return *this;
85 }
86
91 Uint32 retain() const override
92 {
93 return ++m_refcnt;
94 }
95
101 Uint32 release() const override
102 {
103 Uint32 cnt = --m_refcnt;
104 if( !cnt)
105 delete this;
106 return cnt;
107 }
108
119 const IInterface* get_interface( const Uuid& interface_id) const override
120 {
121 return I::get_interface_static( this, interface_id);
122 }
123
134 IInterface* get_interface( const Uuid& interface_id) override
135 {
136 return I::get_interface_static( this, interface_id);
137 }
138
139 using I::get_interface;
140
142 Uuid get_iid() const override
143 {
144 return typename I::IID();
145 }
146
147protected:
149 Atom32& refcount() const { return m_refcnt; }
150
151protected:
152 virtual ~Interface_implement() = default;
153
154private:
155 mutable Atom32 m_refcnt;
156};
157
158
176template <class I1, class I2>
177class Interface_implement_2 : public I1, public I2
178{
179public:
184 : m_refcnt( initial)
185 {
186 }
187
192 : m_refcnt( 1)
193 {
194 // avoid warning
195 (void) other;
196 }
197
202 {
203 // Note: no call of operator= on m_refcount
204 // avoid warning
205 (void) other;
206 return *this;
207 }
208
213 virtual Uint32 retain() const
214 {
215 return ++m_refcnt;
216 }
217
223 virtual Uint32 release() const
224 {
225 Uint32 cnt = --m_refcnt;
226 if( !cnt)
227 delete this;
228 return cnt;
229 }
230
241 virtual const IInterface* get_interface( const Uuid& interface_id) const
242 {
243 const IInterface* iptr = I1::get_interface_static(
244 static_cast<const I1*>( this), interface_id);
245 if( iptr)
246 return iptr;
247
248 return I2::get_interface_static( static_cast<const I2*>( this), interface_id);
249 }
250
261 virtual IInterface* get_interface( const Uuid& interface_id)
262 {
263 IInterface* iptr = I1::get_interface_static( static_cast<I1*>( this), interface_id);
264 if( iptr)
265 return iptr;
266
267 return I2::get_interface_static( static_cast<I2*>( this), interface_id);
268 }
269
270 using I1::get_interface;
271
273 Uuid get_iid() const
274 {
275 return typename I1::IID();
276 }
277
278protected:
279 virtual ~Interface_implement_2() = default;
280
281private:
282 mutable Atom32 m_refcnt;
283};
284
285
302template <class I>
304{
305public:
310 Uint32 retain() const override
311 {
312 return 1;
313 }
314
319 Uint32 release() const override
320 {
321 return 1;
322 }
323
334 const IInterface* get_interface( const Uuid& interface_id) const override
335 {
336 return I::get_interface_static( this, interface_id);
337 }
338
349 IInterface* get_interface( const Uuid& interface_id) override
350 {
351 return I::get_interface_static( this, interface_id);
352 }
353
354 using I::get_interface;
355
357 Uuid get_iid() const override
358 {
359 return typename I::IID();
360 }
361
362protected:
363 virtual ~Interface_implement_singleton() = default;
364};
365
366 // end group mi_base_iinterface
368
369} // namespace base
370} // namespace mi
371
372#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:32
The basic extensible interface.
Definition: iinterface.h:103
Mixin class template for deriving interface implementations from two interfaces.
Definition: interface_implement.h:178
Mixin class template for deriving singleton interface implementations, where the reference count is f...
Definition: interface_implement.h:304
Mixin class template for deriving interface implementations.
Definition: interface_implement.h:41
Interface_implement(const Interface_implement<I> &other)
Copy constructor.
Definition: interface_implement.h:56
Uint32 release() const override
Returns the fixed reference count of one.
Definition: interface_implement.h:319
Uint32 retain() const override
Returns the fixed reference count of one.
Definition: interface_implement.h:310
Uint32 release() const override
Decrements the reference count.
Definition: interface_implement.h:101
Atom32 & refcount() const
Get the current refcount.
Definition: interface_implement.h:149
virtual Uint32 release() const
Decrements the reference count.
Definition: interface_implement.h:223
Uuid get_iid() const override
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:142
Interface_implement_2(Uint32 initial=1)
Constructor.
Definition: interface_implement.h:183
const IInterface * get_interface(const Uuid &interface_id) const override
Acquires a const interface.
Definition: interface_implement.h:119
Interface_implement(Interface_implement &&other)
Move constructor.
Definition: interface_implement.h:75
virtual Uint32 retain() const
Increments the reference count.
Definition: interface_implement.h:213
Interface_implement_2(const Interface_implement_2<I1, I2> &other)
Copy constructor.
Definition: interface_implement.h:191
const IInterface * get_interface(const Uuid &interface_id) const override
Acquires a const interface.
Definition: interface_implement.h:334
IInterface * get_interface(const Uuid &interface_id) override
Acquires a mutable interface.
Definition: interface_implement.h:349
Interface_implement<I> & operator=(const Interface_implement<I> &other)
Assignment operator.
Definition: interface_implement.h:66
Uuid get_iid() const override
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:357
Interface_implement(Uint32 initial=1)
Constructor.
Definition: interface_implement.h:48
virtual const IInterface * get_interface(const Uuid &interface_id) const
Acquires a const interface.
Definition: interface_implement.h:241
IInterface * get_interface(const Uuid &interface_id) override
Acquires a mutable interface.
Definition: interface_implement.h:134
virtual IInterface * get_interface(const Uuid &interface_id)
Acquires a mutable interface.
Definition: interface_implement.h:261
Uuid get_iid() const
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:273
Interface_implement_2<I1, I2> & operator=(const Interface_implement_2<I1, I2> &other)
Assignment operator.
Definition: interface_implement.h:201
Uint32 retain() const override
Increments the reference count.
Definition: interface_implement.h:91
Interface_implement & operator=(Interface_implement &&other)
Move assignment.
Definition: interface_implement.h:81
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).