Skip to content
Gautier Lefebvre edited this page Feb 13, 2018 · 5 revisions

Pool

Create pooled object

You can easily create a pooled object by declaring the class this way:

class Foo :public fwl::APooled<Foo> {
    // class declaration

public:
    // must implement this method
    virtual void reinit(void);

    // can use this method to get an object from the pool and init it right away.
    void init(int, int, int);
};

You need to initialize the pool, otherwise you would need to fill it yourself, and once it is empty you would get an exception when trying to get objects from it:

Foo::initPool(
    10,     // pool size
    5,      // number of objects to add to the pool when it is empty
    "Foo"); // name of the class of the pooled objects

You can clear it (but you don't have to, the pool is scalar and will be emptied when the program ends).

Foo::destroyPool();

You can now get objects from the pool by using this:

Foo* foo = Foo::getFromPool();
// or add arguments, passed to a init() method of your Foo class with the same arguments.
Foo* bar = Foo::getFromPool(1, 2, 3);

And return the objects to the pool:

Foo* foo = Foo::getFromPool();
// use foo
Foo::returnToPool(foo);

Known issue

I haven't figured out a way to call the init method without arguments without having to declare it in any pooled class. Somehow it does not work when adding an init method without arguments in the fwk::APooled class.

If you have an init method without arguments, you have to call it yourself.

Foo* foo = Foo::getFromPool();
foo->init();
Clone this wiki locally