MDL SDK API nvidia_logo_transpbg.gif Up
interface_implement.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2024 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 typedef I Interface;
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
74#if (__cplusplus >= 201103L)
77 : m_refcnt{other.m_refcnt.swap(0)}
78 {
79 }
80
83 {
84 other.m_refcnt = m_refcnt.swap(other.m_refcnt);
85 return *this;
86 }
87#endif
88
93 virtual Uint32 retain() const
94 {
95 return ++m_refcnt;
96 }
97
103 virtual Uint32 release() const
104 {
105 Uint32 cnt = --m_refcnt;
106 if( !cnt)
107 delete this;
108 return cnt;
109 }
110
121 virtual const IInterface* get_interface( const Uuid& interface_id) const
122 {
123 return I::get_interface_static( this, interface_id);
124 }
125
136 virtual IInterface* get_interface( const Uuid& interface_id)
137 {
138 return I::get_interface_static( this, interface_id);
139 }
140
141 using I::get_interface;
142
144 Uuid get_iid() const
145 {
146 return typename I::IID();
147 }
148
149protected:
151 Atom32& refcount() const { return m_refcnt; }
152
153protected:
154 virtual ~Interface_implement() {}
155
156private:
157 mutable Atom32 m_refcnt;
158};
159
160
178template <class I1, class I2>
179class Interface_implement_2 : public I1, public I2
180{
181public:
186 : m_refcnt( initial)
187 {
188 }
189
194 : m_refcnt( 1)
195 {
196 // avoid warning
197 (void) other;
198 }
199
204 {
205 // Note: no call of operator= on m_refcount
206 // avoid warning
207 (void) other;
208 return *this;
209 }
210
215 virtual Uint32 retain() const
216 {
217 return ++m_refcnt;
218 }
219
225 virtual Uint32 release() const
226 {
227 Uint32 cnt = --m_refcnt;
228 if( !cnt)
229 delete this;
230 return cnt;
231 }
232
243 virtual const IInterface* get_interface( const Uuid& interface_id) const
244 {
245 const IInterface* iptr = I1::get_interface_static( static_cast<const I1*>(this),
246 interface_id);
247 if ( iptr == 0)
248 iptr = I2::get_interface_static( static_cast<const I2*>(this), interface_id);
249 return iptr;
250 }
251
262 virtual IInterface* get_interface( const Uuid& interface_id)
263 {
264 IInterface* iptr = I1::get_interface_static(static_cast<I1*>(this),interface_id);
265 if ( iptr == 0)
266 iptr = I2::get_interface_static( static_cast<I2*>(this), interface_id);
267 return iptr;
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() {}
280
281private:
282 mutable Atom32 m_refcnt;
283};
284
285
302template <class I>
304{
305public:
310 virtual Uint32 retain() const
311 {
312 return 1;
313 }
314
319 virtual Uint32 release() const
320 {
321 return 1;
322 }
323
334 virtual const IInterface* get_interface( const Uuid& interface_id) const
335 {
336 return I::get_interface_static( this, interface_id);
337 }
338
349 virtual IInterface* get_interface( const Uuid& interface_id)
350 {
351 return I::get_interface_static( this, interface_id);
352 }
353
354 using I::get_interface;
355
357 Uuid get_iid() const
358 {
359 return typename I::IID();
360 }
361
362protected:
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:41
The basic extensible interface.
Definition: iinterface.h:103
Mixin class template for deriving interface implementations from two interfaces.
Definition: interface_implement.h:180
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
virtual Uint32 release() const
Decrements the reference count.
Definition: interface_implement.h:103
Atom32 & refcount() const
Get the current refcount.
Definition: interface_implement.h:151
virtual Uint32 release() const
Decrements the reference count.
Definition: interface_implement.h:225
virtual const IInterface * get_interface(const Uuid &interface_id) const
Acquires a const interface.
Definition: interface_implement.h:121
Interface_implement_2(Uint32 initial=1)
Constructor.
Definition: interface_implement.h:185
virtual IInterface * get_interface(const Uuid &interface_id)
Acquires a mutable interface.
Definition: interface_implement.h:349
Interface_implement(Interface_implement &&other)
Move constructor.
Definition: interface_implement.h:76
virtual Uint32 retain() const
Increments the reference count.
Definition: interface_implement.h:215
Interface_implement_2(const Interface_implement_2<I1, I2> &other)
Copy constructor.
Definition: interface_implement.h:193
virtual Uint32 release() const
Returns the fixed reference count of one.
Definition: interface_implement.h:319
virtual Uint32 retain() const
Returns the fixed reference count of one.
Definition: interface_implement.h:310
Interface_implement<I> & operator=(const Interface_implement<I> &other)
Assignment operator.
Definition: interface_implement.h:66
virtual IInterface * get_interface(const Uuid &interface_id)
Acquires a mutable interface.
Definition: interface_implement.h:136
Uuid get_iid() const
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:243
virtual IInterface * get_interface(const Uuid &interface_id)
Acquires a mutable interface.
Definition: interface_implement.h:262
Uuid get_iid() const
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:273
virtual Uint32 retain() const
Increments the reference count.
Definition: interface_implement.h:93
Interface_implement_2<I1, I2> & operator=(const Interface_implement_2<I1, I2> &other)
Assignment operator.
Definition: interface_implement.h:203
virtual const IInterface * get_interface(const Uuid &interface_id) const
Acquires a const interface.
Definition: interface_implement.h:334
Uuid get_iid() const
Returns the interface ID of the most derived interface.
Definition: interface_implement.h:144
Interface_implement & operator=(Interface_implement &&other)
Move assignment.
Definition: interface_implement.h:82
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).