about summary refs log tree commit diff
path: root/src/engine/kernel.h
blob: 288f49ab1a902b52f535b168b46f8d2f5be52b25 (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
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com.                */
#ifndef ENGINE_KERNEL_H
#define ENGINE_KERNEL_H

#include <base/system.h>

class IKernel;
class IInterface;

class IInterface
{
	// friend with the kernel implementation
	friend class CKernel;
	IKernel *m_pKernel;
protected:
	IKernel *Kernel() { return m_pKernel; }
public:
	IInterface() : m_pKernel(0) {}
	virtual ~IInterface() {}
	
	//virtual unsigned InterfaceID() = 0;
	//virtual const char *InterfaceName() = 0;
};

#define MACRO_INTERFACE(Name, ver) \
	public: \
		static const char *InterfaceName() { return Name; } \
	private:
	
		//virtual unsigned InterfaceID() { return INTERFACE_ID; }
		//virtual const char *InterfaceName() { return name; }


// This kernel thingie makes the structure very flat and basiclly singletons.
// I'm not sure if this is a good idea but it works for now.
class IKernel
{
	// hide the implementation
	virtual bool RegisterInterfaceImpl(const char *InterfaceName, IInterface *pInterface) = 0;
	virtual bool ReregisterInterfaceImpl(const char *InterfaceName, IInterface *pInterface) = 0;
	virtual IInterface *RequestInterfaceImpl(const char *InterfaceName) = 0;
public:
	static IKernel *Create();
	virtual ~IKernel() {}

	// templated access to handle pointer convertions and interface names
	template<class TINTERFACE>
	bool RegisterInterface(TINTERFACE *pInterface)
	{
		return RegisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface);
	}
	template<class TINTERFACE>
	bool ReregisterInterface(TINTERFACE *pInterface)
	{
		return ReregisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface);
	}
	
	// Usage example:
	//		IMyInterface *pMyHandle = Kernel()->RequestInterface<IMyInterface>()
	template<class TINTERFACE>
	TINTERFACE *RequestInterface()
	{
		return reinterpret_cast<TINTERFACE *>(RequestInterfaceImpl(TINTERFACE::InterfaceName()));
	}
};

#endif