alecthomas / entityx

EntityX - A fast, type-safe C++ Entity-Component system
MIT License
2.21k stars 295 forks source link

Enable move-only components. #210

Closed tylerreisinger closed 6 years ago

tylerreisinger commented 6 years ago

Allows for move-only objects to be used as components. For components that cannot be copied, trying to copy will result in a run time error like: Error trying to copy a component of type 'comp::Enemy' from entity Entity::Id(3.1) to entity Entity::Id(4.1).'comp::Enemy' is not copy constructable.

This relies on RTTI to get the component type name and on g++ and clang it requires an extension to demangle this name, but it should work on any compiler.

Implements #208.

alecthomas commented 6 years ago

I think RTTI is overkill for this. Just using SFINAE should be sufficient, then the compiler will generate an error trace pointing to the appropriate lines. Not quite as user friendly, but hey, C++ developers are used to that.

tylerreisinger commented 6 years ago

I totally agree that RTTI for this is a probably a bit much, but after looking at it for a while I didn't see a better solution that is also user friendly (even in the C++ sense). You can use this solution without RTTI, but that leads to creating an error without the misused type in question being specified. It will give a full stack trace in a debugger, but without one, the only message output will be that abort was called, or that a non-specified component was to be copied when it could not be.

The ComponentHelper class, by using type erasure and virtual methods prevents a clean compile-time detection of the error as any component type used will require the vtable and thus the copy_component_to function to be instantiated, which will cause an error regardless of whether that component was copied or not, even if only Entity::assign() is called.