2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2022-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2022-05-14 04:29:41 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "obs-source.hpp"
|
|
|
|
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-disable.hpp"
|
|
|
|
#include <string_view>
|
|
|
|
#include "warning-enable.hpp"
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
// ToDo:
|
|
|
|
// - Is FORCE_INLINE necessary for optimal performance, or can LTO handle this?
|
2022-05-14 04:29:41 +00:00
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
namespace streamfx::obs {
|
2022-05-14 04:29:41 +00:00
|
|
|
class weak_source {
|
|
|
|
obs_weak_source_t* _ref;
|
|
|
|
|
|
|
|
public:
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE ~weak_source() noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
if (_ref) {
|
|
|
|
obs_weak_source_release(_ref);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
/** Create an empty weak source.
|
2022-05-14 04:29:41 +00:00
|
|
|
*
|
2022-08-20 19:07:11 +00:00
|
|
|
* The weak source will be expired, as it points at nothing.
|
2022-05-14 04:29:41 +00:00
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE weak_source() : _ref(nullptr){};
|
2022-05-14 04:29:41 +00:00
|
|
|
|
|
|
|
/** Create a new weak reference from an existing pointer.
|
|
|
|
*
|
2022-08-20 19:07:11 +00:00
|
|
|
* @param duplicate If true, will duplicate the pointer instead of taking ownership.
|
2022-05-14 04:29:41 +00:00
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE weak_source(obs_weak_source_t* source, bool duplicate = true) : _ref(source)
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
if (!_ref)
|
|
|
|
throw std::invalid_argument("Parameter 'source' does not define a valid source.");
|
2022-08-20 19:07:11 +00:00
|
|
|
if (duplicate)
|
|
|
|
obs_weak_source_addref(_ref);
|
2022-05-14 04:29:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Create a new weak reference from an existing hard reference.
|
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE weak_source(obs_source_t* source)
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
_ref = obs_source_get_weak_source(source);
|
|
|
|
if (!_ref)
|
|
|
|
throw std::invalid_argument("Parameter 'source' does not define a valid source.");
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Create a new weak reference from an existing hard reference.
|
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE weak_source(const ::streamfx::obs::source& source)
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
_ref = obs_source_get_weak_source(source.get());
|
|
|
|
if (!_ref)
|
|
|
|
throw std::invalid_argument("Parameter 'source' does not define a valid source.");
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Create a new weak reference for a given source by name.
|
|
|
|
*
|
|
|
|
* Attention: May fail if the name does not exactly match.
|
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE weak_source(std::string_view name)
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
std::shared_ptr<obs_source_t> ref{obs_get_source_by_name(name.data()),
|
|
|
|
[](obs_source_t* v) { obs_source_release(v); }};
|
|
|
|
if (!ref) {
|
|
|
|
throw std::invalid_argument("Parameter 'name' does not define an valid source.");
|
|
|
|
}
|
|
|
|
_ref = obs_source_get_weak_source(ref.get());
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
public /* Move & Copy Operators */:
|
|
|
|
/** Move Constructor
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
FORCE_INLINE weak_source(::streamfx::obs::weak_source&& move) noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
_ref = move._ref;
|
|
|
|
move._ref = nullptr;
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
/** Copy Constructor
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
FORCE_INLINE weak_source(const ::streamfx::obs::weak_source& copy) noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
2022-08-20 19:07:11 +00:00
|
|
|
_ref = copy._ref;
|
|
|
|
obs_weak_source_addref(_ref);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Move Assign
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
FORCE_INLINE ::streamfx::obs::weak_source& operator=(::streamfx::obs::weak_source&& move) noexcept
|
|
|
|
{
|
|
|
|
reset();
|
2022-05-14 04:29:41 +00:00
|
|
|
if (move._ref) {
|
|
|
|
_ref = move._ref;
|
|
|
|
move._ref = nullptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
/** Copy Assign
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
FORCE_INLINE ::streamfx::obs::weak_source& operator=(const ::streamfx::obs::weak_source& copy) noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
2022-08-20 19:07:11 +00:00
|
|
|
reset();
|
2022-05-14 04:29:41 +00:00
|
|
|
if (copy._ref) {
|
|
|
|
_ref = copy._ref;
|
|
|
|
obs_weak_source_addref(_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
public /* ... */:
|
2022-05-14 04:29:41 +00:00
|
|
|
/** Retrieve the underlying pointer for manual manipulation.
|
|
|
|
*
|
|
|
|
* Attention: Ownership remains with the class instance.
|
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
[[deprecated("Prefer operator*()")]] FORCE_INLINE obs_weak_source_t* get() const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return _ref;
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
/** Release the ownership of the managed object.
|
|
|
|
*
|
|
|
|
* Same as 'x = nullptr;'.
|
|
|
|
*/
|
|
|
|
FORCE_INLINE void reset() noexcept
|
|
|
|
{
|
|
|
|
if (_ref) {
|
|
|
|
obs_weak_source_release(_ref);
|
|
|
|
_ref = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-14 04:29:41 +00:00
|
|
|
/** Is the weak reference expired?
|
|
|
|
*
|
|
|
|
* A weak reference is expired when the original object it is pointing at no longer exists.
|
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE bool expired() const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return (!_ref) || (obs_weak_source_expired(_ref));
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Try and acquire a hard reference to the source.
|
|
|
|
*
|
|
|
|
* May fail if the reference expired before we successfully acquire it.
|
|
|
|
*/
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE ::streamfx::obs::source lock() const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return {obs_weak_source_get_source(_ref)};
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
public /* Type Conversion Operators */:
|
|
|
|
FORCE_INLINE operator obs_weak_source_t*() const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return _ref;
|
2022-08-20 19:07:11 +00:00
|
|
|
};
|
2022-05-14 04:29:41 +00:00
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE obs_weak_source_t* operator*() const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return _ref;
|
2022-08-20 19:07:11 +00:00
|
|
|
};
|
2022-05-14 04:29:41 +00:00
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE operator bool() const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return !expired();
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
public /* Comparison Operators */:
|
|
|
|
FORCE_INLINE bool operator==(::streamfx::obs::weak_source const& rhs) const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return _ref == rhs._ref;
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE bool operator<(::streamfx::obs::weak_source const& rhs) const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return _ref < rhs._ref;
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE bool operator==(obs_weak_source_t* const& rhs) const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return _ref == rhs;
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE bool operator==(::streamfx::obs::source const& rhs) const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return obs_weak_source_references_source(_ref, rhs.get());
|
|
|
|
};
|
|
|
|
|
2022-08-20 19:07:11 +00:00
|
|
|
FORCE_INLINE bool operator==(obs_source_t* const& rhs) const noexcept
|
2022-05-14 04:29:41 +00:00
|
|
|
{
|
|
|
|
return obs_weak_source_references_source(_ref, rhs);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} // namespace streamfx::obs
|