-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathExpressionVariableWatcher.cpp
More file actions
129 lines (100 loc) · 3.58 KB
/
ExpressionVariableWatcher.cpp
File metadata and controls
129 lines (100 loc) · 3.58 KB
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
119
120
121
122
123
124
125
126
127
128
129
#include "ExpressionVariableWatcher.hpp"
#include "Utils.hpp"
#include <projectM-4/debug.h>
#include <cstdint>
#include <string>
#include <utility>
namespace libprojectM {
auto ExpressionVariableWatcher::Add(ExpressionBlocks block, uint32_t index, VariableName variableName) -> const ExpressionVariableWatcher::VariableValues*
{
// Validate block value only, set index to 0 in blocks only occurring once.
switch (block)
{
case PROJECTM_EXPR_PER_FRAME:
case PROJECTM_EXPR_PER_POINT:
index = 0;
case PROJECTM_EXPR_SHAPE_PER_FRAME:
case PROJECTM_EXPR_WAVE_PER_FRAME:
case PROJECTM_EXPR_WAVE_PER_POINT:
case PROJECTM_EXPR_MILKDROP_SPRITE:
break;
default:
return nullptr;
}
libprojectM::Utils::ToLowerInPlace(variableName);
IndexedExpressionBlock const indexedBlock = std::make_pair(block, index);
auto blockInMap = m_watches.find(indexedBlock);
if (blockInMap == m_watches.end())
{
blockInMap = m_watches.insert({indexedBlock, {}}).first;
}
// If the watch was already registered with the given, return the existing pointer again.
auto variableWatch = blockInMap->second.find(variableName);
if (variableWatch != blockInMap->second.end())
{
return &variableWatch->second;
}
// New watch. Create a new value struct and return it.
return &blockInMap->second.insert({variableName, {}}).first->second;
}
auto ExpressionVariableWatcher::Remove(ExpressionBlocks block, uint32_t index, VariableName variableName) -> const ExpressionVariableWatcher::VariableValues*
{
// Validate block value only, set index to 0 in blocks only occurring once.
switch (block)
{
case PROJECTM_EXPR_PER_FRAME:
case PROJECTM_EXPR_PER_POINT:
index = 0;
case PROJECTM_EXPR_SHAPE_PER_FRAME:
case PROJECTM_EXPR_WAVE_PER_FRAME:
case PROJECTM_EXPR_WAVE_PER_POINT:
case PROJECTM_EXPR_MILKDROP_SPRITE:
break;
default:
return nullptr;
}
libprojectM::Utils::ToLowerInPlace(variableName);
IndexedExpressionBlock const indexedBlock = std::make_pair(block, index);
auto blockInMap = m_watches.find(indexedBlock);
if (blockInMap == m_watches.end())
{
return nullptr;
}
auto variableWatch = blockInMap->second.find(variableName);
if (variableWatch == blockInMap->second.end())
{
return nullptr;
}
// Watch found, make a copy of the values struct pointer, then delete it.
const auto* oldValuesStruct = &variableWatch->second;
blockInMap->second.erase(variableWatch);
return oldValuesStruct;
}
void ExpressionVariableWatcher::Clear()
{
m_watches.clear();
}
auto ExpressionVariableWatcher::GetBlockWatches(ExpressionBlocks block, uint32_t index) -> BlockWatches&
{
// Validate block value only.
switch (block)
{
case PROJECTM_EXPR_PER_FRAME:
case PROJECTM_EXPR_PER_POINT:
case PROJECTM_EXPR_SHAPE_PER_FRAME:
case PROJECTM_EXPR_WAVE_PER_FRAME:
case PROJECTM_EXPR_WAVE_PER_POINT:
case PROJECTM_EXPR_MILKDROP_SPRITE:
break;
default:
throw InvalidBlockException("Block type " + std::to_string(block) + " is not supported!");
}
IndexedExpressionBlock const indexedBlock = std::make_pair(block, index);
auto blockInMap = m_watches.find(indexedBlock);
if (blockInMap == m_watches.end())
{
blockInMap = m_watches.insert({indexedBlock, {}}).first;
}
return blockInMap->second;
}
} // namespace libprojectM