vectorwrapper/include/vectorwrapper/vectorwrapper.hpp.in
King_DuckZ fcaa62d69f Fix copy-construction warning in clang
This bumps the c++ requirement to c++17
2021-04-08 15:01:42 +02:00

427 lines
19 KiB
C++

/*
* Copyright 2015-2020 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "vectorwrapper/sequence_bt.hpp"
#include "vectorwrapper/size_type.hpp"
#include "vectorwrapper/implem_vec_base.hpp"
#include "vectorwrapper/implem_vec_common.hpp"
#include <ciso646>
#include <type_traits>
#include <array>
#include <cassert>
#include <functional>
#define VWR_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define VWR_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define VWR_VERSION_PATCH @PROJECT_VERSION_PATCH@
#if defined VWR_OUTER_NAMESPACE
namespace VWR_OUTER_NAMESPACE {
#endif
namespace vwr {
template <typename V>
struct VectorWrapperInfo;
template <typename V, size_type S>
class Vec;
namespace implem {
#if defined(VWR_WITH_IMPLICIT_CONVERSIONS)
template <typename V1, typename V2, size_type D>
Vec<V1>& assign ( Vec<V1, D>& parLeft, const Vec<V2, D>& parRight );
#endif
template <typename T, size_type S=VectorWrapperInfo<T>::dimensions>
struct offsets_array_wrapper {
template <size_type... I>
offsets_array_wrapper ( const bt::number_seq<size_type, I...>& );
const std::array<unsigned int, S> offsets;
};
template <typename V, bool Enabled> struct Vec1Promotion;
template <typename V> struct Vec1Promotion<V, false> { };
template <typename V> struct Vec1Promotion<V, true> {
typedef Vec<typename VectorWrapperInfo<V>::higher_vector_type> higher_vector_type;
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
static_assert(VectorWrapperInfo<typename VectorWrapperInfo<V>::higher_vector_type>::dimensions == 2, "Wrong promoted vector type");
higher_vector_type x1 ( void ) const { return xn(scalar_type(1)); }
higher_vector_type x0 ( void ) const { return xn(scalar_type(0)); }
higher_vector_type xn ( const scalar_type& parN ) const;
higher_vector_type nx ( const scalar_type& parN ) const;
};
template <typename V, bool Enabled> struct Vec2Promotion;
template <typename V> struct Vec2Promotion<V, false> {};
template <typename V> struct Vec2Promotion<V, true> {
typedef Vec<typename VectorWrapperInfo<V>::higher_vector_type> higher_vector_type;
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
static_assert(VectorWrapperInfo<typename VectorWrapperInfo<V>::higher_vector_type>::dimensions == 3, "Wrong promoted vector type");
higher_vector_type xy1 ( void ) const { return xyn(scalar_type(1)); }
higher_vector_type xy0 ( void ) const { return xyn(scalar_type(0)); }
higher_vector_type xyn ( const scalar_type& parZ ) const;
};
template <typename V, bool Enabled> struct Vec3Promotion;
template <typename V> struct Vec3Promotion<V, false> {};
template <typename V> struct Vec3Promotion<V, true> {
typedef Vec<typename VectorWrapperInfo<V>::higher_vector_type> higher_vector_type;
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
static_assert(VectorWrapperInfo<typename VectorWrapperInfo<V>::higher_vector_type>::dimensions == 4, "Wrong promoted vector type");
higher_vector_type xyz1 ( void ) const { return xyzw(scalar_type(1)); }
higher_vector_type xyz0 ( void ) const { return xyzw(scalar_type(0)); }
higher_vector_type xyzw ( const scalar_type& parW ) const;
};
template <typename V, bool Enabled> struct Vec3Demotion;
template <typename V> struct Vec3Demotion<V, false> : Vec3Promotion<V, HasHigherVecTypedef<V>::value> {};
template <typename V> struct Vec3Demotion<V, true> : Vec3Promotion<V, HasHigherVecTypedef<V>::value> {
typedef Vec<typename VectorWrapperInfo<V>::lower_vector_type> lower_vector_type;
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
static_assert(VectorWrapperInfo<typename VectorWrapperInfo<V>::lower_vector_type>::dimensions == 2, "Wrong demoted vector type");
lower_vector_type xy ( void ) const;
lower_vector_type xz ( void ) const;
lower_vector_type yz ( void ) const;
};
template <typename V, bool Enabled> struct Vec4Demotion;
template <typename V> struct Vec4Demotion<V, false> {};
template <typename V> struct Vec4Demotion<V, true> {
typedef Vec<typename VectorWrapperInfo<V>::lower_vector_type> lower_vector_type;
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
static_assert(VectorWrapperInfo<typename VectorWrapperInfo<V>::lower_vector_type>::dimensions == 3, "Wrong demoted vector type");
lower_vector_type xyz ( void ) const;
lower_vector_type xyw ( void ) const;
lower_vector_type xzw ( void ) const;
lower_vector_type yzw ( void ) const;
};
template <typename V, size_type D>
struct VecAccessors;
template <typename V>
struct VecAccessors<V, 4> : Vec4Demotion<V, HasLowerVecTypedef<VectorWrapperInfo<V>>::value> {
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
const Vec<V>& xyzw ( void ) const { return *static_cast<const Vec<V>*>(this); }
const scalar_type& x ( void ) const;
const scalar_type& y ( void ) const;
const scalar_type& z ( void ) const;
const scalar_type& w ( void ) const;
scalar_type& x ( void );
scalar_type& y ( void );
scalar_type& z ( void );
scalar_type& w ( void );
#if defined(VWR_EXTRA_SETTERS)
void set_x (const scalar_type& v) { x() = v; }
void set_y (const scalar_type& v) { y() = v; }
void set_z (const scalar_type& v) { z() = v; }
void set_w (const scalar_type& v) { w() = v; }
#endif
#if defined(VWR_EXTRA_ACCESSORS)
Vec<V> xyz0 ( void ) const { return Vec<V>(x(), y(), z(), scalar_type(0)); }
Vec<V> xyz1 ( void ) const { return Vec<V>(x(), y(), z(), scalar_type(1)); }
Vec<V> x0zw ( void ) const { return Vec<V>(x(), scalar_type(0), z(), w()); }
Vec<V> x1zw ( void ) const { return Vec<V>(x(), scalar_type(1), z(), w()); }
Vec<V> _0yzw ( void ) const { return Vec<V>(scalar_type(0), y(), z(), w()); }
Vec<V> _1yzw ( void ) const { return Vec<V>(scalar_type(1), y(), z(), w()); }
Vec<V> wxyz ( void ) const { return Vec<V>(w(), x(), y(), z()); }
#endif
};
//Workaround for visual studio - VecAccessors<V, 3> should inherit from
//both Vec3Promotion and Vec3Demotion, but when I do that, sizeof(Vec)
//is wrong. I had to linearize the inheritance hierarchy so that the
//sizeof(Vec<V>) == sizeof(V) condition is verified.
template <typename V>
struct VecAccessors<V, 3> : Vec3Demotion<V, HasLowerVecTypedef<VectorWrapperInfo<V>>::value> {
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
const Vec<V>& xyz ( void ) const { return *static_cast<const Vec<V>*>(this); }
const scalar_type& x ( void ) const;
const scalar_type& y ( void ) const;
const scalar_type& z ( void ) const;
scalar_type& x ( void );
scalar_type& y ( void );
scalar_type& z ( void );
#if defined(VWR_EXTRA_SETTERS)
void set_x (const scalar_type& v) { x() = v; }
void set_y (const scalar_type& v) { y() = v; }
void set_z (const scalar_type& v) { z() = v; }
#endif
#if defined(VWR_EXTRA_ACCESSORS)
Vec<V> xy0 ( void ) const { return Vec<V>(x(), y(), scalar_type(0)); }
Vec<V> xy1 ( void ) const { return Vec<V>(x(), y(), scalar_type(1)); }
Vec<V> x0z ( void ) const { return Vec<V>(x(), scalar_type(0), z()); }
Vec<V> x1z ( void ) const { return Vec<V>(x(), scalar_type(1), z()); }
Vec<V> _0yz ( void ) const { return Vec<V>(scalar_type(0), y(), z()); }
Vec<V> _1yz ( void ) const { return Vec<V>(scalar_type(1), y(), z()); }
Vec<V> zxy ( void ) const { return Vec<V>(z(), x(), y()); }
#endif
};
template <typename V>
struct VecAccessors<V, 2> : Vec2Promotion<V, HasHigherVecTypedef<VectorWrapperInfo<V>>::value> {
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
const Vec<V>& xy ( void ) const { return *static_cast<const Vec<V>*>(this); }
const scalar_type& x ( void ) const;
const scalar_type& y ( void ) const;
scalar_type& x ( void );
scalar_type& y ( void );
#if defined(VWR_EXTRA_SETTERS)
void set_x (const scalar_type& v) { x() = v; }
void set_y (const scalar_type& v) { y() = v; }
#endif
#if defined(VWR_EXTRA_ACCESSORS)
Vec<V> x0 ( void ) const { return Vec<V>(x(), scalar_type(0)); }
Vec<V> x1 ( void ) const { return Vec<V>(x(), scalar_type(1)); }
Vec<V> yx ( void ) const { return Vec<V>(y(), x()); }
Vec<V> _0y ( void ) const { return Vec<V>(scalar_type(0), y()); }
Vec<V> _1y ( void ) const { return Vec<V>(scalar_type(1), y()); }
#endif
};
template <typename V>
struct VecAccessors<V, 1> : Vec1Promotion<V, HasHigherVecTypedef<VectorWrapperInfo<V>>::value> {
typedef typename VectorWrapperInfo<V>::scalar_type scalar_type;
scalar_type& x ( void );
const scalar_type& x ( void ) const;
#if defined(VWR_EXTRA_SETTERS)
void set_x (const scalar_type& v) { x() = v; }
#endif
};
template <bool LastVal, typename V1, typename V2, typename ComposeOp, typename Op>
constexpr bool compare ( const Vec<V1>& parLeft, const Vec<V2>& parRight, Op parComposeOp, Op parOp, bt::number_seq<size_type> );
template <bool LastVal, typename V1, typename V2, typename ComposeOp, typename Op, size_type I1, size_type... I>
bool compare ( const Vec<V1>& parLeft, const Vec<V2>& parRight, ComposeOp parComposeOp, Op parOp, bt::number_seq<size_type, I1, I...> );
template <typename V1, typename V2, typename Op, size_type... I>
Vec<typename std::common_type<V1, V2>::type> binary_op ( const Vec<V1>& parLeft, const Vec<V2>& parRight, Op parOp, const Vec<typename std::common_type<V1, V2>::type>& parLastVal, bt::number_seq<size_type, I...> );
} //namespace implem
template <typename V, size_type S>
class Vec : public implem::VecBase<V> {
public:
enum {
dimensions = S
};
};
template <typename V>
class Vec<V, 1> : public implem::VecBase<V>, public implem::VecAccessors<V, 1> {
static_assert(std::is_standard_layout<implem::VecBase<V>>::value, "Base class must be a standard layout type");
public:
typedef typename implem::VecBase<V>::vector_type vector_type;
typedef typename implem::VecBase<V>::scalar_type scalar_type;
enum {
dimensions = 1
};
static const Vec<V, 1> unit_x;
Vec ( void ) = default;
Vec ( const Vec& parOther ) : implem::VecBase<V>(parOther) { }
Vec ( const vector_type& parIn ) : implem::VecBase<V>(parIn) { }
template <typename T>
explicit Vec ( const typename std::enable_if<std::is_same<T, scalar_type>::value and not std::is_same<scalar_type, vector_type>::value, T>::type& parX ) : implem::VecBase<V>(parX) { }
#if defined(VWR_WITH_IMPLICIT_CONVERSIONS)
template <typename V2> Vec ( const Vec<V2, dimensions>& parOther ) { implem::assign(*this, parOther); }
template <typename V2>
Vec& operator= ( const Vec<V2, dimensions>& parOther ) { return implem::assign(*this, parOther); }
#endif
Vec& operator= ( const Vec& parOther ) { return implem::assign_same_type(*this, parOther); }
};
template <typename V>
class Vec<V, 2> : public implem::VecBase<V>, public implem::VecAccessors<V, 2> {
static_assert(std::is_standard_layout<implem::VecBase<V>>::value, "Base class must be a standard layout type");
static_assert(std::is_standard_layout<implem::VecAccessors<V, 2>>::value, "Base class must be a standard layout type");
public:
typedef typename implem::VecBase<V>::scalar_type scalar_type;
typedef typename implem::VecBase<V>::vector_type vector_type;
enum {
dimensions = 2
};
static const Vec<V, 2> unit_x;
static const Vec<V, 2> unit_y;
Vec ( void ) = default;
Vec ( const Vec& parOther ) : implem::VecBase<V>(parOther) { }
Vec ( const vector_type& parIn ) : implem::VecBase<V>(parIn) { }
explicit Vec ( const scalar_type parX ) : implem::VecBase<V>(parX) { }
Vec ( scalar_type parX, scalar_type parY ) : implem::VecBase<V>(parX, parY) { }
#if defined(VWR_WITH_IMPLICIT_CONVERSIONS)
template <typename V2> Vec ( const Vec<V2, dimensions>& parOther ) { implem::assign(*this, parOther); }
template <typename V2>
Vec& operator= ( const Vec<V2, dimensions>& 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; }
};
template <typename V>
class Vec<V, 3> : public implem::VecBase<V>, public implem::VecAccessors<V, 3> {
static_assert(std::is_standard_layout<implem::VecBase<V>>::value, "Base class must be a standard layout type");
static_assert(std::is_standard_layout<implem::VecAccessors<V, 3>>::value, "Base class must be a standard layout type");
public:
typedef typename implem::VecBase<V>::scalar_type scalar_type;
typedef typename implem::VecBase<V>::vector_type vector_type;
enum {
dimensions = 3
};
static const Vec<V, 3> unit_x;
static const Vec<V, 3> unit_y;
static const Vec<V, 3> unit_z;
Vec ( void ) = default;
Vec ( const Vec& parOther ) : implem::VecBase<V>(parOther) { }
Vec ( const vector_type& parIn ) : implem::VecBase<V>(parIn) { }
explicit Vec ( const scalar_type parX ) : implem::VecBase<V>(parX) { }
Vec ( scalar_type parX, scalar_type parY, scalar_type parZ ) : implem::VecBase<V>(parX, parY, parZ) { }
#if defined(VWR_WITH_IMPLICIT_CONVERSIONS)
template <typename V2> Vec ( const Vec<V2, dimensions>& parOther ) { implem::assign(*this, parOther); }
template <typename V2>
Vec& operator= ( const Vec<V2, dimensions>& 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; }
};
template <typename V>
class Vec<V, 4> : public implem::VecBase<V>, public implem::VecAccessors<V, 4> {
static_assert(std::is_standard_layout<implem::VecBase<V>>::value, "Base class must be a standard layout type");
static_assert(std::is_standard_layout<implem::VecAccessors<V, 4>>::value, "Base class must be a standard layout type");
public:
typedef typename implem::VecBase<V>::scalar_type scalar_type;
typedef typename implem::VecBase<V>::vector_type vector_type;
enum {
dimensions = 4
};
static const Vec<V, 4> unit_x;
static const Vec<V, 4> unit_y;
static const Vec<V, 4> unit_z;
static const Vec<V, 4> unit_w;
Vec ( void ) = default;
Vec ( const Vec& parOther ) : implem::VecBase<V>(parOther) { }
Vec ( const vector_type& parIn ) : implem::VecBase<V>(parIn) { }
explicit Vec ( const scalar_type parX ) : implem::VecBase<V>(parX) { }
Vec ( scalar_type parX, scalar_type parY, scalar_type parZ, scalar_type parW ) : implem::VecBase<V>(parX, parY, parZ, parW) { }
#if defined(VWR_WITH_IMPLICIT_CONVERSIONS)
template <typename V2> Vec ( const Vec<V2, dimensions>& parOther ) { implem::assign(*this, parOther); }
template <typename V2>
Vec& operator= ( const Vec<V2, dimensions>& 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; }
};
template <typename V>
Vec<V> mk_vec ( const V& parVec );
template <typename V1, typename V2>
bool operator== ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator!= ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator< ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator> ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator<= ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator>= ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V>
bool operator== ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V>
bool operator< ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V>
Vec<V> operator- ( const Vec<V>& parVec );
template <typename V1, typename T>
bool operator> ( const Vec<V1>& parLeft, const T& parRight );
template <typename V1, typename T>
bool operator<= ( const Vec<V1>& parLeft, const T& parRight );
template <typename V1, typename T>
bool operator>= ( const Vec<V1>& parLeft, const T& parRight );
template <typename V1, typename T>
bool operator!= ( const Vec<V1>& parLeft, const T& parRight );
template <typename V1, typename V2>
Vec<typename std::common_type<V1, V2>::type> operator+ ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
Vec<typename std::common_type<V1, V2>::type> operator- ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
Vec<typename std::common_type<V1, V2>::type> operator* ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
Vec<typename std::common_type<V1, V2>::type> operator/ ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
Vec<typename std::common_type<V1, V2>::type> operator% ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V>
Vec<V> operator+ ( const Vec<V>& parLeft, const typename Vec<V>::scalar_type& parRight );
template <typename V>
Vec<V> operator- ( const Vec<V>& parLeft, const typename Vec<V>::scalar_type& parRight );
template <typename V>
Vec<V> operator* ( const Vec<V>& parLeft, const typename Vec<V>::scalar_type& parRight );
template <typename V>
Vec<V> operator/ ( const Vec<V>& parLeft, const typename Vec<V>::scalar_type& parRight );
template <typename V>
Vec<V> operator% ( const Vec<V>& parLeft, const typename Vec<V>::scalar_type& parRight );
template <typename V>
Vec<V> operator+ ( const typename Vec<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
Vec<V> operator- ( const typename Vec<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
Vec<V> operator* ( const typename Vec<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
Vec<V> operator/ ( const typename Vec<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
Vec<V> operator% ( const typename Vec<V>::scalar_type& parLeft, const Vec<V>& parRight );
} //namespace vwr
#if defined VWR_OUTER_NAMESPACE
} //namespace VWR_OUTER_NAMESPACE
#endif
#include "vectorwrapper/vectorwrapper.inl"