Here's the code to do what you want (probably won't compile as I don't have boost on hand and I'm writing it from memory): class YourClass; // your data type, defined somewhere else boost::object_pool allocator; void destroy(YourClass* pointer) { allocator. Destroy(pointer); } boost::shared_ptr create() { // usage of object_pool::construct requires that you have a // YourClass::YourClass(void) defined. If you need to pass arguments // to the new instance, you need to do that separately.
// // for example using a YourClass::Initialize(your,parameters,here) method // before returning from this function return boost::shared_ptr( allocator.construct(), &destroy ); } // usage: boost::shared_ptr newObject = create() I implemented this twice, in two different projects. In both, the create and destroy functions were synchronized (you can add a boost::mutex lock around the use of allocator) and they were members of a factory class (and the destroy s signature was modified to void (YourClass*) through the usage of boost::bind ) You can also avoid writing two extra functions (the destroy and create ) by binding object_pool::destroy dirrectly in the boost::shared_ptr constructor I'm too lazy to write all that now :) Edit (moved my answer comment in here for the code formatting): To bind the destroy function: class ClassFactory { boost::object_pool allocator; public: boost::shared_ptr create() { return boost::shared_ptr( allocator.construct(), boost::bind(&ClassFactory::destroy, this, _1) ); } void destroy(YourClass* p) { allocator. Destroy(pointer); } } ClassFactory should have a longer lifetime than the shared_ptr (if the ClassFactory instance is deleted, the this pointer passed to the shared_ptr instance will be invalid - and crash your app when the shared_ptr deletes the YourClass instance).
Here's the code to do what you want (probably won't compile as I don't have boost on hand and I'm writing it from memory): class YourClass; // your data type, defined somewhere else boost::object_pool allocator; void destroy(YourClass* pointer) { allocator. Destroy(pointer); } boost::shared_ptr create() { // usage of object_pool::construct requires that you have a // YourClass::YourClass(void) defined. If you need to pass arguments // to the new instance, you need to do that separately.
// // for example using a YourClass::Initialize(your,parameters,here) method // before returning from this function return boost::shared_ptr( allocator.construct(), &destroy ); } // usage: boost::shared_ptr newObject = create(); I implemented this twice, in two different projects. In both, the create and destroy functions were synchronized (you can add a boost::mutex lock around the use of allocator) and they were members of a factory class (and the destroy's signature was modified to void (YourClass*) through the usage of boost::bind). You can also avoid writing two extra functions (the destroy and create) by binding object_pool::destroy dirrectly in the boost::shared_ptr constructor.
I'm too lazy to write all that now :). Edit (moved my answer comment in here for the code formatting): To bind the destroy function: class ClassFactory { boost::object_pool allocator; public: boost::shared_ptr create() { return boost::shared_ptr( allocator.construct(), boost::bind(&ClassFactory::destroy, this, _1) ); } void destroy(YourClass* p) { allocator. Destroy(pointer); } }; ClassFactory should have a longer lifetime than the shared_ptr (if the ClassFactory instance is deleted, the this pointer passed to the shared_ptr instance will be invalid - and crash your app when the shared_ptr deletes the YourClass instance).
Thanks, this is what I was looking for. I would be interested in seeing how it is done using a factory class, I am having issues in binding the destroy function. – myahya May 26 '10 at 11:49.
These are almost orthogonal concerns. Shared_ptr plays no part in allocation of objects. Where it is concerned is in the deletion of memory no longer referenced.
If you have allocated from anything other than the default heap you'll need to provide a custom deleter.
As I understand it, shared_ptr can be defined to take a custom allocator: template shared_ptr(Y * p, D d, A a); The question is, what goes for D if I were to use Boost's pool_alloc – myahya May 26 '10 at 8:29 The allocator is for the counter object – philsquared May 26 '10 at 9:16.
Evident solution: Create your own make_shared function and enforce the use of this method to created shared_ptr. Those who derive from The Rule shall be punished. Note: There seems to be a confusion with the role of the shared_ptr.
Its role is to manage memory than YOU have allocated, however to do so it requires some allocation of its own (counter and deleter), thus you may pass it an allocator for those.
In both, the create and destroy functions were synchronized (you can add a boost::mutex lock around the use of allocator) and they were members of a factory class (and the destroy's signature was modified to void (YourClass*) through the usage of boost::bind). You can also avoid writing two extra functions (the destroy and create) by binding object_pool::destroy dirrectly in the boost::shared_ptr constructor. I'm too lazy to write all that now :).
ClassFactory should have a longer lifetime than the shared_ptr (if the ClassFactory instance is deleted, the this pointer passed to the shared_ptr instance will be invalid - and crash your app when the shared_ptr deletes the YourClass instance).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.