00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CMatrixTemplateObjects_H 00029 #define CMatrixTemplateObjects_H 00030 00031 #include <mrpt/math/CMatrixTemplate.h> 00032 00033 namespace mrpt 00034 { 00035 namespace math 00036 { 00037 /** This template class extends the class "CMatrixTemplate" for storing "objects" at each matrix entry. 00038 * This class allows a very efficient representation of sparse matrixes where each cell is an arbitrary C++ class, 00039 * but its use must carefully observe the following rules: 00040 * - The type in the template especialization MUST be a class with a proper default constructor. 00041 * - Initially all entries are set to NULL pointers. 00042 * - Pointers can be manually asigned, or automatically created through a call to "CMatrixTemplateObjects<T>::allocAllObjects" 00043 * - Independently of how pointers are asigned, memory will be free by destroying objects for each non-NULL entry in the matrix. In some special situations, the user can indicate not to free those objects by calling "CMatrixTemplateObjects<T>::setDestroyBehavior", then it is up to the user to free the memory. In all cases the default behavior is to free the memory. 00044 * - Asignament operator with matrixes will COPY THE POINTERS, thus a copy of objects is not performed. 00045 * - WARNING: Objects are not deleted while shrinking the matrix by calling "setSize", thus please call ""CMatrixTemplateObjects<T>::freeAllObjects" or manually delete objects before shrinking. 00046 * 00047 * \sa CMatrixTemplate 00048 */ 00049 template <class T> 00050 class CMatrixTemplateObjects : public CMatrixTemplate<T*> 00051 { 00052 private: 00053 bool m_freeObjects; 00054 00055 public: 00056 /** Copy constructor 00057 */ 00058 CMatrixTemplateObjects(const CMatrixTemplate<T>& m) : CMatrixTemplate<T*>( m ), m_freeObjects(true) 00059 { 00060 } 00061 00062 /** Constructor 00063 */ 00064 CMatrixTemplateObjects(size_t row = 3, size_t col = 3) : CMatrixTemplate<T*>( row, col ), m_freeObjects(true) 00065 { 00066 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00067 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00068 CMatrixTemplate<T*>::m_Val[i][j] = NULL; 00069 } 00070 00071 /** Changes the size of matrix 00072 */ 00073 virtual void setSize(size_t row, size_t col) 00074 { 00075 CMatrixTemplate<T*>::realloc(row,col,true); 00076 } 00077 00078 /** Destructor 00079 */ 00080 virtual ~CMatrixTemplateObjects() 00081 { 00082 if (m_freeObjects) 00083 freeAllObjects(); 00084 } 00085 00086 /** Delete all the objects in the matrix and set all entries to NULL pointers. 00087 */ 00088 void freeAllObjects() 00089 { 00090 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00091 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00092 if (CMatrixTemplate<T*>::m_Val[i][j]!=NULL) 00093 { 00094 delete CMatrixTemplate<T*>::m_Val[i][j]; 00095 CMatrixTemplate<T*>::m_Val[i][j] = NULL; 00096 } 00097 } 00098 00099 /** Assignment operator 00100 */ 00101 CMatrixTemplateObjects& operator = (const CMatrixTemplateObjects& m) 00102 { 00103 CMatrixTemplate<T*>::realloc( m.getRowCount(), m.getColCount() ); 00104 00105 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00106 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00107 CMatrixTemplate<T*>::m_Val[i][j] = m.m_Val[i][j]; 00108 return *this; 00109 } 00110 00111 /** Sets the behavior on matrix destroy. 00112 * See the general description of the class on the top. 00113 */ 00114 void setDestroyBehavior(bool freeObjects = true) 00115 { 00116 m_freeObjects = freeObjects; 00117 } 00118 00119 /** Alloc memory for all the non-NULL entries in the matrix. 00120 * See the general description of the class on the top. 00121 */ 00122 void allocAllObjects() 00123 { 00124 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00125 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00126 if (NULL==CMatrixTemplate<T*>::m_Val[i][j]) 00127 CMatrixTemplate<T*>::m_Val[i][j] = new T(); 00128 } 00129 00130 }; // end of class definition 00131 00132 00133 } // End of namespace 00134 } // End of namespace 00135 00136 #endif
Page generated by Doxygen 1.5.7.1 for MRPT 0.7.1 SVN: at Mon Aug 17 23:10:56 EDT 2009 |