MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CArray.hpp
1 /*
2  Copyright (C) 2003, 2005, 2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef CARRAY_HPP
20 #define CARRAY_HPP
21 
22 #include "ndbd_malloc.hpp"
23 
27 template <class T>
28 class CArray {
29 public:
30  CArray();
31  ~CArray();
32 
38  bool setSize(Uint32 noOfElements, bool exit_on_error = true);
39 
43  Uint32 getSize() const;
44 
48  void getPtr(Ptr<T> &) const;
49 
53  T * getPtr(Uint32 i) const;
54 
58  void getPtr(Ptr<T> &, Uint32 i) const;
59 
60 private:
61  Uint32 size;
62  T * theArray;
63 };
64 
65 template <class T>
66 inline
68  size = 0;
69  theArray = 0;
70 }
71 
72 template <class T>
73 inline
75  if(theArray != 0){
76  ndbd_free(theArray, size * sizeof(T));
77  theArray = 0;
78  }
79 }
80 
86 template <class T>
87 inline
88 bool
89 CArray<T>::setSize(Uint32 noOfElements, bool exit_on_error){
90  if(size == noOfElements)
91  return true;
92 
93  theArray = (T *)ndbd_malloc(noOfElements * sizeof(T));
94  if(theArray == 0)
95  {
96  if (!exit_on_error)
97  return false;
98  ErrorReporter::handleAssert("CArray<T>::setSize malloc failed",
99  __FILE__, __LINE__, NDBD_EXIT_MEMALLOC);
100  return false; // not reached
101  }
102  size = noOfElements;
103  return true;
104 }
105 
106 template<class T>
107 inline
108 Uint32
110  return size;
111 }
112 
113 template <class T>
114 inline
115 void
116 CArray<T>::getPtr(Ptr<T> & ptr) const {
117  const Uint32 i = ptr.i;
118  if(i < size){
119  ptr.p = &theArray[i];
120  return;
121  } else {
122  ErrorReporter::handleAssert("CArray<T>::getPtr", __FILE__, __LINE__);
123  }
124 }
125 
126 template <class T>
127 inline
128 T *
129 CArray<T>::getPtr(Uint32 i) const {
130  if(i < size){
131  return &theArray[i];
132  } else {
133  ErrorReporter::handleAssert("CArray<T>::getPtr", __FILE__, __LINE__);
134  return 0;
135  }
136 }
137 
138 template <class T>
139 inline
140 void
141 CArray<T>::getPtr(Ptr<T> & ptr, Uint32 i) const {
142  ptr.i = i;
143  if(i < size){
144  ptr.p = &theArray[i];
145  return;
146  } else {
147  ErrorReporter::handleAssert("CArray<T>::getPtr", __FILE__, __LINE__);
148  }
149 }
150 
151 #endif