File: auto_array.h

package info (click to toggle)
crac 2.5.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,452 kB
  • sloc: cpp: 41,834; sh: 391; makefile: 368
file content (38 lines) | stat: -rw-r--r-- 605 bytes parent folder | download | duplicates (9)
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
/*
 * auto_array.h
 *
 *  Created on: Oct 12, 2009
 *      Author: Ben Langmead
 */

#include <cstring>

#ifndef AUTO_ARRAY_H_
#define AUTO_ARRAY_H_

/**
 * A simple fixed-length array of type T, automatically freed in the
 * destructor.
 */
template<typename T>
class AutoArray {
public:
	AutoArray(size_t sz) {
		t_ = NULL;
		t_ = new T[sz];
		memset(t_, 0, sz*sizeof(T));
		sz_ = sz;
	}
	~AutoArray() { if(t_ != NULL) delete[] t_; }
	T& operator[](size_t sz) {
		return t_[sz];
	}
	const T& operator[](size_t sz) const {
		return t_[sz];
	}
private:
	T *t_;
	size_t sz_;
};

#endif /* AUTO_ARRAY_H_ */