Skip to content

Commit

Permalink
use has_raw_data_iface in xview
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed May 3, 2017
1 parent aab23a0 commit 069a5ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
13 changes: 0 additions & 13 deletions include/xtensor/xstridedview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,19 +716,6 @@ namespace xt

namespace detail
{
template <typename T>
class has_raw_data_interface
{
template <typename C>
static std::true_type test(decltype(std::declval<C>().raw_data_offset()));

template <typename C>
static std::false_type test(...);

public:
constexpr static bool value = decltype(test<T>(std::size_t(0)))::value == true;
};

template <class E, std::enable_if_t<has_raw_data_interface<std::decay_t<E>>::value>* = nullptr>
inline auto&& get_data(E&& e)
{
Expand Down
17 changes: 17 additions & 0 deletions include/xtensor/xutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,23 @@ namespace xt
{
return N;
}

/*****************************************
* has_raw_data_interface implementation *
*****************************************/

template <typename T>
class has_raw_data_interface
{
template <typename C>
static std::true_type test(decltype(std::declval<C>().raw_data_offset()));

template <typename C>
static std::false_type test(...);

public:
constexpr static bool value = decltype(test<T>(std::size_t(0)))::value == true;
};
}

#endif
29 changes: 12 additions & 17 deletions include/xtensor/xview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,13 @@ namespace xt
using iterable_base = xexpression_iterable<self_type>;
using inner_shape_type = typename iterable_base::inner_shape_type;
using shape_type = inner_shape_type;
using strides_type = shape_type;

using slice_type = std::tuple<S...>;

using stepper = typename iterable_base::stepper;
using const_stepper = typename iterable_base::const_stepper;

using broadcast_iterator = typename iterable_base::broadcast_iterator;
using const_broadcast_iterator = typename iterable_base::const_broadcast_iterator;

using iterator = typename iterable_base::iterator;
using const_iterator = typename iterable_base::const_iterator;

static constexpr layout_type static_layout = layout_type::dynamic;
static constexpr bool contiguous_layout = false;

Expand Down Expand Up @@ -154,23 +149,23 @@ namespace xt
const_stepper stepper_end(const ST& shape) const;

template <class T = xexpression_type>
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const typename T::container_type&>
std::enable_if_t<has_raw_data_interface<T>::value, const typename T::container_type&>
data() const;

template <class T = xexpression_type>
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const typename T::strides_type>
std::enable_if_t<has_raw_data_interface<T>::value, const typename T::strides_type>
strides() const;

template <class T = xexpression_type>
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const value_type*>
std::enable_if_t<has_raw_data_interface<T>::value, const value_type*>
raw_data() const;

template <class T = xexpression_type>
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, value_type*>
std::enable_if_t<has_raw_data_interface<T>::value, value_type*>
raw_data();

template <class T = xexpression_type>
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const std::size_t>
std::enable_if_t<has_raw_data_interface<T>::value, const std::size_t>
raw_data_offset() const noexcept;

size_type underlying_size(size_type dim) const;
Expand Down Expand Up @@ -519,7 +514,7 @@ namespace xt
template <class CT, class... S>
template <class T>
inline auto xview<CT, S...>::data() const ->
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const typename T::container_type&>
std::enable_if_t<has_raw_data_interface<T>::value, const typename T::container_type&>
{
return m_e.data();
}
Expand All @@ -530,7 +525,7 @@ namespace xt
template <class CT, class... S>
template <class T>
inline auto xview<CT, S...>::strides() const ->
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const typename T::strides_type>
std::enable_if_t<has_raw_data_interface<T>::value, const typename T::strides_type>
{
using strides_type = typename T::strides_type;
strides_type temp = m_e.strides();
Expand Down Expand Up @@ -560,15 +555,15 @@ namespace xt
template <class CT, class... S>
template <class T>
inline auto xview<CT, S...>::raw_data() const ->
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const value_type*>
std::enable_if_t<has_raw_data_interface<T>::value, const value_type*>
{
return m_e.raw_data();
}

template <class CT, class... S>
template <class T>
inline auto xview<CT, S...>::raw_data() ->
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, value_type*>
std::enable_if_t<has_raw_data_interface<T>::value, value_type*>
{
return m_e.raw_data();
}
Expand All @@ -579,10 +574,10 @@ namespace xt
template <class CT, class... S>
template <class T>
inline auto xview<CT, S...>::raw_data_offset() const noexcept ->
std::enable_if_t<std::is_base_of<xcontainer<std::remove_const_t<std::decay_t<T>>>, T>::value, const std::size_t>
std::enable_if_t<has_raw_data_interface<T>::value, const std::size_t>
{
auto func = [](const auto& s) { return xt::value(s, 0); };
typename T::size_type offset = 0;
typename T::size_type offset = m_e.raw_data_offset();

for (size_type i = 0; i < sizeof...(S); ++i)
{
Expand Down

0 comments on commit 069a5ac

Please sign in to comment.