Example: Derive your own classes

/*
 * Expression Template Matrix Library
 *
 * Copyright (C) 2004 - 2006 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 how to derive your own class from exmat::Mat/Vec
 */

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

using namespace std;
using namespace exmat;

// Define some matrix types

namespace MyNS {

// We choose DenseMatCon<float, 3, 3> as our default container
template<class Rep=::exmat::Mat<DenseMatCon<float, 3, 3> > >
class MyMatrix : public Rep
{
    typedef Rep             super_type;
    typedef MyMatrix<Rep>   self_type;
    // This macro defines the necessary typedefs and constructors
    // The last parameter means this is a matrix, pass Vec if this is a vector
    EXMAT_DERIVEDCLASS_IMP(MyMatrix, Mat);

public:
    // Since assignment operators cannot be derived, we need to define them
    // Of course you can make your own if necessary
    EXMAT_DERIVEDCLASS_ASSIGNOP(MyMatrix, Mat);

    // "Square" of this matrix
    MyMatrix Square() const {
        return (*this) * (*this);
    }

    // Print the matrix
    void Print() const {
        cout << *(static_cast<const super_type*>(this)) << endl;
    }
};  // MyMatrix

}   // namespace MyNS

// Enable the default operators (+-*) between MyMatrix, exmat::Mat/Vec and scalar
// This macro have to be used at global scope (outside any namespace)
// Note the use of ::MyNS::MyMatrix with the namespace applied
EXMAT_DERIVEDCLASS_DEFALUTOPS(::MyNS::MyMatrix, Mat);


int main()
{
    typedef MyNS::MyMatrix<> Mat3x3f;
    Mat3x3f m1("0 1 2\n\
                3 4 5\n\
                6 7 8\n\
               ");

    (m1 * 2).Print();

    m1.Square().Print();

    return 0;
}

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