• entries
    503
  • comments
    1888
  • views
    333745

More new engine stuff

Sign in to follow this  

57 views

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

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