MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Array.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 ARRAY_HPP
20 #define ARRAY_HPP
21 
22 #include "ArrayPool.hpp"
23 
24 #include <pc.hpp>
25 #include <ErrorReporter.hpp>
26 
31 template <class T>
32 class Array {
33 public:
34  Array(ArrayPool<T> & thePool);
35 
40  bool seize(Uint32 i);
41 
45  void release();
46 
50  Uint32 getSize() const;
51 
55  inline bool empty() const { return sz == 0;}
56 
60  void getPtr(Ptr<T> &, Uint32 i) const;
61 
65  void getPtr(Ptr<T> &) const ;
66 
70  T * getPtr(Uint32 i) const;
71 
72 private:
73  Uint32 base, sz;
74  ArrayPool<T> & thePool;
75 };
76 
77 template<class T>
78 inline
80  : thePool(_pool)
81 {
82  sz = 0;
83  base = RNIL;
84 }
85 
86 template<class T>
87 inline
88 bool
90  if(base == RNIL && n > 0){
91  base = thePool.seizeN(n);
92  if(base != RNIL){
93  sz = n;
94  return true;
95  }
96  return false;
97  }
98  ErrorReporter::handleAssert("Array<T>::seize failed", __FILE__, __LINE__);
99  return false;
100 }
101 
102 template<class T>
103 inline
104 void
106  if(base != RNIL){
107  thePool.releaseN(base, sz);
108  sz = 0;
109  base = RNIL;
110  return;
111  }
112 }
113 
114 template<class T>
115 inline
116 Uint32
118  return sz;
119 }
120 
121 template <class T>
122 inline
123 void
124 Array<T>::getPtr(Ptr<T> & p, Uint32 i) const {
125  p.i = i;
126 #ifdef ARRAY_GUARD
127  if(i < sz && base != RNIL){
128  p.p = thePool.getPtr(i + base);
129  return;
130  } else {
131  ErrorReporter::handleAssert("Array::getPtr failed", __FILE__, __LINE__);
132  }
133 #endif
134  p.p = thePool.getPtr(i + base);
135 }
136 
137 template<class T>
138 inline
139 void
140 Array<T>::getPtr(Ptr<T> & ptr) const {
141 #ifdef ARRAY_GUARD
142  if(ptr.i < sz && base != RNIL){
143  ptr.p = thePool.getPtr(ptr.i + base);
144  return;
145  } else {
146  ErrorReporter::handleAssert("Array<T>::getPtr failed", __FILE__, __LINE__);
147  }
148 #endif
149  ptr.p = thePool.getPtr(ptr.i + base);
150 }
151 
152 template<class T>
153 inline
154 T *
155 Array<T>::getPtr(Uint32 i) const {
156 #ifdef ARRAY_GUARD
157  if(i < sz && base != RNIL){
158  return thePool.getPtr(i + base);
159  } else {
160  ErrorReporter::handleAssert("Array<T>::getPtr failed", __FILE__, __LINE__);
161  }
162 #endif
163  return thePool.getPtr(i + base);
164 }
165 
166 
167 #endif