Skip to content

Instantly share code, notes, and snippets.

@Hebali
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save Hebali/d9a74c8ebddc93fe45b0 to your computer and use it in GitHub Desktop.

Select an option

Save Hebali/d9a74c8ebddc93fe45b0 to your computer and use it in GitHub Desktop.

Revisions

  1. Hebali revised this gist Feb 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RTTI Type Traits Example
    Original file line number Diff line number Diff line change
    @@ -85,4 +85,4 @@ int main(int argc, const char * argv[])
    delete aBar;

    return 0;
    }
    }
  2. Hebali revised this gist Feb 14, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions RTTI Type Traits Example
    Original 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"; }
  3. Hebali renamed this gist Feb 14, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Hebali created this gist Feb 14, 2015.
    85 changes: 85 additions & 0 deletions Type Traits Example
    Original 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;
    }