Main MRPT website > C++ reference for MRPT 1.5.6
CMatrixTemplateObjects.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CMatrixTemplateObjects_H
10 #define CMatrixTemplateObjects_H
11 
13 
14 namespace mrpt
15 {
16 namespace math
17 {
18 /** This template class extends the class "CMatrixTemplate" for storing "objects" at each matrix entry.
19  * This class allows a very efficient representation of sparse matrixes where each cell is an arbitrary C++ class,
20  * but its use must carefully observe the following rules:
21  * - The type in the template especialization MUST be a class with a proper default constructor.
22  * - Initially all entries are set to NULL pointers.
23  * - Pointers can be manually asigned, or automatically created through a call to "CMatrixTemplateObjects<T>::allocAllObjects"
24  * - 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.
25  * - Asignament operator with matrixes will COPY THE POINTERS, thus a copy of objects is not performed.
26  * - WARNING: Objects are not deleted while shrinking the matrix by calling "setSize", thus please call ""CMatrixTemplateObjects<T>::freeAllObjects" or manually delete objects before shrinking.
27  *
28  * \ingroup mrpt_base_grp
29  * \sa CMatrixTemplate
30  */
31 template <class T>
33 {
34 private:
36 
37 public:
38  /** Copy constructor
39  */
41  {
42  }
43 
44  /** Constructor
45  */
46  CMatrixTemplateObjects(size_t row = 3, size_t col = 3) : CMatrixTemplate<T*>( row, col ), m_freeObjects(true)
47  {
48  for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++)
49  for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++)
50  CMatrixTemplate<T*>::m_Val[i][j] = NULL;
51  }
52 
53  /** Changes the size of matrix
54  */
55  virtual void setSize(size_t row, size_t col)
56  {
57  //TODO: BUGFIX. Doesn't remove objetcs if row<m_Row or col<m_Col
58  CMatrixTemplate<T*>::realloc(row,col,true);
59  }
60 
61  /** Destructor
62  */
64  {
65  if (m_freeObjects)
67  }
68 
69  /** Delete all the objects in the matrix and set all entries to NULL pointers.
70  */
72  {
73  for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++)
74  for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++)
75  if (CMatrixTemplate<T*>::m_Val[i][j]!=NULL)
76  {
77  delete CMatrixTemplate<T*>::m_Val[i][j];
78  CMatrixTemplate<T*>::m_Val[i][j] = NULL;
79  }
80  }
81 
82  /** Assignment operator
83  */
85  {
87 
88  for (size_t i=0; i < CMatrixTemplate<T*>::getRowCount(); i++)
89  for (size_t j=0; j < CMatrixTemplate<T*>::getColCount(); j++)
90  CMatrixTemplate<T*>::m_Val[i][j] = m.m_Val[i][j];
91  return *this;
92  }
93 
94  /** Sets the behavior on matrix destroy.
95  * See the general description of the class on the top.
96  */
97  void setDestroyBehavior(bool freeObjects = true)
98  {
99  m_freeObjects = freeObjects;
100  }
101 
102  /** Alloc memory for all the non-NULL entries in the matrix.
103  * See the general description of the class on the top.
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  if (NULL==CMatrixTemplate<T*>::m_Val[i][j])
110  CMatrixTemplate<T*>::m_Val[i][j] = new T();
111  }
112 
113 }; // end of class definition
114 
115 
116  } // End of namespace
117 } // End of namespace
118 
119 #endif
GLenum GLenum GLvoid * row
Definition: glew.h:2903
EIGEN_STRONG_INLINE size_t getColCount() const
Get number of columns.
Definition: eigen_plugins.h:48
CMatrixTemplateObjects(const CMatrixTemplate< T > &m)
Copy constructor.
CMatrixTemplateObjects & operator=(const CMatrixTemplateObjects &m)
Assignment operator.
This template class provides the basic functionality for a general 2D any-size, resizable container o...
void allocAllObjects()
Alloc memory for all the non-NULL entries in the matrix.
size_t getColCount() const
Number of columns in the matrix.
virtual void setSize(size_t row, size_t col)
Changes the size of matrix.
EIGEN_STRONG_INLINE size_t getRowCount() const
Get number of rows.
Definition: eigen_plugins.h:46
size_t getRowCount() const
Number of rows in the matrix.
void freeAllObjects()
Delete all the objects in the matrix and set all entries to NULL pointers.
CMatrixTemplateObjects(size_t row=3, size_t col=3)
Constructor.
const GLdouble * m
Definition: glew.h:5094
void realloc(size_t row, size_t col, bool newElementsToZero=false)
Internal use only: It reallocs the memory for the 2D matrix, maintaining the previous contents if pos...
This template class extends the class "CMatrixTemplate" for storing "objects" at each matrix entry...
void setDestroyBehavior(bool freeObjects=true)
Sets the behavior on matrix destroy.



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018