Foobar2000:Development:Auto Pointers: Difference between revisions
(Created page with "Category:Foobar2000 Development When performing most kinds of service operations, <code>service_ptr_t<T></code> template should be used instead of working with service po...") |
No edit summary |
||
Line 9: | Line 9: | ||
service_ptr_t<> provides additional convenience operators: | service_ptr_t<> provides additional convenience operators: | ||
* <code>operator&=</code> | * <code>operator&=</code> | ||
Attempts to obtain a pointer to another interface, if the passed object supports it. Returns true on success, false on failure. | Attempts to obtain a pointer to another interface, if the passed object supports it. Returns true on success, false on failure - interface not implemented or null pointer. | ||
<pre> | <pre> | ||
Line 16: | Line 16: | ||
A = create_service1(); | A = create_service1(); | ||
if ( B &= A ) { | if ( B &= A ) { | ||
// | // B is valid. | ||
// Do stuff with B. | |||
} else { | } else { | ||
// A does not implement service2, or is null | // A does not implement service2, or is null. | ||
} | } | ||
</pre> | </pre> |
Latest revision as of 20:12, 21 June 2022
When performing most kinds of service operations, service_ptr_t<T>
template should be used instead of working with service pointers directly; it automatically manages reference counting, ensuring that the service object is deleted when it is no longer referenced.
For convenience, all service classes have myclass::ptr
typedef'd to service_ptr_t<myclass>
.
When working with pointers to core fb2k services, just use C++11 auto keyword and someclass::get()
, e.g. auto myAPI = playlist_manager::get();
service_ptr_t<> provides additional convenience operators:
operator&=
Attempts to obtain a pointer to another interface, if the passed object supports it. Returns true on success, false on failure - interface not implemented or null pointer.
service_ptr_t<service1> A; service_ptr_t<service2> B; A = create_service1(); if ( B &= A ) { // B is valid. // Do stuff with B. } else { // A does not implement service2, or is null. }
operator^=
Forced cast. Use to obtain a valid pointer to another interface in a scenario where you know that the object supports it.
There's no error return, the process crashes if the interface you ask for is not implemented by this object.
service_ptr_t<service1> A; service_ptr_t<service2> B; A = create_service1(); B ^= A; // if you get here, B is valid (or null if A was null)
Use the above operators to safely cast from parent service class to derived service class. Do not use C++ dynamic_cast<> / static_cast<> / reinterpret_cast<>
on foobar2000 service classes.