-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_ElementResource.h
61 lines (52 loc) · 1.67 KB
/
GUI_ElementResource.h
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
#pragma once
/**
* Used for sharing common graphical resources among GUI elements.
* Make sure all references to this resource were deleted before deleting the resource itself!
*/
class GUI_ElementResource
{
friend class GUI_Looks;
public:
GUI_ElementResource(SURFHANDLE surface) : surface(surface) {};
~GUI_ElementResource() {
assert(references.size() == 0 && "Destroying resource that is still referenced!");
oapiDestroySurface(surface);
};
/**
* \return the number of elements referencing this resource.
*/
int NumReferences() { return references.size(); };
/**
* \return true if the resource is compatible with the passed element, false otherwise.
*/
bool IsCompatibleWith(GUI_BaseElement *element)
{
return references.size() > 0 && references[0]->IsResourceCompatibleWith(element);
};
/**
* \return True if this resource is already referenced by the passed element.
*/
bool IsReferencedByElement(GUI_BaseElement *element)
{
auto i = find(references.begin(), references.end(), element);
return i != references.end();
}
/**
* Retrieves the surfhandle
*/
SURFHANDLE GetSurface() { return surface; };
private:
SURFHANDLE surface = NULL;
vector<GUI_BaseElement*> references;
void addReference(GUI_BaseElement *element)
{
assert(find(references.begin(), references.end(), element) == references.end() && "Attempting to register resource twice!");
references.push_back(element);
}
void removeReference(GUI_BaseElement *element)
{
auto reference = find(references.begin(), references.end(), element);
assert(reference != references.end() && "Attempting to remove reference that was never registered!");
references.erase(reference);
}
};