Foobar2000:Development:Auto Pointers

From Hydrogenaudio Knowledgebase
Revision as of 20:12, 21 June 2022 by DEATH (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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.