#ifndef HANDLE_H
#define HANDLE_H
#include "vtable.h"
#include <cstddef>
#include <functional>
class handle
{
public:
~handle() noexcept;
template<class T>
handle(void* blk, T* src) noexcept
: handle(get_vtable_ptr<T>(), blk, src)
{}
handle(handle&& other) noexcept;
handle& operator=(handle&& other) noexcept;
handle(handle const&) = delete;
handle& operator=(handle const&) = delete;
void destroy() noexcept;
void transfer(void* blk, void* dst);
handle copy(void* blk, void* dst) const;
void swap(handle& other) noexcept;
std::size_t align() const noexcept;
std::size_t size() const noexcept;
void* blk() const noexcept;
void* src() const noexcept;
private:
handle(vtable_t const* vtable, void* blk, void* src) noexcept;
vtable_t const* vtable_;
void* blk_;
void* src_;
};
void swap(handle& a, handle& b) noexcept;
bool operator==(handle const& lhs, handle const& rhs) noexcept;
bool operator!=(handle const& lhs, handle const& rhs) noexcept;
namespace std
{
template<>
struct hash<handle>
{
size_t operator()(handle const& h) const
{
return hash<void*>{}(h.src());
}
};
}
#endif // HANDLE_H
As the name implied, this was not theThe full implementation (with iterators and a polymorphic_vectorstd::vector<> implementation, which can be implemented as a simple wrapper or derived class-like interface is omitted because of the amount of boilerplate code involved. This question is already long.
A sample toy implementation is provided in the demo below to demonstrate minimal polymorphic_vector_base. There is a lot of boilerplate involved, so I've omitted it from this already very long question usage.
- Correctness; there are some tricky memory management spots.
- Performance and efficiency of algorithms.
- Container choices.
Here is some sample codeNote: The (purely for demonstration) that shows usage ofpolymorphic_vector public interface should be based on polymorphic_vector_basestd::vector<> as defined in the C++ standard. This implementation is purely for demonstrative purposes.