GPUMLib  0.2.2
GPU Machine Learning Library
BaseArray.h
1 /*
2  Noel Lopes is a Professor at the Polytechnic of Guarda, Portugal
3  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Noel de Jesus Mendonça Lopes
4 
5  This file is part of GPUMLib.
6 
7  GPUMLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef GPUMLib_BaseArray_h
22 #define GPUMLib_BaseArray_h
23 
24 namespace GPUMLib {
25 
28 
29 template <class Type> class CudaArray;
30 
32 template <class Type> class BaseArray {
33  friend class CudaArray<Type>;
34 
35  protected:
36  Type * arrayData;
37  int size;
38 
39  void Init() {
40  arrayData = nullptr;
41  size = 0;
42  }
43 
44  BaseArray() {
45  Init();
46  }
47 
48  void MoveFrom(BaseArray<Type> & other) {
49  size = other.size;
50  arrayData = other.arrayData;
51 
52  other.Init();
53  }
54 
55  virtual void Alloc(int size) = 0;
56 
57  public:
59  virtual void Dispose() = 0;
60 
63  int Length() const {
64  return size;
65  }
66 
70  Type * Pointer() const {
71  return (size > 0) ? arrayData : nullptr;
72  }
73 
78  if (size != this->size) {
79  Dispose();
80  Alloc(size);
81  }
82 
83  return Length();
84  }
85 };
86 
88 
89 }
90 
91 #endif
Type * Pointer() const
Definition: BaseArray.h:70
Create an array of any type, that automatically manages the memory used to hold its elements (data wi...
Definition: BaseArray.h:29
int ResizeWithoutPreservingData(int size)
Definition: BaseArray.h:77
Base class for HostArray and DeviceArray classes (Array base class)
Definition: BaseArray.h:32
virtual void Dispose()=0
Disposes the array.
int Length() const
Definition: BaseArray.h:63