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-2018, 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_math_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*>::rows(); i++)
66  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); 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*>::rows(); i++)
90  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); 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*>::rows(); i++)
105  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); 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*>::rows(); i++)
124  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); 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
mrpt::math::CMatrixTemplateObjects::freeAllObjects
void freeAllObjects()
Delete all the objects in the matrix and set all entries to nullptr.
Definition: CMatrixTemplateObjects.h:87
mrpt::math::CMatrixTemplate::m_Val
T ** m_Val
Definition: CMatrixTemplate.h:84
mrpt::math::CMatrixTemplateObjects::operator=
CMatrixTemplateObjects & operator=(const CMatrixTemplateObjects &m)
Assignment operator.
Definition: CMatrixTemplateObjects.h:100
mrpt::math::CMatrixTemplate
This template class provides the basic functionality for a general 2D any-size, resizable container o...
Definition: CMatrixTemplate.h:72
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::math::CMatrixTemplateObjects::setDestroyBehavior
void setDestroyBehavior(bool freeObjects=true)
Sets the behavior on matrix destroy.
Definition: CMatrixTemplateObjects.h:113
mrpt::math::CMatrixTemplate::rows
size_t rows() const
Number of rows in the matrix.
Definition: CMatrixTemplate.h:298
mrpt::math::CMatrixTemplateObjects::allocAllObjects
void allocAllObjects()
Alloc memory for all the non-NULL entries in the matrix.
Definition: CMatrixTemplateObjects.h:121
mrpt::math::CMatrixTemplateObjects::m_freeObjects
bool m_freeObjects
Definition: CMatrixTemplateObjects.h:50
mrpt::math::CMatrixTemplate::realloc
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...
Definition: CMatrixTemplate.h:90
mrpt::math::CMatrixTemplateObjects
This template class extends the class "CMatrixTemplate" for storing "objects" at each matrix entry.
Definition: CMatrixTemplateObjects.h:47
mrpt::math::CMatrixTemplateObjects::setSize
virtual void setSize(size_t row, size_t col)
Changes the size of matrix.
Definition: CMatrixTemplateObjects.h:72
mrpt::math::CMatrixTemplateObjects::CMatrixTemplateObjects
CMatrixTemplateObjects(const CMatrixTemplate< T > &m)
Copy constructor.
Definition: CMatrixTemplateObjects.h:55
mrpt::math::CMatrixTemplateObjects::~CMatrixTemplateObjects
virtual ~CMatrixTemplateObjects()
Destructor.
Definition: CMatrixTemplateObjects.h:80
mrpt::math::CMatrixTemplateObjects::CMatrixTemplateObjects
CMatrixTemplateObjects(size_t row=3, size_t col=3)
Constructor.
Definition: CMatrixTemplateObjects.h:62
row
GLenum GLenum GLvoid * row
Definition: glext.h:3576
mrpt::math::CMatrixTemplate::cols
size_t cols() const
Number of columns in the matrix.
Definition: CMatrixTemplate.h:302
CMatrixTemplate.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST