Skip to content
Snippets Groups Projects
Commit 252b065a authored by Oliver Bock's avatar Oliver Bock
Browse files

Added classes for resource management

* Resource (represents a single resource incl. its data)
* ResourceFactory (caches ORC resources and instantiates Resource objects)
parent 026c8528
No related branches found
No related tags found
No related merge requests found
#include "Resource.h"
Resource::Resource(const string identifier, const vector<unsigned char>& data) :
m_Data(new vector<unsigned char>(data))
{
m_Identifier = identifier;
}
Resource::~Resource()
{
}
string Resource::Identifier() const
{
return m_Identifier;
}
const vector<unsigned char>* Resource::Data() const
{
return m_Data.get();
}
#ifndef RESOURCE_H_
#define RESOURCE_H_
#include <string>
#include <vector>
using namespace std;
class Resource
{
public:
Resource(const string identifier, const vector<unsigned char>& data);
virtual ~Resource();
string Identifier() const;
const vector<unsigned char> * Data() const;
private:
string m_Identifier;
const auto_ptr<vector<unsigned char> > m_Data;
};
#endif /*RESOURCE_H_*/
#include "ResourceFactory.h"
ResourceFactory::ResourceFactory()
{
// determine number of resources
int resourceCount = c_ResourceIndex[0][0];
// import each resource into factory cache
for(int i = 0; i < resourceCount; ++i) {
// prepare temporary buffer
vector<unsigned char> buffer;
// extract resource data from storage container
for(size_t x = 0; x < c_ResourceIndex[i+1][1]; ++x) {
// use offset and relative position to find the absolute position
unsigned char byteValue = c_ResourceStorage[c_ResourceIndex[i+1][0] + x];
// add byte to buffer
buffer.push_back(byteValue);
}
// add buffer to resource map
m_ResourceMap[c_ResourceIdentifiers[i]] = buffer;
}
}
ResourceFactory::~ResourceFactory()
{
}
const Resource* ResourceFactory::createInstance(const string identifier)
{
Resource *res = NULL;
// determine whether the requested identifier exists
if(m_ResourceMap.find(identifier) != m_ResourceMap.end()) {
// we know the requested resource, create instance
res = new Resource(identifier, m_ResourceMap[identifier]);
}
return res;
}
#ifndef RESOURCEFACTORY_H_
#define RESOURCEFACTORY_H_
#include <string>
#include <map>
#include <iostream>
#include "Resource.h"
class ResourceFactory
{
public:
ResourceFactory();
virtual ~ResourceFactory();
const Resource* createInstance(const string identifier);
private:
map<string, vector<unsigned char> > m_ResourceMap;
};
// TODO: does this need to be global?
extern const string c_ResourceIdentifiers[];
extern const unsigned int c_ResourceIndex[][2];
extern const unsigned char c_ResourceStorage[];
#endif /*RESOURCEFACTORY_H_*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment