Main MRPT website > C++ reference for MRPT 1.9.9
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
19  *"objects" at each matrix entry.
20  * This class allows a very efficient representation of sparse matrixes where
21  *each cell is an arbitrary C++ class,
22  * but its use must carefully observe the following rules:
23  * - The type in the template especialization MUST be a class with a
24  *proper
25  *default constructor.
26  * - Initially all entries are set to nullptr.
27  * - Pointers can be manually asigned, or automatically created through
28  *a
29  *call to "CMatrixTemplateObjects<T>::allocAllObjects"
30  * - Independently of how pointers are asigned, memory will be free by
31  *destroying objects for each non-NULL entry in the matrix. In some special
32  *situations, the user can indicate not to free those objects by calling
33  *"CMatrixTemplateObjects<T>::setDestroyBehavior", then it is up to the user to
34  *free the memory. In all cases the default behavior is to free the memory.
35  * - Asignament operator with matrixes will COPY THE POINTERS, thus a
36  *copy
37  *of objects is not performed.
38  * - WARNING: Objects are not deleted while shrinking the matrix by
39  *calling
40  *"setSize", thus please call ""CMatrixTemplateObjects<T>::freeAllObjects" or
41  *manually delete objects before shrinking.
42  *
43  * \ingroup mrpt_base_grp
44  * \sa CMatrixTemplate
45  */
46 template <class T>
48 {
49  private:
51 
52  public:
53  /** Copy constructor
54  */
56  : CMatrixTemplate<T*>(m), m_freeObjects(true)
57  {
58  }
59 
60  /** Constructor
61  */
62  CMatrixTemplateObjects(size_t row = 3, size_t col = 3)
63  : CMatrixTemplate<T*>(row, col), m_freeObjects(true)
64  {
65  for (size_t i = 0; i < CMatrixTemplate<T*>::getRowCount(); i++)
66  for (size_t j = 0; j < CMatrixTemplate<T*>::getColCount(); j++)
67  CMatrixTemplate<T*>::m_Val[i][j] = nullptr;
68  }
69 
70  /** Changes the size of matrix
71  */
72  virtual void setSize(size_t row, size_t col)
73  {
74  // TODO: BUGFIX. Doesn't remove objetcs if row<m_Row or col<m_Col
76  }
77 
78  /** Destructor
79  */
81  {
83  }
84 
85  /** Delete all the objects in the matrix and set all entries to nullptr.
86  */
88  {
89  for (size_t i = 0; i < CMatrixTemplate<T*>::getRowCount(); i++)
90  for (size_t j = 0; j < CMatrixTemplate<T*>::getColCount(); j++)
91  if (CMatrixTemplate<T*>::m_Val[i][j] != nullptr)
92  {
93  delete CMatrixTemplate<T*>::m_Val[i][j];
94  CMatrixTemplate<T*>::m_Val[i][j] = nullptr;
95  }
96  }
97 
98  /** Assignment operator
99  */
101  {
103 
104  for (size_t i = 0; i < CMatrixTemplate<T*>::getRowCount(); i++)
105  for (size_t j = 0; j < CMatrixTemplate<T*>::getColCount(); j++)
106  CMatrixTemplate<T*>::m_Val[i][j] = m.m_Val[i][j];
107  return *this;
108  }
109 
110  /** Sets the behavior on matrix destroy.
111  * See the general description of the class on the top.
112  */
113  void setDestroyBehavior(bool freeObjects = true)
114  {
115  m_freeObjects = freeObjects;
116  }
117 
118  /** Alloc memory for all the non-NULL entries in the matrix.
119  * See the general description of the class on the top.
120  */
122  {
123  for (size_t i = 0; i < CMatrixTemplate<T*>::getRowCount(); i++)
124  for (size_t j = 0; j < CMatrixTemplate<T*>::getColCount(); j++)
125  if (nullptr == CMatrixTemplate<T*>::m_Val[i][j])
126  CMatrixTemplate<T*>::m_Val[i][j] = new T();
127  }
128 
129 }; // end of class definition
130 
131 } // End of namespace
132 } // End of namespace
133 
134 #endif
This template class provides the basic functionality for a general 2D any-size, resizable container o...
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...
size_t getColCount() const
Number of columns in the matrix.
size_t getRowCount() const
Number of rows in the matrix.
This template class extends the class "CMatrixTemplate" for storing "objects" at each matrix entry.
void freeAllObjects()
Delete all the objects in the matrix and set all entries to nullptr.
CMatrixTemplateObjects(const CMatrixTemplate< T > &m)
Copy constructor.
void setDestroyBehavior(bool freeObjects=true)
Sets the behavior on matrix destroy.
void allocAllObjects()
Alloc memory for all the non-NULL entries in the matrix.
CMatrixTemplateObjects(size_t row=3, size_t col=3)
Constructor.
virtual void setSize(size_t row, size_t col)
Changes the size of matrix.
CMatrixTemplateObjects & operator=(const CMatrixTemplateObjects &m)
Assignment operator.
EIGEN_STRONG_INLINE size_t getRowCount() const
Get number of rows.
Definition: eigen_plugins.h:57
EIGEN_STRONG_INLINE size_t getColCount() const
Get number of columns.
Definition: eigen_plugins.h:59
GLenum GLenum GLvoid * row
Definition: glext.h:3576
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST