/* Useful C++ classes and bindings for base obs data */
#pragma once
#include "obs.h"
/* RAII wrappers */
//模板类的声明 void addref(T)相当于void (*addref)(T) 函数指针
template<typename T, void addref(T), void release(T)> class OBSRef;
//使用using 定义别名(类 T = obs_source_t *,函数指针名称(obs_source_addref,obs_source_release))
using OBSSource = OBSRef<obs_source_t *, obs_source_addref, obs_source_release>;
using OBSScene = OBSRef<obs_scene_t *, obs_scene_addref, obs_scene_release>;
using OBSSceneItem =
OBSRef<obs_sceneitem_t *, obs_sceneitem_addref, obs_sceneitem_release>;
using OBSData = OBSRef<obs_data_t *, obs_data_addref, obs_data_release>;
using OBSDataArray = OBSRef<obs_data_array_t *, obs_data_array_addref,
obs_data_array_release>;
using OBSOutput = OBSRef<obs_output_t *, obs_output_addref, obs_output_release>;
using OBSEncoder =
OBSRef<obs_encoder_t *, obs_encoder_addref, obs_encoder_release>;
using OBSService =
OBSRef<obs_service_t *, obs_service_addref, obs_service_release>;
using OBSWeakSource = OBSRef<obs_weak_source_t *, obs_weak_source_addref,
obs_weak_source_release>;
using OBSWeakOutput = OBSRef<obs_weak_output_t *, obs_weak_output_addref,
obs_weak_output_release>;
using OBSWeakEncoder = OBSRef<obs_weak_encoder_t *, obs_weak_encoder_addref,
obs_weak_encoder_release>;
using OBSWeakService = OBSRef<obs_weak_service_t *, obs_weak_service_addref,
obs_weak_service_release>;
//类的声明
template<typename T, void addref(T), void release(T)> class OBSRef {
T val;
/*inline的用法:关键字inline 必须与函数定义体放在一起才能使函数成为内联,仅将inline 放在函数声明前面不起任何作用
inline 函数对编译器而言必须是可见的,以便它能够在调用点内展开该函数
定义在类声明之中的成员函数将自动地成为内联函数*/
//替换
inline OBSRef &Replace(T valIn)
{
addref(valIn);
release(val);
val = valIn;
return *this;
}
/*空结构体(在C++语言中的确规定了空结构体和空类所占内存大小为1,而C语言中空类和空结构体占用的大小是0);
为了满足C++标准规定的不同对象不能有相同地址,C++编译器保证任何类型对象大小不能为0。C++编译器会在空类或空结构体中增加一个虚设的字节(有的编译器可能不止一个),以确保不同的对象都具有不同的地址;
实例化的原因(空类同样可以被实例化),每个实例在内存中都有一个独一无二的地址,为了达到这个目的,编译器往往会给一个空类或空结构体(C++中结构体也可看为类)隐含的加一个字节,
这样空类或空结构体在实例化后在内存得到了独一无二的地址,所以空类所占的内存大小是1个字节。
*/
struct TakeOwnership {
};
//私有的构造函数
inline OBSRef(T val, TakeOwnership) : val(val) {}
public:
//构造函数
inline OBSRef() : val(nullptr) {}
inline OBSRef(T val_) : val(val_) { addref(val); }
inline OBSRef(const OBSRef &ref) : val(ref.val) { addref(val); }
inline OBSRef(OBSRef &&ref) : val(ref.val) { ref.val = nullptr; }
inline ~OBSRef() { release(val); }
//运算符重载
inline OBSRef &operator=(T valIn) { return Replace(valIn); }
inline OBSRef &operator=(const OBSRef &ref) { return Replace(ref.val); }
inline OBSRef &operator=(OBSRef &&ref)
{
if (this != &ref) {
release(val);
val = ref.val;
ref.val = nullptr;
}
return *this;
}
//强制转换类型运算符重载
inline operator T() const { return val; }
inline T Get() const { return val; }
//运算符重载
inline bool operator==(T p) const { return val == p; }
inline bool operator!=(T p) const { return val != p; }
/*友元函数(不是任何类的成员函数)
友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员
因为友元函数没有this指针,则参数要有三种情况:
要访问非static成员时,需要对象做参数;
要访问static成员或全局变量时,则不需要对象做参数;
如果做参数的对象是全局对象,则不需要对象做参数.
可以直接调用友元函数,不需要通过对象或指针
*/
friend OBSSource OBSGetStrongRef(obs_weak_source_t *weak);
friend OBSWeakSource OBSGetWeakRef(obs_source_t *source);
friend OBSOutput OBSGetStrongRef(obs_weak_output_t *weak);
friend OBSWeakOutput OBSGetWeakRef(obs_output_t *output);
friend OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak);
friend OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder);
friend OBSService OBSGetStrongRef(obs_weak_service_t *weak);
friend OBSWeakService OBSGetWeakRef(obs_service_t *service);
};
//因此外部定义的友元函数是没有作用域的(OBSRef::)
inline OBSSource OBSGetStrongRef(obs_weak_source_t *weak)
{
return {obs_weak_source_get_source(weak), OBSSource::TakeOwnership()};
}
inline OBSWeakSource OBSGetWeakRef(obs_source_t *source)
{
return {obs_source_get_weak_source(source),
OBSWeakSource::TakeOwnership()};
}
inline OBSOutput OBSGetStrongRef(obs_weak_output_t *weak)
{
return {obs_weak_output_get_output(weak), OBSOutput::TakeOwnership()};
}
inline OBSWeakOutput OBSGetWeakRef(obs_output_t *output)
{
return {obs_output_get_weak_output(output),
OBSWeakOutput::TakeOwnership()};
}
inline OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak)
{
return {obs_weak_encoder_get_encoder(weak),
OBSEncoder::TakeOwnership()};
}
inline OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder)
{
return {obs_encoder_get_weak_encoder(encoder),
OBSWeakEncoder::TakeOwnership()};
}
inline OBSService OBSGetStrongRef(obs_weak_service_t *weak)
{
return {obs_weak_service_get_service(weak),
OBSService::TakeOwnership()};
}
inline OBSWeakService OBSGetWeakRef(obs_service_t *service)
{
return {obs_service_get_weak_service(service),
OBSWeakService::TakeOwnership()};
}
//不打算实例化的对象
/* objects that are not meant to be instanced */
template<typename T, void destroy(T)> class OBSObj {
T obj;
public:
inline OBSObj() : obj(nullptr) {}
inline OBSObj(T obj_) : obj(obj_) {}
inline OBSObj(const OBSObj &) = delete;
inline OBSObj(OBSObj &&other) : obj(other.obj) { other.obj = nullptr; }
inline ~OBSObj() { destroy(obj); }
//运算符重载
inline OBSObj &operator=(T obj_)
{
if (obj_ != obj)
destroy(obj);
obj = obj_;
return *this;
}
//此运算符重载方式被禁用的
inline OBSObj &operator=(const OBSObj &) = delete;
inline OBSObj &operator=(OBSObj &&other)
{
if (obj)
destroy(obj);
obj = other.obj;
other.obj = nullptr;
return *this;
}
//强制类型运算符重载
inline operator T() const { return obj; }
//运算符重载
inline bool operator==(T p) const { return obj == p; }
inline bool operator!=(T p) const { return obj != p; }
};
//使用using 进行别名
using OBSDisplay = OBSObj<obs_display_t *, obs_display_destroy>;
using OBSView = OBSObj<obs_view_t *, obs_view_destroy>;
//信号处理器连接
/* signal handler connection */
class OBSSignal {
signal_handler_t *handler;
const char *signal;
signal_callback_t callback;
void *param;
public:
//构造函数
inline OBSSignal()
: handler(nullptr),
signal(nullptr),
callback(nullptr),
param(nullptr)
{
}
inline OBSSignal(signal_handler_t *handler_, const char *signal_,
signal_callback_t callback_, void *param_)
: handler(handler_),
signal(signal_),
callback(callback_),
param(param_)
{
signal_handler_connect_ref(handler, signal, callback, param);
}
//断开连接
inline void Disconnect()
{
signal_handler_disconnect(handler, signal, callback, param);
handler = nullptr;
signal = nullptr;
callback = nullptr;
param = nullptr;
}
inline ~OBSSignal() { Disconnect(); }
//开启连接
inline void Connect(signal_handler_t *handler_, const char *signal_,
signal_callback_t callback_, void *param_)
{
Disconnect();
handler = handler_;
signal = signal_;
callback = callback_;
param = param_;
signal_handler_connect_ref(handler, signal, callback, param);
}
//此种方式禁用
OBSSignal(const OBSSignal &) = delete;
//关键字作用noexcept:此拷贝构造函数运行时 不抛出异常 等同于noexcept ( true )
//https://en.cppreference.com/w/cpp/language/noexcept_spec
OBSSignal(OBSSignal &&other) noexcept
: handler(other.handler),
signal(other.signal),
callback(other.callback),
param(other.param)
{
other.handler = nullptr;
other.signal = nullptr;
other.callback = nullptr;
other.param = nullptr;
}
//此种方式禁用
OBSSignal &operator=(const OBSSignal &) = delete;
//复制运算符重载
OBSSignal &operator=(OBSSignal &&other) noexcept
{
Disconnect();
handler = other.handler;
signal = other.signal;
callback = other.callback;
param = other.param;
other.handler = nullptr;
other.signal = nullptr;
other.callback = nullptr;
other.param = nullptr;
return *this;
}
};
OBS源码: obs.hpp 类分析
最新推荐文章于 2022-11-07 11:14:09 发布