obs-studio/libobs/obs.hpp
jp9000 6e1dd92f0c Improve thread safety in UI code
- Implemented better C++ classes for handling scenes/sources/items in
  obs.hpp, allowing them to automatically increment and decrement the
  references of each, as well as assign them to QVariants.

- Because QVariants are now using the C++ classes, remove the pointer
  QVariant wrapper.

- Use the new C++ classes with the QVariant user data of list box items,
  both for the sake of thread safety and to ensure that the data
  referenced is not freed until removed.  NOTE: still might need some
  testing.

- Implemented a source-remove signal from libobs, and start using that
  signal instead of the source-destroy signal for signalling item
  removal.
2014-02-02 14:26:23 -07:00

97 lines
2.7 KiB
C++

/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/* Useful C++ classes and bindings for base obs data */
#pragma once
#include <string.h>
#include <stdarg.h>
#include "obs.h"
/* RAII wrappers */
template<class RefClass> class OBSRef {
typedef typename RefClass::type T;
T val;
public:
inline OBSRef() : val(nullptr) {}
inline OBSRef(T val_) : val(val_) {RefClass::AddRef(val);}
inline OBSRef(const OBSRef &ref) : val(ref.val) {RefClass::AddRef(val);}
inline OBSRef(OBSRef &&ref) : val(ref.val) {ref.val = nullptr;}
inline ~OBSRef() {RefClass::Release(val);}
inline OBSRef &operator=(T valIn)
{
RefClass::AddRef(valIn);
RefClass::Release(val);
val = valIn;
return *this;
}
inline OBSRef &operator=(const OBSRef &ref)
{
RefClass::AddRef(ref.val);
RefClass::Release(val);
val = ref.val;
return *this;
}
inline OBSRef &operator=(OBSRef &&ref)
{
if (this != &ref) {
val = ref.val;
ref.val = NULL;
}
return *this;
}
inline operator T() const {return val;}
inline bool operator==(T p) const {return val == p;}
inline bool operator!=(T p) const {return val != p;}
};
class OBSSourceRefClass {
public:
typedef obs_source_t type;
static inline void AddRef(type val) {obs_source_addref(val);}
static inline void Release(type val) {obs_source_release(val);}
};
class OBSSceneRefClass {
public:
typedef obs_scene_t type;
static inline void AddRef(type val) {obs_scene_addref(val);}
static inline void Release(type val) {obs_scene_release(val);}
};
class OBSSceneItemRefClass {
public:
typedef obs_sceneitem_t type;
static inline void AddRef(type val) {obs_sceneitem_addref(val);}
static inline void Release(type val) {obs_sceneitem_release(val);}
};
typedef OBSRef<OBSSourceRefClass> OBSSource;
typedef OBSRef<OBSSceneRefClass> OBSScene;
typedef OBSRef<OBSSceneItemRefClass> OBSSceneItem;