Skip to content

Cannot create a component without copy semantics. #5

@WarlockD

Description

@WarlockD

I wanted to create a compoent as such:

kult::component<'ptr', std::unique_ptr<Renderable>> krenderable

The problem is that a unique_ptr is not copyable so the template fails on the merge method within the component. I create a template hack so that when it runs merge it just uses the default constructor.

... somwhere above eveything
struct _default_create {};
struct _can_copy : _default_create {};
template<class B>
    typename std::enable_if<std::is_default_constructible<B>::value>::type
        inline _assign(B& l, B&, _default_create) {
    l = B();
}

template<class B>
typename std::enable_if<std::is_copy_assignable<B>::value>::type
    inline _assign(B& l, B& r, _can_copy) {
    l = r;
}

... changing the merge in enity
virtual void merge(const type &dst, const type &src) const {
    //add<component>(dst) = T(get<component>(src));
    _assign(add<component>(dst), get<component>(src), _can_copy());
}

It will still throw errors if you cannot copy AND its not default construable. To be honest I think you should have copy semantics. I built a unique_ptr container that takes a template abstract class and derived class that does this, but I thought I post the hack I made.

The only other way I can think of doing this is having the component object store std::function of copy/move/constructor/deconstructor. I now see why unique_ptr carries around a separate deconstructor now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions