summaryrefslogtreecommitdiff
path: root/Runtime/BaseClasses/MessageIdentifier.h
blob: 9469ef86419f04002e7ffc025c359f6ad3887313 (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
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
#ifndef MESSAGEIDENTIFIER_H
#define MESSAGEIDENTIFIER_H

#include <list>
#include <typeinfo>
#include "Runtime/Utilities/LogAssert.h"
#include "Runtime/Misc/Allocator.h"
#include <map>
#include "Runtime/Allocator/MemoryMacros.h"
#include "Runtime/Scripting/Backend/ScriptingTypes.h"
#include "Runtime/BaseClasses/ObjectDefines.h"
#include "Runtime/BaseClasses/ClassIDs.h"

struct MessageData
{
	int type;
private:
	// Note: on Metro WinRT types cannot be located in union, so don't use union!
	intptr_t data;
	ScriptingObjectPtr scriptingObjectData;
public:
	MessageData () : scriptingObjectData(SCRIPTING_NULL)
	{ 
		data = 0; type = 0; 
	}
	
	template<class T>
	void SetData (T inData, int classId)
	{
		// Check if SetData is used instead of SetScriptingObjectData 
		Assert (type != ClassID (MonoObject));
		AssertIf (sizeof (T) > sizeof (data)); // increase the data size
		*reinterpret_cast<T*> (&data) = *reinterpret_cast<T*> (&inData);
		type = classId;
	}
	
	template<class T>
	T GetData ()
	{
		// Check if GetData is used instead of GetScriptingObjectData 
		Assert (type != ClassID (MonoObject));
		return *reinterpret_cast<T*> (&data);
	}

	intptr_t& GetGenericDataRef ()
	{
		// Check if GetGenericDataRef is used instead of GetScriptingObjectData 
		Assert (type != ClassID (MonoObject));
		return data;
	}

	void SetScriptingObjectData (ScriptingObjectPtr inData)
	{
		scriptingObjectData = inData;
		type = ClassID (MonoObject);
	}
	
	ScriptingObjectPtr GetScriptingObjectData ()
	{
		Assert (type == ClassID (MonoObject));
		return scriptingObjectData;
	}

};

// usage:
// MessageIdentifier kOnTransformChanged ("OnTransformChanged", MessageIdentifier::kDontSendToScripts);
// MessageIdentifier kOnTransformChanged ("OnTransformChanged", MessageIdentifier::kDontSendToScripts, ClassID (int));


class MessageIdentifier
{
	public:
	
	enum Options { kDontSendToScripts = 0, kSendToScripts = 1 << 0, kUseNotificationManager = 1 << 1, kDontSendToDisabled = 1 << 2 };
	
	const char* messageName;
	const char* scriptParameterName;
	int			messageID;
	int			parameterClassId;
	int         options;
	
	/// Place the MessageIdentifier as a global variable!
	/// The constructor must be called before InitializeEngine
	explicit MessageIdentifier (const char* name, Options opt, int classId = 0, const char* scriptParamName = NULL);
		
	typedef std::list<MessageIdentifier*, STL_ALLOCATOR(kMemPermanent, MessageIdentifier*) > RegisteredMessages;
	typedef std::map<std::string, MessageIdentifier> SortedMessages;
	
	// All registered message identifiers
	static RegisteredMessages& GetRegisteredMessages ();
	// Sorted list of message identifiers
	// - Duplicate message identifiers are ignored
	// - Only notification messages are returned if onlyNotificationMessages otherwise only normal messages are returned (See kUseNotificationManager)
	static SortedMessages GetSortedMessages (bool onlyNotificationMessages);

	static void Cleanup ();
	
	// Only to be used by subclasses
	MessageIdentifier ()
	{
		messageID = -1; scriptParameterName = NULL; messageName = NULL;
		options = kSendToScripts;
	}
};

#include "MessageIdentifiers.h"

#endif