Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement detection of nested container #384

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions include/boost/gil/extension/numeric/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,30 @@ inline Kernel reverse_kernel(Kernel const& kernel)

namespace detail {

template <typename Core>
template <typename Container>
struct is_nested_container {
private:
// use decltype and skip expression with coma operator
// note that decltype argument must be an expression
template <typename Inner>
static constexpr decltype(std::declval<typename Inner::value_type>(), true) is_nested_impl(int)
{
return true;
}

// C style variable size arguments have least priority
// in overload resolution, thus previous definition
// will be selected if possible
template <typename U>
static constexpr bool is_nested_impl(...)
{
return false;
}
public:
constexpr static bool value = is_nested_impl<typename Container::value_type>(0);
};

template <typename Core, bool is_nested = is_nested_container<Core>::value>
class kernel_2d_adaptor : public Core
{
public:
Expand Down Expand Up @@ -256,7 +279,7 @@ class kernel_2d_adaptor : public Core
{
throw std::out_of_range("Index out of range");
}

return this->begin()[y * this->size() + x];
}

Expand All @@ -267,6 +290,11 @@ class kernel_2d_adaptor : public Core
protected:
std::size_t square_size{ 0 };
};

template <typename Core>
class kernel_2d_adaptor<Core, true> {
// Miral, over here! :-)
};
} //namespace detail

/// \brief variable-size kernel
Expand Down
17 changes: 16 additions & 1 deletion test/extension/numeric/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define BOOST_DISABLE_ASSERTS 1 // kernel_1d_adaptor assertions are too strict
#include <boost/gil/extension/numeric/kernel.hpp>
#include <vector>
#include <array>
#include <type_traits>

#define BOOST_TEST_MODULE test_ext_numeric_kernel
#include "unit_test.hpp"
Expand All @@ -32,7 +34,7 @@ BOOST_AUTO_TEST_CASE(kernel_2d_default_constructor)
BOOST_TEST(k.center_horizontal() == 0);

//BOOST_TEST(k.left_size() == 0);
//BOOST_TEST(k.right_size() == -1);
//BOOST_TEST(k.right_size() == -1);
BOOST_TEST(k.upper_size() == 0);
BOOST_TEST(k.lower_size() == -1);
// std::vector interface
Expand Down Expand Up @@ -304,4 +306,17 @@ BOOST_AUTO_TEST_CASE(kernel_1d_fixed_reverse_Kernel)
BOOST_TEST(k == values_rev);
}

template <typename Kernel2d>
struct is_detected_as_nested: std::false_type {};

template <typename T>
struct is_detected_as_nested<gil::detail::kernel_2d_adaptor<T, true>>: std::true_type {};

static_assert(is_detected_as_nested<gil::detail::kernel_2d_adaptor<std::vector<std::vector<double>>>>::value,
"did not detect nested container properly for kernel_2d_adaptor");
static_assert(is_detected_as_nested<gil::detail::kernel_2d_adaptor<std::array<std::array<double, 3>, 3>>>::value,
"did not detect nested container properly for kernel_2d_adaptor");
static_assert(!is_detected_as_nested<gil::detail::kernel_2d_adaptor<std::vector<double>>>::value,
"false positive for nested container detection for kernel_2d_adaptor");
static_assert(!is_detected_as_nested<gil::detail::kernel_2d_adaptor<std::array<double, 3>>>::value,
"false positive for nested container detection for kernel_2d_adaptor");