00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 00005 | | 00006 | Copyright (C) 2005-2011 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 * \ingroup mrpt_base_grp 00048 * \sa CMatrixTemplate 00049 */ 00050 template <class T> 00051 class CMatrixTemplateObjects : public CMatrixTemplate<T*> 00052 { 00053 private: 00054 bool m_freeObjects; 00055 00056 public: 00057 /** Copy constructor 00058 */ 00059 CMatrixTemplateObjects(const CMatrixTemplate<T>& m) : CMatrixTemplate<T*>( m ), m_freeObjects(true) 00060 { 00061 } 00062 00063 /** Constructor 00064 */ 00065 CMatrixTemplateObjects(size_t row = 3, size_t col = 3) : CMatrixTemplate<T*>( row, col ), m_freeObjects(true) 00066 { 00067 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00068 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00069 CMatrixTemplate<T*>::m_Val[i][j] = NULL; 00070 } 00071 00072 /** Changes the size of matrix 00073 */ 00074 virtual void setSize(size_t row, size_t col) 00075 { 00076 //TODO: BUGFIX. Doesn't remove objetcs if row<m_Row or col<m_Col 00077 CMatrixTemplate<T*>::realloc(row,col,true); 00078 } 00079 00080 /** Destructor 00081 */ 00082 virtual ~CMatrixTemplateObjects() 00083 { 00084 if (m_freeObjects) 00085 freeAllObjects(); 00086 } 00087 00088 /** Delete all the objects in the matrix and set all entries to NULL pointers. 00089 */ 00090 void freeAllObjects() 00091 { 00092 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00093 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00094 if (CMatrixTemplate<T*>::m_Val[i][j]!=NULL) 00095 { 00096 delete CMatrixTemplate<T*>::m_Val[i][j]; 00097 CMatrixTemplate<T*>::m_Val[i][j] = NULL; 00098 } 00099 } 00100 00101 /** Assignment operator 00102 */ 00103 CMatrixTemplateObjects& operator = (const CMatrixTemplateObjects& m) 00104 { 00105 CMatrixTemplate<T*>::realloc( m.getRowCount(), m.getColCount() ); 00106 00107 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00108 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00109 CMatrixTemplate<T*>::m_Val[i][j] = m.m_Val[i][j]; 00110 return *this; 00111 } 00112 00113 /** Sets the behavior on matrix destroy. 00114 * See the general description of the class on the top. 00115 */ 00116 void setDestroyBehavior(bool freeObjects = true) 00117 { 00118 m_freeObjects = freeObjects; 00119 } 00120 00121 /** Alloc memory for all the non-NULL entries in the matrix. 00122 * See the general description of the class on the top. 00123 */ 00124 void allocAllObjects() 00125 { 00126 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00127 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00128 if (NULL==CMatrixTemplate<T*>::m_Val[i][j]) 00129 CMatrixTemplate<T*>::m_Val[i][j] = new T(); 00130 } 00131 00132 }; // end of class definition 00133 00134 00135 } // End of namespace 00136 } // End of namespace 00137 00138 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |