Fix some build errors in certain cases.

- wrapped vector is not guaranteed to have constructors
- vector_type is not necessary
- has typedef and has enum were not always working
This commit is contained in:
King_DuckZ 2015-07-25 00:55:03 +02:00
parent 9b107d6e82
commit d3b63aab8c
4 changed files with 26 additions and 18 deletions

View file

@ -34,10 +34,10 @@
private: \
struct TrueType { int a[2]; }; \
typedef int FalseType; \
template <typename C> static decltype(C::typedef_name(), TrueType()) has_typedef ( int ); \
template <typename C> static TrueType has_typedef ( const typename C::typedef_name* ); \
template <typename C> static FalseType has_typedef ( ... ); \
public: \
enum { value = sizeof(has_typedef<T>(0)) == sizeof(TrueType) }; \
enum { value = sizeof(has_typedef<T>(nullptr)) == sizeof(TrueType) }; \
}
#define define_has_enum(enum_name,pretty_name) \
template <typename T> \
@ -45,8 +45,7 @@
private: \
struct TrueType { int a[2]; }; \
typedef int FalseType; \
template <int> void f ( void ); \
template <typename C> static TrueType has_enum ( decltype(&f<C::enum_name>) = nullptr ); \
template <typename C> static TrueType has_enum ( decltype(C::enum_name)* ); \
template <typename C> static FalseType has_enum ( ... ); \
public:\
enum { value = sizeof(has_enum<T>(nullptr)) == sizeof(TrueType) }; \

View file

@ -77,7 +77,7 @@ namespace vwr {
struct have_same_layout {
enum {
value =
HasOffsetXEnum<T>::value and HasOffsetXEnum<U>::value and
HasOffsetXEnum<VectorWrapperInfo<T>>::value and HasOffsetXEnum<VectorWrapperInfo<U>>::value and
VectorWrapperInfo<T>::dimensions == VectorWrapperInfo<U>::dimensions and
have_same_offsets<T, U>::value
};
@ -153,11 +153,11 @@ namespace vwr {
const std::array<unsigned int, S> offsets;
};
template <typename T, bool=HasOffsetXEnum<VectorWrapperInfo<T>>::value and std::is_standard_layout<typename VectorWrapperInfo<T>::vector_type>::value>
template <typename T, bool=HasOffsetXEnum<VectorWrapperInfo<T>>::value and std::is_standard_layout<T>::value>
class VecGetter;
template <typename T>
struct VecGetter<T, true> {
static typename VectorWrapperInfo<T>::scalar_type& get_at ( typename VectorWrapperInfo<T>::vector_type& parVec, std::size_t parIndex );
static typename VectorWrapperInfo<T>::scalar_type& get_at ( T& parVec, std::size_t parIndex );
};
template <typename T>
struct VecGetter<T, false> {
@ -169,7 +169,7 @@ namespace vwr {
static_assert(std::is_lvalue_reference<typename std::result_of<get_at_func>::type>::value, "Read-only vectors not implemented");
public:
static typename VectorWrapperInfo<T>::scalar_type& get_at ( typename VectorWrapperInfo<T>::vector_type& parVec, std::size_t parIndex );
static typename VectorWrapperInfo<T>::scalar_type& get_at ( T& parVec, std::size_t parIndex );
};
template <typename V, bool Enabled> struct Vec2Promotion;

View file

@ -24,17 +24,23 @@ namespace vwr {
}
template <typename V>
VecBase<V>::VecBase (const vector_type& parInit) :
m_wrapped(parInit)
{
VecBase<V>::VecBase (const vector_type& parInit) {
for (int z = 0; z < VectorWrapperInfo<V>::dimensions; ++z) {
VecGetter<V>::get_at(m_wrapped, z) = parInit;
}
}
template <typename V>
template <typename... Args>
VecBase<V>::VecBase (scalar_type parX, scalar_type parY, Args... parArgs) :
m_wrapped(parX, parY, parArgs...)
{
VecBase<V>::VecBase (scalar_type parX, scalar_type parY, Args... parArgs) {
static_assert(2 + sizeof...(Args) == dimensions, "Wrong number of parameters received");
VecGetter<V>::get_at(m_wrapped, 0) = parX;
VecGetter<V>::get_at(m_wrapped, 1) = parY;
const scalar_type args[sizeof...(Args)] = {parArgs...};
for (int z = 0; z < sizeof...(Args); ++z) {
VecGetter<V>::get_at(m_wrapped, z + 2) = args[z];
}
}
template <typename V>
@ -109,9 +115,9 @@ namespace vwr {
}
template <typename T>
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, true>::get_at (typename VectorWrapperInfo<T>::vector_type& parVec, std::size_t parIndex) {
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, true>::get_at (T& parVec, std::size_t parIndex) {
assert(parIndex < VectorWrapperInfo<T>::dimensions);
typedef typename VectorWrapperInfo<T>::vector_type vector_type;
typedef T vector_type;
typedef typename VectorWrapperInfo<T>::scalar_type scalar_type;
typedef scalar_type (vector_type::*coordinate_property);
static_assert(std::is_standard_layout<vector_type>::value, "Can't use this function with this vector_type");
@ -120,7 +126,7 @@ namespace vwr {
}
template <typename T>
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, false>::get_at (typename VectorWrapperInfo<T>::vector_type& parVec, std::size_t parIndex) {
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, false>::get_at (T& parVec, std::size_t parIndex) {
assert(parIndex < VectorWrapperInfo<T>::dimensions);
return VectorWrapperInfo<T>::get_at(parIndex, parVec);
}

View file

@ -154,7 +154,6 @@ namespace vwr {
typedef Vec<MixedDataVector3> mvec3;
typedef Vec<PaddedVector3> pvec3;
static_assert(not implem::HasOffsetXEnum<float>::value, "Should return false");
static_assert(sizeof(svec1) == sizeof(float), "Wrong size");
static_assert(sizeof(svec2) == sizeof(SimpleVector2), "Wrong size");
static_assert(sizeof(svec3) == sizeof(SimpleVector3), "Wrong size");
@ -163,6 +162,10 @@ namespace vwr {
static_assert(sizeof(ivec3) == sizeof(IntVector3), "Wrong size");
static_assert(sizeof(mvec3) == sizeof(MixedDataVector3), "Wrong size");
static_assert(sizeof(pvec3) == sizeof(PaddedVector3), "Wrong size");
//Vector Wrapper debug assertions
static_assert(not implem::HasOffsetXEnum<VectorWrapperInfo<float>>::value, "Should return false");
static_assert(implem::HasOffsetXEnum<VectorWrapperInfo<SimpleVector2>>::value, "Should return true");
} //namespace vwr
#endif