I've just realised what you mean now, I thought heap and heap_array where scoped pointers. But there's just used as a parameter to the handle class, kinda like std::allocator.
What does it mean to copy a handle class? Should it copy the handle, or share the handle with the other object.
If you just need to share the handle, adding a ref count to heap and heap_array would solve that problem. If you need to copy the object being handled, I can't think of a way to do it without passing in the size to one of the objects. To keep it as generic as possible, you could use another template class that does the copying.
template<typename Type>struct copy_resource{ static Type* copy(const Type* src) const { return new Type(src); }};
Then you could defined heap like this:
template <typename type, typename copier = copy_resource<type> >struct heap : copier{ void deallocate(type* resource) { delete( resource ); } };
The reason for the inheritance is so that handle just calls whatever::copy(src), that way you can use a static function (there's no instance date) so it'll keep the handle class small (deallocate could also be static). You could change it to a private base class then add a using declaration if you like, as long as heap exposes a copy it I'll work.
For heap_handle you'll need a way for it to know the size of the thing it's holding, so something like this should work (although it means the size needs to be know at compile time)
template<typename Type, size_t Size>struct copy_resource_array{ static Type* copy(const Type* src) const { Type* temp = new Type[Size]; std::copy(src, src+Size, temp); return temp; }};
The problem with this is that you need to know the size at compile time, which you probably wont. But unless you give clients access to the ctor of heap_array somehow I can't think of another way to do this.
If you don't want to pass a copier to heap_array when you don't need to copy it, you could create another class which will generate a compile time error when you try to copy it:
template<typename Type>struct cant_copy_resource{ static Type* copy(const Type* src) const { int cant_copy[-1]; //the -1 size will give you a compile error }};
then make cant_copy_resource the default copier for heap_array.
Making the copier a template type lets you have more interesting coping for some types, for example a class that has a virtual clone function to clone it could be used like this
template<typename Type>struct prototype_resource{ static Type* copy(const Type* src) const { return src->clone(); }};
This wouldn't really fit in with the rest of your handle class though, so it's probably better to go in an std::auto_ptr type class. I might have missed the point again here, which would make this a waste of time
btw is there a reason you can't use std::vector instead of heap_array?
Edited by - Wilka on September 9, 2000 8:10:32 PM