4 ms·
Even simpler with some template boilerplate code: // Just copy paste this template<typename T> class CContainer { protected: T *pim
by htfy96 8y ago
Even simpler with some template boilerplate code:
// Just copy paste this
template<typename T>
class CContainer {
protected:
T *pimpl;
CContainer(T* raw_ptr): pimpl(raw_ptr) {}
public:
CContainer(const CContainer&) = delete;
CContainer& operator=(const CContainer&) = delete;
virtual const char* get_my_name() = 0;
virtual ~CContainer() {};
};
// User
class MyCHandler : public CContainer<c_struct>
{
public:
MyCHandler(int arg) : CContainer(my_c_new(arg)) {}
virtual const char* get_my_name() override {
static const char[] kMyName = "MyC";
return kMyName;
}
virtual ~MyCHandler() {
my_c_destroy(pimpl);
}
std::unique_ptr<MyCHandler> clone() {
return std::make_unique<MyCHandler>(my_c_clone(p_impl));
}
};
Moreover, the zero-copy magic from pointer to option in Rust is not something like what you think - Rust relies on compiler magic to eliminate option type's heap allocation, and if you convert it from a pointer, it will reside on the heap.
- steveklabnik 8y agoThe option type never heap allocates on its own. There’s no reason to rely on compiler magic that doesn’t exist in the first place.