Sign in to follow this  
  • entries
    26
  • comments
    21
  • views
    26137

Untitled

Sign in to follow this  

261 views

Well, its a common fact that your prediction for how long something will take is usually far too short. I've started the resource management system, and it went through several iterations. Over the week I decided to work on something unrelated to the goal in the last journal entry.

The first was with just the textures. Once I had textures loading with the correct interface, I realised that I actually needed a templated resource counting class. So I implemented this instead and re-implemented the texture table using it. This core functionality is repeated so much that a templated class seemed the only way to go. I considered using inheritance but decided to keep this simple.

#pragma once
#include
#include
#include
#include
#include
#include

#include "../Logging/Logging.h"

/*
Templated storage of a set of objects along with a modifiable usage count.
*/


template <class VALUE> class UseCountTable
{
public:
UseCountTable()
{

}

~UseCountTable()
{

}

bool addMember(const std::string& name, int use_count, VALUE &object)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it == objects.end())
{
objects[name]=std::pair<int,VALUE>(use_count,object);
return true;
}
else
{
return false;
}
}

bool updateMember(const std::string &name, int change)
{//return false if action cannot be performed.
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
it->second.first += change;
return true;
}
else
{
return false;
}
}

bool deleteMember(const std::string& name)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
objects.erase(it);
return true;
}
else
{
return false;
}
}

bool exists(const std::string& name)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
return true;
}
else
{
return false;
}
}
bool getMember(const std::string& name, int& use_count, VALUE& object)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
use_count = it->second.first;
object = it->second.second;
return true;
}
else
{
return false;
}
}

bool getUseCount(const std::string& name, int& use_count)
{//return false if action cannot be performed
std::mapint,VALUE> >::iterator it = objects.find(name);
if(it != objects.end())
{
use_count = it->second.first;
return true;
}
else
{
return false;
}
}

void consoleDump()
{
std::mapint,VALUE> >::iterator
it = objects.begin(),
end=objects.end();
for(it; it != end; ++it)
{
std::cout<<"NAME:" <first <<" USE COUNT:" <second.first< }
}
void getAllObjects(std::vector &table)
{
std::mapint,VALUE> >::iterator
it = objects.begin(),
end=objects.end();
for(it; it != end; ++it)
{
table.push_back(it->second);
}
}

private:
std::mapint,VALUE> > objects;
};


The other classes in the system will each be a wrapper for this; they will handle the shopping list they are given and load / update the resources under their control accordingly. I've dabbled with different ways of ensuring deletion of the contents, but after looking at one hack or another, decided to let the wrapper classes do it. any suggestions are welcome though. I've started a thread discussing this class and the unit test I did for it.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now