medfall

A super great game engine
Log | Files | Refs

pool.h (538B)


      1 #pragma once
      2 
      3 #include "intrinsics.h"
      4 #include "array.h"
      5 
      6 template< typename T, size_t N >
      7 struct Pool {
      8 	Pool() {
      9 		elems = array< T >( memory, 0 );
     10 	}
     11 
     12 	T * acquire() {
     13 		if( elems.n == N )
     14 			return NULL;
     15 
     16 		elems.n++;
     17 		return &elems[ elems.n - 1 ];
     18 	}
     19 
     20 	void release( T * x ) {
     21 		ASSERT( x >= elems.ptr() && x < elems.ptr() + N );
     22 
     23 		*x = elems[ elems.n - 1 ];
     24 		elems.n--;
     25 	}
     26 
     27 	void clear() {
     28 		elems.n = 0;
     29 	}
     30 
     31 	T * begin() {
     32 		return elems.begin();
     33 	}
     34 
     35 	T * end() {
     36 		return elems.end();
     37 	}
     38 
     39 	T memory[ N ];
     40 	array< T > elems;
     41 };