I figured out how to do it. Here''s what I''ve got so far:
namespace deallocation {
template <typename type>
struct heap
{ void deallocate(type* resource) { delete( resource ); } };
template <typename type>
struct stack
{ void deallocate(type* resource) { resource->~type(); } }
};
namespace duplication {
template <typename type, typename second_type = type>
struct heap
{ type* duplicate(second_type* resource) { return( new type(*resource) ); } };
// for stack, implement something special with placement new...
};
The handle code simply calls those functions on its members. duplication::heap is only used by copy_handle. If the user of the handle classes wants to use handles of different classes in a hierarchy interchangably, he has to declare the proper deallocator as the base class, and should use virtual destructors. Also, the duplicator class as a templated struct cannot be used interchangably with different parameters for the "type" template parameter, so copying is always safe.
About the array stuff, I disallowed any use of delete[] as a deallocator. Remember, if the user wants an array, he/she should use vector instead of a handle (it''s essentially the same thing, but vector is much better suited and does a lot more). Nothing prevents the user from having a handle to a vector or list, or whatever container type he/she wishes.
BTW, the stack duplicator is for placement new to "emulate" the stack with a set chunk of heap memory. I thought it might be a very useful feature for holding matrices/vertices/etc. If anyone has any suggestions on how to do that, I''d be grateful!
Whew! finally done!
Thanks Wilka!
- null_pointer
Sabre Multimedia