Main MRPT website > C++ reference
MRPT logo
CMatrixTemplateObjects.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CMatrixTemplateObjects_H
29 #define CMatrixTemplateObjects_H
30 
32 
33 namespace mrpt
34 {
35 namespace math
36 {
37 /** This template class extends the class "CMatrixTemplate" for storing "objects" at each matrix entry.
38  * This class allows a very efficient representation of sparse matrixes where each cell is an arbitrary C++ class,
39  * but its use must carefully observe the following rules:
40  * - The type in the template especialization MUST be a class with a proper default constructor.
41  * - Initially all entries are set to NULL pointers.
42  * - Pointers can be manually asigned, or automatically created through a call to "CMatrixTemplateObjects<T>::allocAllObjects"
43  * - 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.
44  * - Asignament operator with matrixes will COPY THE POINTERS, thus a copy of objects is not performed.
45  * - WARNING: Objects are not deleted while shrinking the matrix by calling "setSize", thus please call ""CMatrixTemplateObjects<T>::freeAllObjects" or manually delete objects before shrinking.
46  *
47  * \ingroup mrpt_base_grp
48  * \sa CMatrixTemplate
49  */
50 template <class T>
52 {
53 private:
55 
56 public:
57  /** Copy constructor
58  */
60  {
61  }
62 
63  /** Constructor
64  */
65  CMatrixTemplateObjects(size_t row = 3, size_t col = 3) : CMatrixTemplate<T*>( row, col ), m_freeObjects(true)
66  {
67  for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++)
68  for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++)
69  CMatrixTemplate<T*>::m_Val[i][j] = NULL;
70  }
71 
72  /** Changes the size of matrix
73  */
74  virtual void setSize(size_t row, size_t col)
75  {
76  //TODO: BUGFIX. Doesn't remove objetcs if row<m_Row or col<m_Col
77  CMatrixTemplate<T*>::realloc(row,col,true);
78  }
79 
80  /** Destructor
81  */
83  {
84  if (m_freeObjects)
86  }
87 
88  /** Delete all the objects in the matrix and set all entries to NULL pointers.
89  */
91  {
92  for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++)
93  for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++)
94  if (CMatrixTemplate<T*>::m_Val[i][j]!=NULL)
95  {
96  delete CMatrixTemplate<T*>::m_Val[i][j];
97  CMatrixTemplate<T*>::m_Val[i][j] = NULL;
98  }
99  }
100 
101  /** Assignment operator
102  */
104  {
106 
107  for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++)
108  for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++)
109  CMatrixTemplate<T*>::m_Val[i][j] = m.m_Val[i][j];
110  return *this;
111  }
112 
113  /** Sets the behavior on matrix destroy.
114  * See the general description of the class on the top.
115  */
116  void setDestroyBehavior(bool freeObjects = true)
117  {
118  m_freeObjects = freeObjects;
119  }
120 
121  /** Alloc memory for all the non-NULL entries in the matrix.
122  * See the general description of the class on the top.
123  */
125  {
126  for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++)
127  for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++)
128  if (NULL==CMatrixTemplate<T*>::m_Val[i][j])
129  CMatrixTemplate<T*>::m_Val[i][j] = new T();
130  }
131 
132 }; // end of class definition
133 
134 
135  } // End of namespace
136 } // End of namespace
137 
138 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013