Archived

This topic is now archived and is closed to further replies.

PaulCunningham

Candidate for Template?

Recommended Posts

PaulCunningham    132
Hi folks - I'm back, by popular demand (ahem!). I've got the following classes that store my config data for my game and was wondering if this is something I can turn into a template to stop me creating classes for every type of data. e.g. here are two classes - 1 stores CWeapon* data, the other CAlien* data
  
class CWeaponDataStore 
{	
	public:
		CWeaponDataStore();
		virtual ~CWeaponDataStore();

		static int addData(ActorClassEnum actor_class, CWeaponData *weapon_data);
		static CWeaponData *getData(ActorClassEnum actor_class);
		static bool release();

	protected:
	
	private:
		static std::map<ActorClassEnum, CWeaponData*> m_weapon_data_map;
};



class CAlienDataStore 
{	
	public:
		CAlienDataStore();
		virtual ~CAlienDataStore();

		static int addData(ActorClassEnum actor_class, CAlienData *alien_data);
		static CAlienData *getData(ActorClassEnum actor_class);
		static bool release();

	protected:
	
	private:
		static std::map<ActorClassEnum, CAlienData*> m_alien_data_map;
};
  
This is how I use them in a proxy class that reads in my data from a Lua script...
  
CLuaProxyWeaponData::CLuaProxyWeaponData(lua_State* L) 
{
	const int expected = 1;
	const int stackpos_actor_class = 1; 

	//Ensure that we get what we wanted

	if (gsCLua::handleParameters(L, expected)) {
		//Populate the field with the data from the stack

		ActorClassEnum actor_class = static_cast<ActorClassEnum> ((int) lua_tonumber(L, stackpos_actor_class));

		//Determine if the data has already been registered

		m_weapon_data = CWeaponDataStore::getData(actor_class);

		if (!m_weapon_data) {
			//It hasn't so create and register it

			m_weapon_data = new CWeaponData;

			CWeaponDataStore::addData(actor_class, m_weapon_data);
		}
	}

	//Clear the stack

	lua_settop(L, 0);	
}


CLuaProxyAlienData::CLuaProxyAlienData(lua_State* L) 
{
	const int expected = 1;
	const int stackpos_actor_class = 1; 

	//Ensure that we get what we wanted

	if (gsCLua::handleParameters(L, expected)) {
		//Populate the field with the data from the stack

		ActorClassEnum actor_class = static_cast<ActorClassEnum> ((int) lua_tonumber(L, stackpos_actor_class));

		//Determine if the data has already been registered

		m_alien_data = CAlienDataStore::getData(actor_class);

		if (!m_alien_data) {
			//It hasn't so create and register it

			m_alien_data = new CAlienData;

			CAlienDataStore::addData(actor_class, m_alien_data);
		}
	}

	//Clear the stack

	lua_settop(L, 0);	
}

  
As you can see they are practically identical. Looks to me like I could possibly go the template route but just thought I'd post first to see if it was a good idea (and indeed if it's possible for my implementation) Cheers, Paul EDIT: Added Source tags [edited by - PaulCunningham on May 28, 2003 3:57:36 AM]

Share this post


Link to post
Share on other sites
PaulCunningham    132
OK, here''s my implementation....


  
// Template1.cpp : Defines the entry point for the console application.

//


#include "stdafx.h"
#include <map>

using namespace std;

class CWeaponData {
public:
float m_fire_delay;

};

class CAlienData {
public:
int m_kill_bonus;

};

template<class T>
class CDataStore {
public:
void addData(int _class, T* data) { m_data_list[_class] = data; };
T* getData(int _class) { return m_data_list[_class]; };

private:
map<int, T*> m_data_list;

};

//map<int, CAlienData> CDataStore<CAlienData>::m_data_list;

//map<int, CWeaponData> CDataStore<CWeaponData>::m_data_list;


int _tmain(int argc, _TCHAR* argv[])
{

CAlienData *alien_data1 = new CAlienData;
CAlienData *alien_data2 = new CAlienData;

CWeaponData *weapon_data = new CWeaponData;

alien_data1->m_kill_bonus = 100;
alien_data2->m_kill_bonus = 2500;

weapon_data->m_fire_delay = 12.5;

CDataStore<CAlienData> alien_data_store;
CDataStore<CWeaponData> weapon_data_store;

alien_data_store.addData(1, alien_data1);
alien_data_store.addData(2, alien_data2);

weapon_data_store.addData(1, weapon_data);

//CDataStore<CAlienData>::addData(1, alien_data1);

//CDataStore<CAlienData>::addData(2, alien_data2);

//CDataStore<CWeaponData>::addData(1, weapon_data);


//printf("CDataStore<CAlienData>::getData(1)->m_kill_bonus = %i", CDataStore<CAlienData>::getData(1)->m_kill_bonus);

//printf("CDataStore<CAlienData>::getData(2)->m_kill_bonus = %i", CDataStore<CAlienData>::getData(2)->m_kill_bonus);

//printf("CDataStore<CWeaponData>::getData(1)->m_fire_delay = %f", CDataStore<CWeaponData>::getData(1)->m_fire_delay);


printf("alien_data_store.getData(1)->m_kill_bonus = %i\n", alien_data_store.getData(1)->m_kill_bonus);
printf("alien_data_store.getData(2)->m_kill_bonus = %i\n", alien_data_store.getData(2)->m_kill_bonus);
printf("weapon_data_store.getData(1)->m_fire_delay = %f\n", weapon_data_store.getData(1)->m_fire_delay);

delete alien_data1;
delete alien_data2;
delete weapon_data;

int i = scanf("Wait");

return 0;
}


I couldn''t make my CDataStore methods static because I couldn''t do either of these...


  
map<int, CAlienData> CDataStore<CAlienData>::m_data_list;
map<int, CWeaponData> CDataStore<CWeaponData>::m_data_list;


Gives ''redefinition of basic type'' error

and...


  
map<int, T*> CDataStore<T>::m_data_list;


didn''t know what T was.

Is there a way around this or do I just go with what I have (possibly trying to include a singleton in there somewhere)

Cheers,
Paul

Share this post


Link to post
Share on other sites
PaulCunningham    132
DOH!

Missed my * from the declaration.

map CDataStore::m_data_list;
map CDataStore::m_data_list;

Now works...

Is it still possible to do something like...

map CDataStore::m_data_list;

but so compiler knows what T is?

Cheers,
Paul Cunningham

[edited by - PaulCunningham on May 28, 2003 5:16:14 AM]

Share this post


Link to post
Share on other sites