Last active
August 29, 2015 14:15
-
-
Save Hebali/d9a74c8ebddc93fe45b0 to your computer and use it in GitHub Desktop.
Revisions
-
Hebali revised this gist
Feb 14, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -85,4 +85,4 @@ int main(int argc, const char * argv[]) delete aBar; return 0; } -
Hebali revised this gist
Feb 14, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ // RTTI Type Traits // Example by Patrick Hebron template <typename T> struct TypeTraits { static const char* name() { return "TYPE_NAME"; } -
Hebali renamed this gist
Feb 14, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Hebali created this gist
Feb 14, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,85 @@ template <typename T> struct TypeTraits { static const char* name() { return "TYPE_NAME"; } }; struct Base { Base() {} virtual ~Base() {} virtual void testPrint() = 0; virtual void testPrintTraitName() = 0; }; template <typename DerivedT> struct BaseImpl : public Base { BaseImpl() {} }; #define DECLARE_DERIVED_TYPE(Type,Symbol) \ template<> struct TypeTraits<struct Type> \ { \ typedef Type type; \ static const char* name() { return Symbol; } \ }; \ struct Type : public BaseImpl<Type> \ DECLARE_DERIVED_TYPE( Foo, "FOO_TYPE" ) { Foo() {} void testPrint() { std::cout << "FOO::testPrint()" << std::endl; }; void testPrintTraitName() { std::cout << TypeTraits<Foo>::name() << std::endl; }; void testFoo() { std::cout << "FOO::testFoo()" << std::endl; }; }; DECLARE_DERIVED_TYPE( Bar, "BAR_TYPE" ) { Bar() {} void testPrint() { std::cout << "BAR::testPrint()" << std::endl; }; void testPrintTraitName() { std::cout << TypeTraits<Bar>::name() << std::endl; }; void testBar() { std::cout << "BAR::testBar()" << std::endl; }; }; int main(int argc, const char * argv[]) { using namespace std; Foo* aFoo = new Foo(); Bar* aBar = new Bar(); Base* aFooBaseCast = aFoo; Base* aBarBaseCast = aBar; aFoo->testPrint(); aFoo->testPrintTraitName(); aFoo->testFoo(); cout << endl; aBar->testPrint(); aBar->testPrintTraitName(); aBar->testBar(); cout << endl; aFooBaseCast->testPrint(); aFooBaseCast->testPrintTraitName(); cout << endl; aBarBaseCast->testPrint(); aBarBaseCast->testPrintTraitName(); cout << endl; delete aFoo; delete aBar; return 0; }