Example: Error handling

/*
 * Expression Template Matrix Library
 *
 * Copyright (C) 2004 - 2004 Ricky Lung <mtlung@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 *  Demonstrate error handling abilities of exmat
 */

#include <iostream>
#include "../include/exmat.h"

using namespace std;
using namespace exmat;

// Define matrix with different level of error handling

// Change this #define for different level (0-2)
#define ERRCHKLEVEL 2

#if ERRCHKLEVEL ==  0
// Matrix with no error checking
typedef Mat<DenseMatCon<float,0,0>, EmptyErrorChecker> Matf;
typedef Mat<DenseMatCon<float,3,3>, EmptyErrorChecker> Mat3x3f;
#elif ERRCHKLEVEL == 1
// Matrix with default error checking
typedef Mat<DenseMatCon<float,0,0> > Matf;
typedef Mat<DenseMatCon<float,3,3> > Mat3x3f;
#elif ERRCHKLEVEL == 2
// Matrix with full error checking
typedef Mat<DenseMatCon<float,0,0>, FullErrorChecker> Matf;
typedef Mat<DenseMatCon<float,3,3>, FullErrorChecker> Mat3x3f;
#endif

// MACRO for the try/catch block
#define START() try {
#define END() } catch(std::exception& e) {  \
    cout << e.what() << endl;   \
}   \
cout << endl;

int main()
{
    /*
     *  The following codes will have one runtime error in each START/END
     *  block. The error will trigger an exception, it will be catched
     *  and information about the exception will be printed on screen
     */

    // Error checking during initialization

    // Invalid size parameter in string initialization
    START()
        // The string instruct the constructor resize the matrix of size 0x0
        Matf("0 0 1 2 3");
    END()

    // Initializing dynamic size
    START()
        // Invalid size parameter
        Matf m1(Dim(0), Dim(3), 1.0f);
    END()

    // Try to init a static size matrix from another matrix with different size
    START()
        // Init a dynamic matrix with size 2x2
        Matf m1("2 2, 1 2 3 4");
        // Try to init a static size 3x3 matrix from the above 2x2 matrix
        Mat3x3f m2(m1);
    END()

    // Try to init a static size matrix from another matrix with different size
    START()
        // Init a dynamic matrix with size 3x3
        Matf m1("3 3, 1 2 3 4 5 6 7 8 9");
        // Init a dynamic matrix with size 3x1
        Matf m2("3 1, 1 2 3");
        // Try to init a static size 3x3 matrix from the expression m1*m2
        Mat3x3f m3(m1*m2);
    END()

    // Out of bound during comma initialization
    START()
        Mat3x3f m1;
        m1 = 1, 2, 3,
             4, 5, 6,
             7, 8, 9, 10;   // <-- extra element, 10
    END()

    // Index operator bounds check
    START()
        Mat3x3f m1;
        float a = m1[1][4];
        (void)a;
    END()

    // Bounds check of an expression
    START();
        Mat3x3f m1, m2;
        // See the documentation for what's the resultant error checking policy
        // of an expression composed by matrix with different checking level
        float a = (m1*m2+m1)[3][0];
        (void)a;
    END();

    // Resize operation check
    START()
        Matf m1;
        m1.resize(0, 10);
    END();

    // Size mis-match operation

    // Assignment
    START();
        Matf m1("2 2");     // Dynamic matrix of size 2x2
        Matf m2("2 3");     // Dynamic matrix of size 2x3
        m2 = m1;
    END();

    // Binary operation
    START();
        Matf m1("2 2");     // Dynamic matrix of size 2x2
        Matf m2("2 3");     // Dynamic matrix of size 2x3
        cout << (m1+m2);
    END();

    return 0;
}

Generated on Sat May 6 23:12:04 2006 for Exmat by  doxygen 1.4.6-NO