Skip to content

Instantly share code, notes, and snippets.

@nilium
Created March 30, 2009 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilium/87633 to your computer and use it in GitHub Desktop.
Save nilium/87633 to your computer and use it in GitHub Desktop.
// ignore this, just include bits as usual
#import <list>
#import <stdint.h>
// this is just an example class, don't read too much into it
class Object
{
private:
uint32_t m_refs;
public:
Object() : m_refs(0) {
retain();
}
virtual ~Object() {}
Object* retain() {
m_refs++;
return this;
}
void release() {
m_refs--;
if (m_refs <= 0) {
delete this;
}
}
uint32_t refs() {
return m_refs;
}
};
// extending class
class Foobar : public Object {
public:
Foobar() : Object() {}
virtual ~Foobar(){}
};
// this bit is important, first define ObjectList as being an std::list of
// Object pointers
typedef std::list<Object*> ObjectList;
// secondly, define ObjectIter as an iterator of an ObjectList.
// you don't really see this object when using BlitzMax, but in some sense
// it's there (check TList for ObjectEnumerator and TListEnum).
typedef ObjectList::iterator ObjectIter;
int main( int argc, char* argv[] )
{
ObjectList objList;
//ObjectList* objList = new ObjectList();
// ^^ also works, but you'll need to change .s to ->s where specified and
// dereference some things
// just populating the list, ignore this bit
for (int iter = 0; iter < 20; ++iter) {
objList.push_back(new Foobar());
}
// This is a hack of a macro to provide basic foreach support. I do not
// recommend this approach for dealing with unhappiness in using
// std::list, but you mentioned using preprocessor so I figured I'd
// illustrate. This only works with references to lists and not pointers,
// so you have to dereference them to use this (or change it to use ->
// instead of a .)
#define foreach(variable, list)\
for (typeof((list).begin()) variable##_ITER = (list).begin(); \
variable##_ITER != (list).end() && (variable = (*variable##_ITER)); ++variable##_ITER )
// object
Object* obj;
// iterate over the list using the hacky-macro
// foreach(obj, objList)
{
// do something with the object
}
// this is how you should do it, and although it's a bit more typing, it's
// not as hard to use as people will often make it look:
ObjectIter iter = objList.begin();
while ( iter != objList.end() )
{
obj = *iter;
obj->release();
*iter = NULL;
++iter; // next in the list
}
// there are fancier things you can do with std::list and its iterators,
// but this is more or less as simple as you can get with it short of
// using C++0x, and that's not really 'out' yet...
// for example, this
objList.clear();
// could have been handled in the above loop like this:
/*
while ( iter != objList.end() )
{
obj = *iter;
obj->release();
*iter = NULL;
iter = objList.erase(iter);
}
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment