libobs: Add AutoRelease OBSRef wrappers for OBS types

These AutoRelease versions of the C++ OBSRef types do not add a ref on
construction, which is useful when accepting the result of a function
that turns a raw C pointer type that has had a reference added.

Not having these types has resulted in multiple awkward anti-patterns.
Such as immediately releasing after construction to account for the
extra addref, or avoiding using the C++ type entirely and manually
releasing it later.

Example:
```
OBSSource source = obs_get_source_by_name(name.c_str());
obs_source_release(source);
```

The whole point of these types is to avoid the need for manual releases,
and rely on the RAII mechanisms inside of C++. Additionally, the
immediate release isn't commented anywhere, resulting in confusion for
other developers looking at the code as to why things are being
immediately released.

The AutoRelease types and names are taken from obs-websocket.
This commit is contained in:
VodBox 2021-11-26 21:48:28 +13:00
parent ab5d41c445
commit a8c30373d6

View File

@ -47,6 +47,52 @@ using OBSWeakEncoder = OBSRef<obs_weak_encoder_t *, obs_weak_encoder_addref,
using OBSWeakService = OBSRef<obs_weak_service_t *, obs_weak_service_addref,
obs_weak_service_release>;
inline void ___source_dummy_addref(obs_source_t *){};
inline void ___scene_dummy_addref(obs_scene_t *){};
inline void ___sceneitem_dummy_addref(obs_sceneitem_t *){};
inline void ___data_dummy_addref(obs_data_t *){};
inline void ___data_array_dummy_addref(obs_data_array_t *){};
inline void ___output_dummy_addref(obs_output_t *){};
inline void ___encoder_dummy_addref(obs_encoder_t *){};
inline void ___service_dummy_addref(obs_service_t *){};
inline void ___weak_source_dummy_addref(obs_weak_source_t *){};
inline void ___weak_output_dummy_addref(obs_weak_output_t *){};
inline void ___weak_encoder_dummy_addref(obs_weak_encoder_t *){};
inline void ___weak_service_dummy_addref(obs_weak_service_t *){};
using OBSSourceAutoRelease =
OBSRef<obs_source_t *, ___source_dummy_addref, obs_source_release>;
using OBSSceneAutoRelease =
OBSRef<obs_scene_t *, ___scene_dummy_addref, obs_scene_release>;
using OBSSceneItemAutoRelease =
OBSRef<obs_sceneitem_t *, ___sceneitem_dummy_addref,
obs_sceneitem_release>;
using OBSDataAutoRelease =
OBSRef<obs_data_t *, ___data_dummy_addref, obs_data_release>;
using OBSDataArrayAutoRelease =
OBSRef<obs_data_array_t *, ___data_array_dummy_addref,
obs_data_array_release>;
using OBSOutputAutoRelease =
OBSRef<obs_output_t *, ___output_dummy_addref, obs_output_release>;
using OBSEncoderAutoRelease =
OBSRef<obs_encoder_t *, ___encoder_dummy_addref, obs_encoder_release>;
using OBSServiceAutoRelease =
OBSRef<obs_service_t *, ___service_dummy_addref, obs_service_release>;
using OBSWeakSourceAutoRelease =
OBSRef<obs_weak_source_t *, ___weak_source_dummy_addref,
obs_weak_source_release>;
using OBSWeakOutputAutoRelease =
OBSRef<obs_weak_output_t *, ___weak_output_dummy_addref,
obs_weak_output_release>;
using OBSWeakEncoderAutoRelease =
OBSRef<obs_weak_encoder_t *, ___weak_encoder_dummy_addref,
obs_weak_encoder_release>;
using OBSWeakServiceAutoRelease =
OBSRef<obs_weak_service_t *, ___weak_service_dummy_addref,
obs_weak_service_release>;
template<typename T, void addref(T), void release(T)> class OBSRef {
T val;