2010-11-20 10:37:14 +00:00
|
|
|
/* (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. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#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;
|
2020-09-26 19:41:58 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
protected:
|
|
|
|
IKernel *Kernel() { return m_pKernel; }
|
2020-09-26 19:41:58 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
2020-09-26 19:41:58 +00:00
|
|
|
IInterface() :
|
2022-06-13 16:28:13 +00:00
|
|
|
m_pKernel(nullptr) {}
|
2022-09-16 14:31:36 +00:00
|
|
|
virtual void Shutdown() {}
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual ~IInterface() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MACRO_INTERFACE(Name, ver) \
|
2020-09-26 19:41:58 +00:00
|
|
|
public: \
|
|
|
|
static const char *InterfaceName() { return Name; } \
|
|
|
|
\
|
|
|
|
private:
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2022-10-25 16:51:56 +00:00
|
|
|
// This kernel thingie makes the structure very flat and basically singletons.
|
2010-05-29 07:25:38 +00:00
|
|
|
// I'm not sure if this is a good idea but it works for now.
|
|
|
|
class IKernel
|
|
|
|
{
|
|
|
|
// hide the implementation
|
2022-06-30 22:36:32 +00:00
|
|
|
virtual bool RegisterInterfaceImpl(const char *pInterfaceName, IInterface *pInterface, bool Destroy) = 0;
|
|
|
|
virtual bool ReregisterInterfaceImpl(const char *pInterfaceName, IInterface *pInterface) = 0;
|
|
|
|
virtual IInterface *RequestInterfaceImpl(const char *pInterfaceName) = 0;
|
2020-09-26 19:41:58 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
|
|
|
static IKernel *Create();
|
2022-09-16 14:31:36 +00:00
|
|
|
virtual void Shutdown() = 0;
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual ~IKernel() {}
|
|
|
|
|
2018-07-10 09:29:02 +00:00
|
|
|
// templated access to handle pointer conversions and interface names
|
2010-05-29 07:25:38 +00:00
|
|
|
template<class TINTERFACE>
|
2017-07-28 12:49:09 +00:00
|
|
|
bool RegisterInterface(TINTERFACE *pInterface, bool Destroy = true)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2017-07-28 12:49:09 +00:00
|
|
|
return RegisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface, Destroy);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
template<class TINTERFACE>
|
|
|
|
bool ReregisterInterface(TINTERFACE *pInterface)
|
|
|
|
{
|
|
|
|
return ReregisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// Usage example:
|
|
|
|
// IMyInterface *pMyHandle = Kernel()->RequestInterface<IMyInterface>()
|
|
|
|
template<class TINTERFACE>
|
|
|
|
TINTERFACE *RequestInterface()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<TINTERFACE *>(RequestInterfaceImpl(TINTERFACE::InterfaceName()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|