Adding Exceptions & RTTI to the Windows CE Compiler: Part II
by Dani Carles

Example 1:
template <class T> class tcu__RttiTypeIdImplementation
{
public:
   static const void *s_id;
};
template <class T> const void *tcu__RttiTypeIdImplementation<T>::s_id = 0;

Example 2:
template <class T> class tcu__RttiTypeIdImplementation
{
   static const void *s_id;
public:
   static const void *GetUniqueId()
   {
      return &s_id;
   }
};
template <class T> const void * tcu__RttiTypeIdImplementation<T>::s_id = 0;

Example 3:
const void * tcu_XhOsXface::GetUniqueId(const char *s)
{
   return (const void *) RegisterWindowMessageA(s);
}


Listing Four
{
    tcu__XTryBlock __tcuXtb__;
    tcu_Xc *__tcuXcPtr__;
    __tcuXcPtr__ = 0;

    if (!_setjmp (__tcuXtb__.jmpBuf()))
    {
        {
            DerivedObj d;
            foo();
        }
    } else if ((__tcuXcPtr__=
           tcu_rttiDynamicCast<ExcDerived>(__tcuXtb__.valuePtr())) != 0)
    {
        ExcDerived &e = *reinterpret_cast<ExcDerived *>(__tcuXcPtr__);
        __tcuXtb__.valuePtr() = 0;
        {
            cout << "ExcDerived caught: " << e.what() <<
                                        " (0x" << &e << ")" << endl;
        }
    } else if ((__tcuXcPtr__=
           tcu_rttiDynamicCast<ExcBase>(__tcuXtb__.valuePtr())) != 0)
    {
        ExcBase &e = *reinterpret_cast<ExcBase *>(__tcuXcPtr__);
        __tcuXtb__.valuePtr() = 0;
        {
            cout << "ExcBase caught: " << e.what() <<
                                     " (0x" << &e << ")" << endl;
        }
    } else
    {
        __tcuXtb__.valuePtr() = 0;
        {
            cout << "Unknown exception" << endl;
        }
    }
}



Listing Five
#include <iostream>
#include <cassert>
#include "tcutest.h"

using namespace std;

// File scope functions
namespace {
void bar()
{
    cout << "In bar() ..." << endl;
    UnwindableObj bar1,bar2;
    TCU_X_THROW(ExcDerived("A derived exception"));
}
void foo()
{
    cout << "In foo() ..." << endl;
    UnwindableObj foo1,foo2;
    TCU_X_TRY
    {
        bar();
    }
    TCU_X_CATCH_TYPE(ExcDerived)
    {
        UnwindableObj foo3,foo4;
        cout << "ExcDerived caught in foo " << endl;
        TCU_X_THROW(ExcDerived("A different derived exception"));
    } TCU_X_END_TRY
}
}
// Static member definition
unsigned int UnwindableObj::mv_nCount = 0;
// main
int main()
{
    cout << "Throwing inside a catch block using the
                                 original TCU library" << endl << endl;
    // EH test
    TCU_X_TRY
    {
        DerivedObj d;
        foo();
    }
    TCU_X_CATCH(ExcDerived,e)
    {
      cout << "ExcDerived caught: " << e.what() <<
                                  " (0x" << &e << ")" << endl;
    }
    TCU_X_CATCH(ExcBase,e)
    {
      cout << "ExcBase caught: " << e.what() << " (0x" << &e << ")" << endl;
    }
    TCU_X_CATCH_ALL
    {
      cout << "Unknown exception" << endl;
    } TCU_X_END_TRY
    assert(UnwindableObj::getCtorCount() == 0);
    cout << "Press Enter to exit" << endl;
    cin.get();
    return 0;
}



Listing Six
#include <iostream>
#include <cassert>
#include "tcutest.h"
using namespace std;
// File scope functions
namespace {
void bar()
{
    TCU_X_TRY
    {
        cout << "In bar() ..." << endl;
    }
    TCU_X_CATCH_TYPE(ExcDerived)
    {
        cout << "ExcDerived caught in bar " << endl;
    } TCU_X_END_TRY
}
void foo()
{
    cout << "In foo() ..." << endl;
    TCU_X_THROW(ExcDerived("A derived exception"));
}
}
// Static member definition
unsigned int UnwindableObj::mv_nCount = 0;
// main
int main()
{
    bool bRethrownCaught = false;
    TCU_X_TRY
    {
        TCU_X_TRY
        {
            foo();
        }
        TCU_X_CATCH(ExcDerived,e)
        {
            cout << "ExcDerived caught: " << e.what() <<
                                        " (0x" << &e << ")" << endl;
            bar();
            TCU_X_RETHROW;
        } TCU_X_END_TRY
    }
    TCU_X_CATCH(ExcDerived,e)
    {
        cout << "Rethrown ExcDerived caught: " << e.what() <<
                                             " (0x" << &e << ")" << endl;
        bRethrownCaught = true;
    } TCU_X_END_TRY
    assert(bRethrownCaught);
    cout << "Press Enter to exit" << endl;
    cin.get();
    return 0;
}



Listing Seven
#include <iostream>
#include <cassert>
#include "tcutest.h"

using namespace std;

// File scope functions
namespace {

DerivedObj bar()
{
    cout << "In bar() ..." << endl;
    DerivedObj ret;
    printUnwObjList();
    return ret;
}
void foo()
{
    cout << "In foo() ..." << endl;
    printUnwObjList();
    DerivedObj d = bar();
    printUnwObjList();
    TCU_X_THROW(ExcDerived("A derived exception"));
}
}
// Static member definition
unsigned int UnwindableObj::mv_nCount = 0;
// main
int main()
{
    cout << "Showing destruction order problem using the original
                                              TCU library" << endl << endl;
    // EH test
    TCU_X_TRY
    {
        foo();
    }
    TCU_X_CATCH(ExcDerived,e)
    {
      cout << "ExcDerived caught:" << e.what() << "(0x" << &e << ")" << endl;
    }
    TCU_X_CATCH(ExcBase,e)
    {
      cout << "ExcBase caught: " << e.what() << " (0x" << &e << ")" << endl;
    }
    TCU_X_CATCH_ALL
    {
        cout << "Unknown exception" << endl;
    } TCU_X_END_TRY
    assert(UnwindableObj::getCtorCount() == 0);
    cout << "Press Enter to exit" << endl;
    cin.get();
    return 0;
}





