Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created June 22, 2011 05:22
Show Gist options
  • Save hpcx82/1039556 to your computer and use it in GitHub Desktop.
Save hpcx82/1039556 to your computer and use it in GitHub Desktop.
#include <string.h>
#define RUNTIME_TYPE_ID_ROOT(typestr) \
static const char* Type() {return typestr;} \
virtual bool IsKindOf(const char* typeName) {return 0 == strcmp(typeName, Type());}
// how about multiple inheritance
#define RUNTIME_TYPE_ID(typestr, baseclass) \
static const char* Type() {return typestr;} \
virtual bool IsKindOf(const char* typeName) {return (0 == strcmp(typeName, Type())) || baseclass::IsKindOf(typeName);}
class IObject
{
public:
virtual ~IObject() {}
RUNTIME_TYPE_ID_ROOT("IObject");
};
class IGeometry: public IObject
{
public:
RUNTIME_TYPE_ID("IGeometry", IObject);
};
class IStyle: public IObject
{
public:
RUNTIME_TYPE_ID("IStyle", IObject);
};
class IBrep: public IGeometry
{
public:
RUNTIME_TYPE_ID("IBrep", IGeometry);
};
int _tmain(int argc, _TCHAR* argv[])
{
IBrep* pBrep = new IBrep;
IStyle* pStyle = new IStyle;
bool bOk = false;
bOk = pBrep->IsKindOf(IBrep::Type());
bOk = pBrep->IsKindOf(IGeometry::Type());
bOk = pBrep->IsKindOf(IObject::Type());
bOk = pBrep->IsKindOf(IStyle::Type());
bOk = pStyle->IsKindOf(IStyle::Type());
bOk = pStyle->IsKindOf(IObject::Type());
bOk = pStyle->IsKindOf(IGeometry::Type());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment