summaryrefslogtreecommitdiff
path: root/Runtime/BaseClasses/CleanupManager.cpp
blob: 7adb1e79e1d4ae0ec96065e0832b2ca8002e1c88 (plain)
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
#include "UnityPrefix.h"
#if UNITY_EDITOR
#include "CleanupManager.h"
#include "Runtime/Misc/GameObjectUtility.h"
#include "Runtime/Graphics/Transform.h"
#include <algorithm>

using namespace std;

void CleanupManager::MarkForDeletion( PPtr<Unity::Component> comp, std::string const& reason )
{
	// Ignore component that is already marked for deletion
	list<struct MarkedComponent>::iterator a = std::find(m_markedComponents.begin(), m_markedComponents.end(), static_cast<Unity::Component*>(comp));
	if (a != m_markedComponents.end())
		return;

	struct MarkedComponent marker;
	m_markedComponents.push_back (marker);
	m_markedComponents.back ().component = comp;
	m_markedComponents.back ().reason = reason;
}

void CleanupManager::Flush()
{
	while (m_markedComponents.size() > 0)
	{
		struct MarkedComponent& marked_component = m_markedComponents.front ();
		PPtr<Unity::Component> comp = marked_component.component;
		
		Unity::Component* compPtr = comp;
		if (compPtr)
		{
			LogString(Format("%s component deleted: %s", comp->GetClassName ().c_str (), marked_component.reason.c_str()));
			if (marked_component.component->GetGameObjectPtr () != NULL)
			{
				DestroyObjectHighLevel (comp);
			}
			else
			{
				// if the component is a transform remove the references
				if (comp->GetClassID () == ClassID(Transform))
				{
					DestroyTransformComponentAndChildHierarchy(static_cast<Transform&>(*comp));
				}
				
				DestroySingleObject (comp);
			}
		}

		m_markedComponents.pop_front ();
	}
}

static CleanupManager* singleton = NULL;
CleanupManager& GetCleanupManager ()
{
	if (singleton == NULL)
	{
		singleton = new CleanupManager();
	}

	return *singleton;
}

#endif