🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

More new engine stuff

posted in A Keyboard and the Truth for project 96 Mill
Published August 31, 2005
Advertisement
So, having my nice new smart pointer object, I decided that it would be a good idea to make practical use of it.

So I started work on a simple ArrayList, which is type safe and plays nicely with the smart pointer.

It is lacking about half of the functaionlity it should have (notably removes, and the FIFO LIFO add/remove variants.)

but it proves the concept, indeed I made bunches of pointers and passed them in and out and etc. and everything got released properly. I for one am happy to leave the delete keyword behind =)

#if !defined(ARRAYLIST_H)#define ARRAYLIST_H#include "assert.h"#include "object.h"#include "pointer.h"template <class T> class ArrayList : public Object{private:	Pointer**	list;	unsigned int	size;public:	ArrayList(void)	{		list=0;		size=0;	}	~ArrayList(void)	{		clear();	}	bool add(Pointer o)	{		size++;		Pointer** newl=new Pointer*[size];		assert(newl);		if(list)		{			newl=(Pointer**)memcpy(newl,list,sizeof(Pointer*)*size);			delete[] list;		}		list=newl;		Pointer* ptr=new Pointer(o);		list[size-1]=ptr;		return true;	}	bool addAll(Pointer al)	{		unsigned int oldsize=size;		size+=al->size;		Pointer** newl=new Pointer*[size];		assert(newl);		if(list)		{			newl=(Pointer**)memcpy(newl,list,sizeof(Pointer*)*size);			delete[] list;		}		list=newl;		Pointer* ptr;		for(int i=0;isize;i++)		{			ptr=new Pointer(al->get(i));			list[oldsize+i]=ptr;		}		return true;	}	Pointer get(unsigned int index)	{		assert(index < size);		return *list[index];	}	Pointer set(unsigned int index,Pointer o)	{		assert(index < size);		Pointer p=get(index);		*list[index]=o;		return p;	}	void clear(void)	{		if(list)		{			Pointer** lptr=list;			for(int i=0;i			{				delete *lptr;				lptr++;			}			delete[] list;			list=0;			size=0;		}	}};#endif
Previous Entry Forward =)
Next Entry In by the buzzer!
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement