add some doc comments to the type-system header

This commit is contained in:
Gered 2013-04-02 14:01:32 -04:00
parent c9d9152e45
commit 3993326711

View file

@ -3,45 +3,77 @@
#include "../common.h" #include "../common.h"
// default generic type system identifier type
typedef const char* TYPE_IDENT; typedef const char* TYPE_IDENT;
#define TYPE_DEFINE(type, value) \ // The macros below can be used to quickly add methods to classes which allow
static type GetType() \ // a "poor-mans" run-time type system to be used. This type system allows for:
{ \ //
static type typeName = value; \ // - retrieval of a type-identifier at run-time via either:
return typeName; \ // object.GetTypeOf() (instance method) or
} \ // ObjectType::GetType() (static method)
type GetTypeOf() const \ //
{ \ // - comparison of two types using either template methods (static) or
return GetType(); \ // variables containing type identifiers
// BOOL result = object.Is<ObjectType>() or
// BOOL result = object.Is(objectTypeId)
//
// - "safe" casting - if the type doesn't match, NULL is returned
// ObjectType* type = object.As<ObjectType>()
/**
* Adds methods to a base type class that implement type identifier retrieval
* (which will be overridden by subclasses) and type comparison and safe
* casting between types.
* @param type the compiler type used for the type identifier
*/
#define TYPE_BASE(type) \
virtual type GetTypeOf() const = 0; \
template<class T> BOOL Is() const \
{ \
return (GetTypeOf() == T::GetType()); \
} \
BOOL Is(type otherType) const \
{ \
return (GetTypeOf() == otherType); \
} \
template<class T> T* As() \
{ \
if (Is<T>()) \
return (T*)this; \
else \
return NULL; \
} \
template<class T> const T* As() const \
{ \
if (Is<T>()) \
return (const T*)this; \
else \
return NULL; \
} }
#define TYPE_BASE(type) \ /**
virtual type GetTypeOf() const = 0; \ * Adds methods used by subclasses of a type that defines methods for
template<class T> BOOL Is() const \ * type identifier retrieval and comparisons
{ \ * @param type the compiler type used for the type identifier for this class
return (GetTypeOf() == T::GetType()); \ * @param value the value to assign to this class's type identifier
} \ */
BOOL Is(type otherType) const \ #define TYPE_DEFINE(type, value) \
{ \ static type GetType() \
return (GetTypeOf() == otherType); \ { \
} \ static type typeName = value; \
template<class T> T* As() \ return typeName; \
{ \ } \
if (Is<T>()) \ type GetTypeOf() const \
return (T*)this; \ { \
else \ return GetType(); \
return NULL; \
} \
template<class T> const T* As() const \
{ \
if (Is<T>()) \
return (const T*)this; \
else \
return NULL; \
} }
#define TYPE_DEFINE_DEFAULT(value) TYPE_DEFINE(TYPE_IDENT, value) // these macros work the same as the above two, but they will use
// the TYPE_IDENT typedef defined above as the type identifier compiler type
#define TYPE_BASE_DEFAULT TYPE_BASE(TYPE_IDENT) #define TYPE_BASE_DEFAULT TYPE_BASE(TYPE_IDENT)
#define TYPE_DEFINE_DEFAULT(value) TYPE_DEFINE(TYPE_IDENT, value)
#endif #endif