DynamicArrayTemplatePointer.h
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: DynamicArrayTemplatePointer.h
37 // Author: Pedram Azad
38 // Date: 11.02.2010
39 // ****************************************************************************
40 
41 #ifndef _DYNAMIC_ARRAY_TEMPLATE_POINTER_H_
42 #define _DYNAMIC_ARRAY_TEMPLATE_POINTER_H_
43 
44 
45 // ****************************************************************************
46 // Necessary includes
47 // ****************************************************************************
48 
49 #include <new> // for explicitly using correct new/delete operators on VC DSPs
50 #include <stdio.h>
51 
52 
53 
54 // ****************************************************************************
55 // CDynamicArrayTemplatePointer
56 // ****************************************************************************
57 
58 template <typename T> class CDynamicArrayTemplatePointer
59 {
60 public:
61  // constructors
62  CDynamicArrayTemplatePointer(bool bManageMemory, int nInitialSize = 100)
63  {
64  m_bManageMemory = bManageMemory;
65 
66  m_nElements = 0;
67  m_nCurrentSize = nInitialSize;
68 
69  m_ppElements = new T*[m_nCurrentSize];
70  for (int i = 0; i < m_nCurrentSize; i++)
71  m_ppElements[i] = 0;
72  }
73 
74  // CDynamicArray
76  {
77  if (m_bManageMemory)
78  {
79  for (int i = 0; i < m_nElements; i++)
80  delete m_ppElements[i];
81  }
82 
83  delete [] m_ppElements;
84  }
85 
86 
87  // public methods
88  void AddElement(T *pElement)
89  {
92 
93  m_ppElements[m_nElements++] = pElement;
94  }
95 
96  bool DeleteElement(int nIndex)
97  {
98  if (nIndex < 0 || nIndex >= m_nElements)
99  return false;
100 
101  if (m_bManageMemory)
102  delete m_ppElements[nIndex];
103 
104  for (int i = nIndex; i < m_nElements - 1; i++)
105  m_ppElements[i] = m_ppElements[i + 1];
106 
107  m_nElements--;
108 
109  return true;
110  }
111 
112  void Clear()
113  {
114  if (m_bManageMemory)
115  {
116  for (int i = 0; i < m_nElements; i++)
117  delete m_ppElements[i];
118  }
119 
120  m_nElements = 0;
121  }
122 
123  void ClearAndResize(int nSize)
124  {
125  Clear();
126 
127  m_nCurrentSize = nSize;
128 
129  delete [] m_ppElements;
130  m_ppElements = new T[m_nCurrentSize];
131  }
132 
133  int GetSize() const { return m_nElements; }
134  int GetStorageSize() const { return m_nCurrentSize; }
135 
136  // operators
137  inline const T* operator[](const int nElement) const { return m_ppElements[nElement]; }
138  inline T*& operator[](const int nElement) { return m_ppElements[nElement]; }
139 
140 
141 
142 private:
143  // private methods
144  void SetCurrentSize(int nCurrentSize)
145  {
146  if (nCurrentSize <= m_nCurrentSize)
147  {
148  printf("error: tried to set size smaller than current size in CDynamicArray::SetCurrentSize\n");
149  return;
150  }
151 
152  m_nCurrentSize = nCurrentSize;
153 
154  T **ppElements = new T*[nCurrentSize];
155 
156  for (int i = 0; i < m_nElements; i++)
157  ppElements[i] = m_ppElements[i];
158 
159  delete [] m_ppElements;
160  m_ppElements = ppElements;
161  }
162 
163 
164  // private attribute
168 
170 };
171 
172 
173 
174 #endif /* _DYNAMIC_ARRAY_TEMPLATE_POINTER_H_ */
const T * operator[](const int nElement) const
CDynamicArrayTemplatePointer(bool bManageMemory, int nInitialSize=100)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27