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::math
15 {
16 /** This template class extends the class "CMatrixTemplate" for storing
17  *"objects" at each matrix entry.
18  * This class allows a very efficient representation of sparse matrixes where
19  *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
22  *proper
23  *default constructor.
24  * - Initially all entries are set to nullptr.
25  * - Pointers can be manually asigned, or automatically created through
26  *a
27  *call to "CMatrixTemplateObjects<T>::allocAllObjects"
28  * - Independently of how pointers are asigned, memory will be free by
29  *destroying objects for each non-NULL entry in the matrix. In some special
30  *situations, the user can indicate not to free those objects by calling
31  *"CMatrixTemplateObjects<T>::setDestroyBehavior", then it is up to the user to
32  *free the memory. In all cases the default behavior is to free the memory.
33  * - Asignament operator with matrixes will COPY THE POINTERS, thus a
34  *copy
35  *of objects is not performed.
36  * - WARNING: Objects are not deleted while shrinking the matrix by
37  *calling
38  *"setSize", thus please call ""CMatrixTemplateObjects<T>::freeAllObjects" or
39  *manually delete objects before shrinking.
40  *
41  * \ingroup mrpt_math_grp
42  * \sa CMatrixTemplate
43  */
44 template <class T>
46 {
47  private:
49 
50  public:
51  /** Copy constructor
52  */
54  : CMatrixTemplate<T*>(m), m_freeObjects(true)
55  {
56  }
57 
58  /** Constructor
59  */
60  CMatrixTemplateObjects(size_t row = 3, size_t col = 3)
61  : CMatrixTemplate<T*>(row, col), m_freeObjects(true)
62  {
63  for (size_t i = 0; i < CMatrixTemplate<T*>::rows(); i++)
64  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); j++)
65  CMatrixTemplate<T*>::m_Val[i][j] = nullptr;
66  }
67 
68  /** Changes the size of matrix
69  */
70  virtual void setSize(size_t row, size_t col)
71  {
72  // TODO: BUGFIX. Doesn't remove objetcs if row<m_Row or col<m_Col
74  }
75 
76  /** Destructor
77  */
79  {
81  }
82 
83  /** Delete all the objects in the matrix and set all entries to nullptr.
84  */
86  {
87  for (size_t i = 0; i < CMatrixTemplate<T*>::rows(); i++)
88  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); j++)
89  if (CMatrixTemplate<T*>::m_Val[i][j] != nullptr)
90  {
91  delete CMatrixTemplate<T*>::m_Val[i][j];
92  CMatrixTemplate<T*>::m_Val[i][j] = nullptr;
93  }
94  }
95 
96  /** Assignment operator
97  */
99  {
101 
102  for (size_t i = 0; i < CMatrixTemplate<T*>::rows(); i++)
103  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); j++)
104  CMatrixTemplate<T*>::m_Val[i][j] = m.m_Val[i][j];
105  return *this;
106  }
107 
108  /** Sets the behavior on matrix destroy.
109  * See the general description of the class on the top.
110  */
111  void setDestroyBehavior(bool freeObjects = true)
112  {
113  m_freeObjects = freeObjects;
114  }
115 
116  /** Alloc memory for all the non-NULL entries in the matrix.
117  * See the general description of the class on the top.
118  */
120  {
121  for (size_t i = 0; i < CMatrixTemplate<T*>::rows(); i++)
122  for (size_t j = 0; j < CMatrixTemplate<T*>::cols(); j++)
123  if (nullptr == CMatrixTemplate<T*>::m_Val[i][j])
124  CMatrixTemplate<T*>::m_Val[i][j] = new T();
125  }
126 
127 }; // end of class definition
128 
129 }
130 #endif
131 
132 
This base provides a set of functions for maths stuff.
size_t rows() const
Number of rows in the matrix.
CMatrixTemplateObjects(const CMatrixTemplate< T > &m)
Copy constructor.
CMatrixTemplateObjects & operator=(const CMatrixTemplateObjects &m)
Assignment operator.
size_t cols() const
Number of columns in the matrix.
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.
virtual void setSize(size_t row, size_t col)
Changes the size of matrix.
GLenum GLenum GLvoid * row
Definition: glext.h:3576
void freeAllObjects()
Delete all the objects in the matrix and set all entries to nullptr.
CMatrixTemplateObjects(size_t row=3, size_t col=3)
Constructor.
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.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020