From 6ab4d586eb5689c8bae7888b29339f1fc2b4d0f2 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 2 Nov 2016 01:12:00 +0100 Subject: [PATCH] Disable implicit conversions by default and update readme. Implicit conversions are still needed for the old unit test to build. --- README.md | 9 ++++++--- include/vectorwrapper/vectorwrapper.hpp | 20 ++++++++++++++------ test/unit/CMakeLists.txt | 4 ++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9863866..d161675 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ ## Motivation ## ### Short version ### -A vector wrapper is mainly useful if used as an adapter between vectors from different libraries. It basically removes the need to explicitly construct a vector of type B when you have a vector of type A. -It's roughly equivalent to using a reinterpret_cast on your vectors, but the code tries really hard to make sure the cast is valid in all cases, wrapping it nicely behind a clean interface. Implicit constructors also allow to convert easily from one type to the other. +A vector wrapper is useful if used as an adapter between vectors from different libraries. It removes the need to explicitly construct a vector of type B when you have a vector of type A. +It's roughly equivalent to using a reinterpret_cast on your vectors, but the code tries really hard to make sure the cast is valid in all cases, wrapping safety checks nicely behind a clean interface. Implicit constructors also allow to convert easily from one type to the other (if enabled at build time). ### Long version ### We all need vectors in our code, and there are quite a few implementations out there. They all do their job, and in terms of interface they are pretty similar to each other, but not *exactly* the same thing. @@ -27,8 +27,11 @@ Unfortunately the fact that you use `Vec>` and `Vec` from a `Vec`, provided they have the same dimensions and the assignemnt operator is able to convert B's scalar type to that of A. +You can assign or construct a `Vec` from a `Vec`, provided they have the same dimensions and the assignemnt operator is able to convert B's scalar type to that of A. You need to define VWR_WITH_IMPLICIT_CONVERSIONS for this to work. ### Access the wrapped type ### Through the `data()` method you can always obtain a ref to the wrapped type. diff --git a/include/vectorwrapper/vectorwrapper.hpp b/include/vectorwrapper/vectorwrapper.hpp index 4f5a7c2..2c7f106 100644 --- a/include/vectorwrapper/vectorwrapper.hpp +++ b/include/vectorwrapper/vectorwrapper.hpp @@ -39,8 +39,10 @@ namespace vwr { define_has_method(get_at, GetAt); define_has_enum(cast_ignore_trailing_properties, CastIgnoreTrailingProperties); +#if defined(VWR_WITH_IMPLICIT_CONVERSIONS) template Vec& assign ( Vec& parLeft, const Vec& parRight ); +#endif template Vec& assign_same_type ( Vec& parLeft, const Vec& parRight ); @@ -318,11 +320,13 @@ namespace vwr { explicit Vec ( const vector_type& parIn ) : implem::VecBase(parIn) { } template explicit Vec ( const typename std::enable_if::value and not std::is_same::value, T>::type& parX ) : implem::VecBase(parX) { } +#if defined(VWR_WITH_IMPLICIT_CONVERSIONS) template Vec ( const Vec& parOther ) { implem::assign(*this, parOther); } - - Vec& operator= ( const Vec& parOther ) { return implem::assign_same_type(*this, parOther); } template Vec& operator= ( const Vec& parOther ) { return implem::assign(*this, parOther); } +#endif + + Vec& operator= ( const Vec& parOther ) { return implem::assign_same_type(*this, parOther); } }; template @@ -344,11 +348,13 @@ namespace vwr { explicit Vec ( const vector_type& parIn ) : implem::VecBase(parIn) { } explicit Vec ( const scalar_type parX ) : implem::VecBase(parX) { } Vec ( scalar_type parX, scalar_type parY ) : implem::VecBase(parX, parY) { } +#if defined(VWR_WITH_IMPLICIT_CONVERSIONS) template Vec ( const Vec& parOther ) { implem::assign(*this, parOther); } - - Vec& operator= ( const Vec& parOther ) { return implem::assign_same_type(*this, parOther); } template Vec& operator= ( const Vec& parOther ) { return implem::assign(*this, parOther); } +#endif + + Vec& operator= ( const Vec& parOther ) { return implem::assign_same_type(*this, parOther); } Vec& operator= ( const vector_type& parOther ) { this->data() = parOther; return *this; } }; @@ -372,11 +378,13 @@ namespace vwr { explicit Vec ( const vector_type& parIn ) : implem::VecBase(parIn) { } explicit Vec ( const scalar_type parX ) : implem::VecBase(parX) { } Vec ( scalar_type parX, scalar_type parY, scalar_type parZ ) : implem::VecBase(parX, parY, parZ) { } +#if defined(VWR_WITH_IMPLICIT_CONVERSIONS) template Vec ( const Vec& parOther ) { implem::assign(*this, parOther); } - - Vec& operator= ( const Vec& parOther ) { return implem::assign_same_type(*this, parOther); } template Vec& operator= ( const Vec& parOther ) { return implem::assign(*this, parOther); } +#endif + + Vec& operator= ( const Vec& parOther ) { return implem::assign_same_type(*this, parOther); } Vec& operator= ( const vector_type& parOther ) { this->data() = parOther; return *this; } }; diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 6dba8a6..34ecd47 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -11,3 +11,7 @@ add_executable(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} gtest ) + +target_compile_definitions(${PROJECT_NAME} + PRIVATE VWR_WITH_IMPLICIT_CONVERSIONS +)