Enable checking if temp var is in use

This commit is contained in:
WarmUpTill
2026-03-20 23:30:11 +01:00
committed by WarmUpTill
parent a28b800228
commit ec3052c823
12 changed files with 139 additions and 6 deletions

View File

@@ -10,9 +10,12 @@
#include <QVariant>
#include <QAbstractItemView>
#include <atomic>
Q_DECLARE_METATYPE(advss::TempVariableRef);
static std::atomic<uint64_t> tempVarInUseGeneration{0};
namespace advss {
TempVariable::TempVariable(const std::string &id, const std::string &name,
@@ -120,6 +123,60 @@ TempVariableRef TempVariable::GetRef() const
return ref;
}
static bool refsContain(const std::vector<TempVariableRef> &refs,
const TempVariableRef &ref)
{
for (const auto &r : refs) {
if (r == ref) {
return true;
}
}
return false;
}
template<typename T>
static bool segmentsReferTo(const T &segments, const TempVariableRef &ref)
{
for (const auto &segment : segments) {
if (refsContain(segment->GetTempVarRefs(), ref)) {
return true;
}
}
return false;
}
bool TempVariable::IsInUse() const
{
const auto currentGen =
tempVarInUseGeneration.load(std::memory_order_relaxed);
if (_isInUseCacheGeneration == currentGen) {
return _isInUseCache;
}
bool inUse = false;
const auto ref = GetRef();
if (ref.HasValidID()) {
for (const auto &macro : GetAllMacros()) {
if (segmentsReferTo(macro->Conditions(), ref) ||
segmentsReferTo(macro->Actions(), ref) ||
segmentsReferTo(macro->ElseActions(), ref)) {
inUse = true;
break;
}
}
}
_isInUseCache = inUse;
_isInUseCacheGeneration = currentGen;
return inUse;
}
void IncrementTempVarInUseGeneration()
{
tempVarInUseGeneration.fetch_add(1, std::memory_order_relaxed);
}
TempVariableRef::SegmentType TempVariableRef::GetType() const
{
auto segment = _segment.lock();
@@ -665,6 +722,7 @@ TempVarSignalManager *TempVarSignalManager::Instance()
void NotifyUIAboutTempVarChange(MacroSegment *segment)
{
IncrementTempVarInUseGeneration();
obs_queue_task(
OBS_TASK_UI,
[](void *segment) {

View File

@@ -44,6 +44,7 @@ public:
void SetValue(const std::string &val);
void InvalidateValue();
TempVariableRef GetRef() const;
EXPORT bool IsInUse() const;
private:
std::string _id = "";
@@ -53,6 +54,8 @@ private:
mutable std::mutex _lastValuesMutex;
std::vector<std::string> _lastValues;
bool _valueIsValid = false;
mutable bool _isInUseCache = false;
mutable uint64_t _isInUseCacheGeneration = UINT64_MAX;
std::weak_ptr<MacroSegment> _segment;
friend TempVariableSelection;
@@ -122,5 +125,6 @@ private:
};
void NotifyUIAboutTempVarChange(MacroSegment *);
EXPORT void IncrementTempVarInUseGeneration();
} // namespace advss