class Resource {
public:
Resource()
{
ResourceManager.attach( this );
}
virtual ~Resource()
{
ResourceManager.detach( this );
}
virtual void streamIn() = 0;
virtual UINT streamOut() = 0;
public:
UINT last_use_time;
};
class ResourceManager {
public:
void attach( Resource *res );
void detach( Resource *res );
UINT get_current_time()
{
return ++current_time;
}
UINT pop()
{
Resource *old_res;
for( UINT i = 0; i < reslist.size(); ++i )
{
Resource *res = reslist[i];
// LRU...
if( res->last_use_time < old_res->last_use_time )
old_res = res;
}
return old_res->steamOut();
}
private:
std::vector<Resource*> reslist;
UINT current_time;
};
class MemoryAllocator {
public:
void *alloc( UINT size )
{
if( memoryAvailable > 0 )
return new char[size];
else
{
UINT got_size = 0;
do {
got_size += ResourceManager.pop();
} while( got_size < size );
return new char[size];
}
}
};
class RayObject : public Resource {
public:
void streamIn()
{
if( compressed )
{
prilist = new RayTriangle [prisize];
compressed = false;
}
last_use_time = ResourceManager.get_current_time();
}
UINT streamOut()
{
if( !compressed )
{
delete [] prilist;
compressed = true;
}
return size;
}
private:
RayTriangle prilist;
bool compressed;
};