File: condition-logic.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (118 lines) | stat: -rw-r--r-- 3,069 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "condition-logic.hpp"
#include "obs-module-helper.hpp"
#include "log-helper.hpp"

#include <cassert>
#include <QComboBox>

namespace advss {

const std::map<Logic::Type, const char *> Logic::localeMap = {
	{Logic::Type::NONE, {"AdvSceneSwitcher.logic.none"}},
	{Logic::Type::AND, {"AdvSceneSwitcher.logic.and"}},
	{Logic::Type::OR, {"AdvSceneSwitcher.logic.or"}},
	{Logic::Type::AND_NOT, {"AdvSceneSwitcher.logic.andNot"}},
	{Logic::Type::OR_NOT, {"AdvSceneSwitcher.logic.orNot"}},
	{Logic::Type::ROOT_NONE, {"AdvSceneSwitcher.logic.rootNone"}},
	{Logic::Type::ROOT_NOT, {"AdvSceneSwitcher.logic.not"}},
};

bool Logic::ApplyConditionLogic(Type type, bool currentMatchResult,
				bool conditionMatched, const char *context)
{
	return ApplyConditionLogic(
		type, currentMatchResult,
		[conditionMatched]() { return conditionMatched; }, context);
}

bool Logic::ApplyConditionLogic(Type type, bool currentMatchResult,
				const std::function<bool()> &evaluateCondition,
				const char *context)
{
	if (!context) {
		context = "";
	}

	switch (type) {
	case Type::ROOT_NONE:
		return evaluateCondition();
	case Type::ROOT_NOT:
		return !evaluateCondition();
	case Type::ROOT_LAST:
		break;
	case Type::NONE:
		vblog(LOG_INFO, "skipping condition check for '%s'", context);
		return currentMatchResult;
	case Type::AND:
		return currentMatchResult && evaluateCondition();
	case Type::OR:
		return currentMatchResult || evaluateCondition();
	case Type::AND_NOT:
		return currentMatchResult && !evaluateCondition();
	case Type::OR_NOT:
		return currentMatchResult || !evaluateCondition();
	case Type::LAST:
	default:
		blog(LOG_WARNING, "ignoring invalid logic check (%s)", context);
		return currentMatchResult;
	}

	return currentMatchResult;
}

void Logic::PopulateLogicTypeSelection(QComboBox *list, bool isRootCondition)
{
	auto compare = isRootCondition
			       ? std::function<bool(int)>{[](int typeValue) {
					 return typeValue < rootOffset;
				 }}
			       : std::function<bool(int)>{[](int typeValue) {
					 return typeValue >= rootOffset;
				 }};
	for (const auto &[type, name] : localeMap) {
		const int typeValue = static_cast<int>(type);
		if (compare(typeValue)) {
			list->addItem(obs_module_text(name), typeValue);
		}
	}
}

void Logic::Save(obs_data_t *obj, const char *name) const
{
	obs_data_set_int(obj, name, static_cast<int>(_type));
}

void Logic::Load(obs_data_t *obj, const char *name)
{
	_type = static_cast<Type>(obs_data_get_int(obj, name));
}

bool Logic::IsRootType() const
{
	return _type < static_cast<Type>(rootOffset);
}

bool Logic::IsNegationType(Logic::Type type)
{
	return type == Type::ROOT_NOT || type == Type::AND_NOT ||
	       type == Type::OR_NOT;
	;
}

bool Logic::IsValidSelection(bool isRootCondition) const
{
	if (!IsRootType() == isRootCondition) {
		return false;
	}
	if (IsRootType() &&
	    (_type < Type::ROOT_NONE || _type >= Type::ROOT_LAST)) {
		return false;
	}
	if (!IsRootType() &&
	    (_type <= Type::ROOT_LAST || _type >= Type::LAST)) {
		return false;
	}
	return true;
}

} // namespace advss