GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
optmatrix.h
00001 /*
00002  * The GURLS Package in C++
00003  *
00004  * Copyright (C) 2011-1013, IIT@MIT Lab
00005  * All rights reserved.
00006  *
00007  * authors:  M. Santoro
00008  * email:   msantoro@mit.edu
00009  * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  *
00015  *     * Redistributions of source code must retain the above
00016  *       copyright notice, this list of conditions and the following
00017  *       disclaimer.
00018  *     * Redistributions in binary form must reproduce the above
00019  *       copyright notice, this list of conditions and the following
00020  *       disclaimer in the documentation and/or other materials
00021  *       provided with the distribution.
00022  *     * Neither the name(s) of the copyright holders nor the names
00023  *       of its contributors or of the Massacusetts Institute of
00024  *       Technology or of the Italian Institute of Technology may be
00025  *       used to endorse or promote products derived from this software
00026  *       without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00031  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00032  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00033  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00034  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00038  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039  * POSSIBILITY OF SUCH DAMAGE.
00040  */
00041 
00042 
00043 #ifndef _GURLS_OPTMATRIX_H_
00044 #define _GURLS_OPTMATRIX_H_
00045 
00046 #include <gurls++/options.h>
00047 
00048 #ifdef _BGURLS
00049 #include <bgurls++/bigarray.h>
00050 #endif
00051 
00052 namespace gurls
00053 {
00054 
00059 class GURLS_EXPORT OptMatrixBase: public GurlsOption
00060 {
00061 public:
00062 
00066     OptMatrixBase(): GurlsOption(MatrixOption){}
00067 
00072     enum MatrixType{FLOAT, DOUBLE, ULONG};
00073 
00077     MatrixType getMatrixType() const
00078     {
00079         return matType;
00080     }
00081 
00082 #ifdef _BGURLS
00083 
00086     virtual bool hasBigArray() const = 0;
00087 #endif
00088 
00089 
00090 protected:
00091     MatrixType matType; 
00092 };
00093 
00094 
00095 template<class Matrix>
00096 OptMatrixBase::MatrixType getMatrixCellType()
00097 {
00098     throw gException(Exception_Unsupported_MatrixType);
00099 }
00100 
00101 template<>
00102 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<gMat2D<float> >();
00103 
00104 template<>
00105 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<gMat2D<double> >();
00106 
00107 template<>
00108 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<const gMat2D<float> >();
00109 
00110 template<>
00111 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<const gMat2D<double> >();
00112 
00113 template<>
00114 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<gMat2D<unsigned long> >();
00115 
00116 #ifdef _BGURLS
00117 
00118 template<>
00119 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<BigArray<float> >();
00120 
00121 template<>
00122 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<BigArray<double> >();
00123 
00124 template<>
00125 GURLS_EXPORT OptMatrixBase::MatrixType getMatrixCellType<BigArray<unsigned long> >();
00126 
00127 #endif
00128 
00129 
00135 template <typename Matrix>
00136 class OptMatrix: public OptMatrixBase
00137 {
00138 private:
00139     Matrix* value;  
00140     bool isOwner;   
00141 
00142 public:
00143     typedef Matrix ValueType;
00144 
00148     OptMatrix(): OptMatrixBase () , value (new Matrix()), isOwner(true)
00149     {
00150         this->matType = getMatrixCellType<Matrix>();
00151     }
00152 
00156     OptMatrix(Matrix& m, bool owner = true): OptMatrixBase(), value(&m), isOwner(owner)
00157     {
00158         this->matType = getMatrixCellType<Matrix>();
00159     }
00160 
00164     OptMatrix<Matrix>& operator=(const OptMatrix<Matrix>& other);
00165 
00169     ~OptMatrix()
00170     {
00171         if(isOwner)
00172             delete value;
00173     }
00174 
00178     void detachValue()
00179     {
00180         isOwner = false;
00181     }
00182 
00186     OptMatrix& operator=(const Matrix& other)
00187     {
00188         setValue(other);
00189 
00190         this->type = MatrixOption;
00191 
00192         return *this;
00193     }
00194 
00198     void setValue(const Matrix& newvalue)
00199     {
00200         ~OptMatrix();
00201 
00202         value = new Matrix(newvalue);
00203         this->isOwner = true;
00204     }
00205 
00209     Matrix& getValue()
00210     {
00211         return *value;
00212     }
00213 
00217     const Matrix& getValue() const
00218     {
00219         return *value;
00220     }
00221 
00225     virtual bool isA(OptTypes id) const
00226     {
00227         return (id == MatrixOption);
00228     }
00229 
00230 #ifdef _BGURLS
00231 
00234     virtual bool hasBigArray() const
00235     {
00236         return false;
00237     }
00238 #endif
00239 
00243     static OptMatrix* dynacast(GurlsOption* opt)
00244     {
00245         if (opt->isA(MatrixOption) )
00246             return static_cast<OptMatrix*>(opt);
00247 
00248         throw gException(gurls::Exception_Illegal_Dynamic_Cast);
00249     }
00250 
00254     static const OptMatrix* dynacast(const GurlsOption* opt)
00255     {
00256         if (opt->isA(MatrixOption) )
00257             return static_cast<const OptMatrix*>(opt);
00258 
00259         throw gException(gurls::Exception_Illegal_Dynamic_Cast);
00260     }
00261 
00265     virtual std::ostream& operator<<(std::ostream& os) const;
00266 
00267 };
00268 
00269 #ifdef _BGURLS
00270 
00271 template <>
00272 bool OptMatrix<BigArray<float> >::hasBigArray() const{return true;};
00273 
00274 template <>
00275 bool OptMatrix <BigArray<double> >::hasBigArray() const{return true;};
00276 
00277 template <>
00278 bool OptMatrix <BigArray<unsigned long> >::hasBigArray() const{return true;};
00279 
00280 #endif
00281 
00282 
00286 template <typename T>
00287 std::ostream& OptMatrix<T>::operator << (std::ostream& os) const
00288 {
00289     return os << std::endl << this->getValue();
00290 }
00291 
00292 }
00293 
00294 #endif //_GURLS_OPTMATRIX_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends