00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 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 * \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 //TODO: BUGFIX. Doesn't remove objetcs if row<m_Row or col<m_Col 00076 CMatrixTemplate<T*>::realloc(row,col,true); 00077 } 00078 00079 /** Destructor 00080 */ 00081 virtual ~CMatrixTemplateObjects() 00082 { 00083 if (m_freeObjects) 00084 freeAllObjects(); 00085 } 00086 00087 /** Delete all the objects in the matrix and set all entries to NULL pointers. 00088 */ 00089 void freeAllObjects() 00090 { 00091 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00092 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00093 if (CMatrixTemplate<T*>::m_Val[i][j]!=NULL) 00094 { 00095 delete CMatrixTemplate<T*>::m_Val[i][j]; 00096 CMatrixTemplate<T*>::m_Val[i][j] = NULL; 00097 } 00098 } 00099 00100 /** Assignment operator 00101 */ 00102 CMatrixTemplateObjects& operator = (const CMatrixTemplateObjects& m) 00103 { 00104 CMatrixTemplate<T*>::realloc( m.getRowCount(), m.getColCount() ); 00105 00106 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00107 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00108 CMatrixTemplate<T*>::m_Val[i][j] = m.m_Val[i][j]; 00109 return *this; 00110 } 00111 00112 /** Sets the behavior on matrix destroy. 00113 * See the general description of the class on the top. 00114 */ 00115 void setDestroyBehavior(bool freeObjects = true) 00116 { 00117 m_freeObjects = freeObjects; 00118 } 00119 00120 /** Alloc memory for all the non-NULL entries in the matrix. 00121 * See the general description of the class on the top. 00122 */ 00123 void allocAllObjects() 00124 { 00125 for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++) 00126 for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++) 00127 if (NULL==CMatrixTemplate<T*>::m_Val[i][j]) 00128 CMatrixTemplate<T*>::m_Val[i][j] = new T(); 00129 } 00130 00131 }; // end of class definition 00132 00133 00134 } // End of namespace 00135 } // End of namespace 00136 00137 #endif
Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011 |