//References: // The symbols in [] below are used to make references // in the code following. /* [PROB]: http://article.gmane.org/gmane.comp.lib.boost.devel/243633 [SOLN]: http://article.gmane.org/gmane.comp.lib.boost.devel/243634 */ //Purpose: // See if problem posed in [PROB] can be solved using // boost::offset_ptr as suggested in [SOLN]. // #include #include unsigned object_count=0; struct non_pod_object /**@brief * This has virtual functions making (IIUC) * any objects of this class a "non-POD" * object as required by the proposed problem * (see [PROBLEM] above). */ { unsigned my_id /**@brief * This keeps count of number of times * the non_pod_object::CTOR has been * called. This will also give some * evidence on if the COPY_CTOR has * been called. */ ; non_pod_object*const raw_ptr /**@brief * This is the original problem posed in [PROB] */ ; boost::interprocess::offset_ptr const off_ptr /**@brief * This is the solution proposed in [SOLN] */ ; non_pod_object() : my_id(++object_count) , raw_ptr(this) , off_ptr(this) { std::cout<<"default CTOR:\n"; this->print(std::cout); } non_pod_object(non_pod_object const&a_obj) : my_id(++object_count) , raw_ptr(this) , off_ptr(this) { std::cout<<"copy CTOR:\n"; std::cout<<"[ from=\n"; a_obj.print(std::cout); std::cout<<", to=\n"; this->print(std::cout); std::cout<<"]\n"; } void operator=(non_pod_object const&) {} virtual ~non_pod_object() {} virtual void print(std::ostream&sout)const { sout <<"non_pod_object" <<"( my_id="< { typedef std::vector super_type; static std::size_t const size=sizeof(non_pod_object); storage():super_type(size){} storage(storage const&a_from):super_type(a_from){} char*data() { return super_type::data(); } char const*data()const { return super_type::data(); } }; int main() { { //The following just demonstrates that //the DEFAULT and COPY CTOR's do work //as expected. non_pod_object def_stk; non_pod_object cpy_stk(def_stk); } { //This is the real test to see if offset_ptr //will preserve the correct value when //the storage is copy from one vector to another. storage store_from;//This "simulates" storage before resize. non_pod_object*ptr_from= new(store_from.data()) non_pod_object; std::cout<<"ptr_from->print=\n"; ptr_from->print(std::cout); storage store_to(store_from);//this "simulates" storage after resize. void*ptr_void=store_to.data(); non_pod_object*ptr_to=static_cast(ptr_void); std::cout<<"ptr_to->print=\n"; ptr_to->print(std::cout); } return 0; }