1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#pragma once
#ifndef ARRAY2D_H_INCLUDED
#define ARRAY2D_H_INCLUDED
//====================================
// Bounds checking option. Change this #define to one of the following values:
// 0 = never bounds checking
// 1 = bounds checking in Debug builds only (DEBUG or _DEBUG defined)
// 2 = bounds checking always
#define ARRAY2D_BOUNDCHECK_OPTION 1
//====================================
// v* DON'T MESS WITH THIS *v
//====================================
#if (ARRAY2D_BOUNDCHECK_OPTION < 0) || (ARRAY2D_BOUNDCHECK_OPTION > 2)
#error "Invalid value for ARRAY2D_BOUNDCHECK_OPTION in array2d.h"
#elif (ARRAY2D_BOUNDCHECK_OPTION == 2) || ( (ARRAY2D_BOUNDCHECK_OPTION == 1) && (defined(DEBUG) || defined(_DEBUG)) )
#define ARRAY2D_BOUNDCHECK_PERFORM 1
#else
#define ARRAY2D_BOUNDCHECK_PERFORM 0
#endif
//====================================
// ^* DON'T MESS WITH THAT *^
//====================================
// need <algorithm> for std::copy
#include <algorithm>
#if ARRAY2D_BOUNDCHECK_PERFORM == 1
#include <stdexcept>
#endif
// the actual class!
template <typename T>
class array2d
{
private:
T* p;
unsigned w;
unsigned h;
public:
//===============================
// ctors / dtors
array2d() : p(0), w(0), h(0) { }
array2d(const array2d<T>& r) : p(0), w(r.w), h(r.h)
{
if(w && h)
{
try
{
p = new T[w*h];
std::copy( r.p,
r.p + w*h,
p );
}catch(...)
{
delete[] p;
throw;
}
}
}
array2d(unsigned width,unsigned height) : p(0), w(width), h(height)
{
if(w && h)
{
p = new T[w*h];
}
}
~array2d()
{
delete[] p;
}
//================================
// assignment
array2d& operator = (const array2d<T>& r)
{
if(this == &r)
return *this;
T* n = 0;
if(r.w && r.h)
{
try
{
n = new T[r.w * r.h];
std::copy( r.p,
r.p + r.w*r.h,
n );
}catch(...)
{
delete[] n;
throw;
}
}
delete[] p;
p = n;
w = r.w;
h = r.h;
return *this;
}
//================================
// info
inline unsigned GetWidth() const { return w; }
inline unsigned GetHeight() const { return h; }
//================================
// accessing
#if ARRAY2D_BOUNDCHECK_PERFORM == 0
inline T& operator () (unsigned x,unsigned y) { return p[(y*w)+x]; }
inline const T& operator () (unsigned x,unsigned y) const { return p[(y*w)+x]; }
#elif ARRAY2D_BOUNDCHECK_PERFORM == 1
inline T& operator () (unsigned x,unsigned y) { AssertBounds(x,y); return p[(y*w)+x]; }
inline const T& operator () (unsigned x,unsigned y) const { AssertBounds(x,y); return p[(y*w)+x]; }
#endif
//===============================
// resizing
void Resize(unsigned width,unsigned height)
{
T* n = 0;
if(width && height)
{
try
{
n = new T[width * height];
unsigned copyx = std::min(w,width);
unsigned copyy = std::min(h,height);
for(unsigned y = 0; y < copyy; ++y)
std::copy( p + (y*w),
p + (y*w) + copyx,
n + (y*width) );
}catch(...)
{
delete[] n;
throw;
}
}
delete[] p;
p = n;
w = width;
h = height;
}
//===============================
// bounds checking
private:
#if ARRAY2D_BOUNDCHECK_PERFORM == 1
inline void AssertBounds(unsigned x,unsigned y) const
{
if((x >= w) || (y >= h))
throw std::out_of_range("Attempting to access out-of-bounds element in array2d class");
}
#endif
};
#endif // ARRAY2D_H_INCLUDED
|