From f052171e417c326cacb1f5b1d64f1328ab0bcda1 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 2 Apr 2013 13:18:23 -0400 Subject: [PATCH] add macros for easy adding of the basic type system in use for a number of systems --- src/framework/util/typesystem.h | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/framework/util/typesystem.h diff --git a/src/framework/util/typesystem.h b/src/framework/util/typesystem.h new file mode 100644 index 0000000..1327946 --- /dev/null +++ b/src/framework/util/typesystem.h @@ -0,0 +1,47 @@ +#ifndef __FRAMEWORK_UTIL_TYPESYSTEM_H_INCLUDED__ +#define __FRAMEWORK_UTIL_TYPESYSTEM_H_INCLUDED__ + +#include "../common.h" + +typedef const char* TYPE_IDENT; + +#define TYPE_DEFINE(type, value) \ + static #type GetType() \ + { \ + static #type typeName = #value; \ + return typeName; \ + } \ + #type GetTypeOf() const \ + { \ + return GetType(); \ + } + +#define TYPE_BASE(type) \ + virtual type GetTypeOf() const = 0; \ + template BOOL Is() const \ + { \ + return (GetTypeOf() == T::GetType()); \ + } \ + BOOL Is(type otherType) const \ + { \ + return (GetTypeOf() == otherType); \ + } \ + template T* As() \ + { \ + if (Is()) \ + return (T*)this; \ + else \ + return NULL; \ + } \ + template const T* As() const \ + { \ + if (Is()) \ + return (const T*)this; \ + else \ + return NULL; \ + } + +#define TYPE_DEFINE_DEFAULT(value) TYPE_DEFINE(TYPE_IDENT, value) +#define TYPE_BASE_DEFAULT TYPE_BASE(TYPE_IDENT) + +#endif