vectorwrapper/include/vectorwrapper/implem_vec_common.hpp

95 lines
3.5 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 "size_type.hpp"
#include "has_method.hpp"
#include <type_traits>
#if defined VWR_OUTER_NAMESPACE
namespace VWR_OUTER_NAMESPACE {
#endif
namespace vwr {
template <typename V>
struct VectorWrapperInfo;
typedef decltype(sizeof(int)) vwr_size_t;
template <typename V, size_type S=VectorWrapperInfo<V>::dimensions>
class Vec;
namespace implem {
define_has_typedef(lower_vector_type, LowerVec);
define_has_typedef(higher_vector_type, HigherVec);
define_has_enum(offset_x, OffsetX);
define_has_method(get_at, GetAt);
define_has_enum(cast_ignore_trailing_properties, CastIgnoreTrailingProperties);
template <typename T, bool=HasOffsetXEnum<VectorWrapperInfo<T>>::value and std::is_standard_layout<T>::value>
struct VecGetter;
template <typename T>
struct VecGetter<T, true> {
static typename VectorWrapperInfo<T>::scalar_type& get_at ( T& parVec, size_type parIndex );
};
template <typename T>
struct VecGetter<T, false> {
private:
static_assert(HasGetAtMethod<VectorWrapperInfo<T>>::value, "You must provide a get_at() static method for this vector_type");
typedef typename VectorWrapperInfo<T>::scalar_type scalar_type;
typedef T vector_type;
using get_at_func = decltype(&VectorWrapperInfo<T>::get_at)(size_type, vector_type&);
using get_at_rettype = typename std::result_of<get_at_func>::type;
static_assert(not std::is_rvalue_reference<get_at_rettype>::value, "rvalue ref return types not implemented");
static_assert(std::is_lvalue_reference<get_at_rettype>::value, "Read-only vectors not implemented");
public:
static get_at_rettype get_at ( T& parVec, size_type parIndex );
};
template <typename T, size_type I> struct get_offset_enum_from_index;
template <typename T> struct get_offset_enum_from_index<T, 0> {
static constexpr vwr_size_t value = VectorWrapperInfo<T>::offset_x;
};
template <typename T> struct get_offset_enum_from_index<T, 1> {
static constexpr vwr_size_t value = VectorWrapperInfo<T>::offset_y;
};
template <typename T> struct get_offset_enum_from_index<T, 2> {
static constexpr vwr_size_t value = VectorWrapperInfo<T>::offset_z;
};
template <typename T> struct get_offset_enum_from_index<T, 3> {
static constexpr vwr_size_t value = VectorWrapperInfo<T>::offset_w;
};
template <typename T, size_type S=VectorWrapperInfo<T>::dimensions> struct min_offset {
static constexpr vwr_size_t value = (
static_cast<int>(get_offset_enum_from_index<T, S-1>::value) < static_cast<int>(min_offset<T, S-1>::value) ?
static_cast<int>(get_offset_enum_from_index<T, S-1>::value) :
static_cast<int>(min_offset<T, S-1>::value)
);
};
template <typename T> struct min_offset<T, 1> {
static constexpr vwr_size_t value = get_offset_enum_from_index<T, 0>::value;
};
} //namespace implem
} //namespace vwr
#if defined VWR_OUTER_NAMESPACE
} //namespace VWR_OUTER_NAMESPACE
#endif