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

Containers: add explicit StridedDimension conversion from/to external types #162

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
27 changes: 25 additions & 2 deletions src/Corrade/Containers/StridedDimensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Corrade { namespace Containers {

namespace Implementation {
template<unsigned, class, class> struct StridedDimensionsConverter;
template<unsigned, class, class> struct ExplicitStridedDimensionsConverter;
/* Used by StridedArrayView, needed here to friend them */
template<unsigned, class> struct StridedElement;
template<int> struct ArrayCastFlattenOrInflate;
Expand Down Expand Up @@ -87,7 +88,17 @@ template<unsigned dimensions, class T> class StridedDimensions {
* Conversion from a @ref StaticArrayView of the same type and
* dimension count is builtin.
*/
template<class U, class = decltype(Implementation::StridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(std::declval<U&&>()))> constexpr /*implicit*/ StridedDimensions(U&& other) noexcept: StridedDimensions{Implementation::StridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(Utility::forward<U>(other))} {}
template<class U, typename std::enable_if<sizeof(decltype(Implementation::StridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(std::declval<U&&>()))) != 0, int>::type = 0> constexpr /*implicit*/ StridedDimensions(U&& other) noexcept: StridedDimensions{Implementation::StridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(Utility::forward<U>(other))} {}

/**
* @brief Explicitly construct from an external representation
* @m_since_latest
*
* Compared to the above, this conversion is explicit and thus meant to
* be used in scenarios with a potential for data loss, such as
* conversion between signed and unsigned types.
*/
template<class U, typename std::enable_if<sizeof(decltype(Implementation::ExplicitStridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(std::declval<U&&>()))) != 0, int>::type = 0> constexpr explicit StridedDimensions(U&& other) noexcept: StridedDimensions{Implementation::ExplicitStridedDimensionsConverter<dimensions, T, typename std::decay<U&&>::type>::from(Utility::forward<U>(other))} {}

/**
* @brief Convert to an external representation
Expand All @@ -96,10 +107,22 @@ template<unsigned dimensions, class T> class StridedDimensions {
* Conversion to a @ref StaticArrayView of the same type and dimension
* count is builtin.
*/
template<class U, class = decltype(Implementation::StridedDimensionsConverter<dimensions, T, U>::to(std::declval<StridedDimensions<dimensions, T>>()))> constexpr /*implicit*/ operator U() const noexcept {
template<class U, typename std::enable_if<sizeof(decltype(Implementation::StridedDimensionsConverter<dimensions, T, U>::to(std::declval<StridedDimensions<dimensions, T>>()))) != 0, int>::type = 0> constexpr /*implicit*/ operator U() const noexcept {
return Implementation::StridedDimensionsConverter<dimensions, T, U>::to(*this);
}

/**
* @brief Explicitly convert to an external representation
* @m_since_latest
*
* Compared to the above, this conversion is explicit and thus meant to
* be used in scenarios with a potential for data loss, such as
* conversion between signed and unsigned types.
*/
template<class U, typename std::enable_if<sizeof(decltype(Implementation::ExplicitStridedDimensionsConverter<dimensions, T, U>::to(std::declval<StridedDimensions<dimensions, T>>()))) != 0, int>::type = 0> constexpr explicit operator U() const noexcept {
return Implementation::ExplicitStridedDimensionsConverter<dimensions, T, U>::to(*this);
}

/**
* @brief Conversion to a scalar
*
Expand Down
65 changes: 56 additions & 9 deletions src/Corrade/Containers/Test/StridedDimensionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@

namespace {

struct Rectangle {
constexpr Rectangle(int rows, int cols): rows{rows}, cols{cols} {}
struct Rectangleu {
constexpr Rectangleu(unsigned rows, unsigned cols): rows{rows}, cols{cols} {}

unsigned rows;
unsigned cols;
};

struct Rectanglei {
constexpr Rectanglei(int rows, int cols): rows{rows}, cols{cols} {}

int rows;
int cols;
Expand All @@ -46,11 +53,20 @@ namespace Corrade { namespace Containers {

namespace Implementation {

template<> struct StridedDimensionsConverter<2, std::size_t, Rectangle> {
constexpr static Size2D from(const Rectangle& other) {
template<> struct StridedDimensionsConverter<2, std::size_t, Rectangleu> {
constexpr static Size2D from(const Rectangleu& other) {
return {std::size_t(other.rows), std::size_t(other.cols)};
}
constexpr static Rectangle to(const Size2D& to) {
constexpr static Rectangleu to(const Size2D& to) {
return {unsigned(to[0]), unsigned(to[1])};
}
};

template<> struct ExplicitStridedDimensionsConverter<2, std::size_t, Rectanglei> {
constexpr static Size2D from(const Rectanglei& other) {
return {std::size_t(other.rows), std::size_t(other.cols)};
}
constexpr static Rectanglei to(const Size2D& to) {
return {int(to[0]), int(to[1])};
}
};
Expand All @@ -71,6 +87,7 @@ struct StridedDimensionsTest: TestSuite::Tester {
void convertScalar();
void convertScalar3D();
void convertExternal();
void convertExternalExplicit();
void convertExternalStaticArrayView();

void compare();
Expand All @@ -90,6 +107,7 @@ StridedDimensionsTest::StridedDimensionsTest() {
&StridedDimensionsTest::convertScalar,
&StridedDimensionsTest::convertScalar3D,
&StridedDimensionsTest::convertExternal,
&StridedDimensionsTest::convertExternalExplicit,
&StridedDimensionsTest::convertExternalStaticArrayView,

&StridedDimensionsTest::compare,
Expand Down Expand Up @@ -231,24 +249,53 @@ constexpr Size2D Sizes{34, 67};
void StridedDimensionsTest::convertExternal() {
Size2D a{12, 37};

Rectangle b = a;
Rectangleu b = a;
CORRADE_COMPARE(b.rows, 12);
CORRADE_COMPARE(b.cols, 37);

Size2D c = b;
CORRADE_COMPARE(c[0], 12);
CORRADE_COMPARE(c[1], 37);

constexpr Rectangle cb = Sizes;
constexpr Rectangleu cb = Sizes;
CORRADE_COMPARE(cb.rows, 34);
CORRADE_COMPARE(cb.cols, 67);

constexpr Size2D cc = cb;
CORRADE_COMPARE(cc[0], 34);
CORRADE_COMPARE(cc[1], 67);

CORRADE_VERIFY(std::is_nothrow_constructible<Size2D, Rectangle>::value);
CORRADE_VERIFY(std::is_nothrow_constructible<Rectangle, Size2D>::value);
CORRADE_VERIFY(std::is_nothrow_constructible<Size2D, Rectangleu>::value);
CORRADE_VERIFY(std::is_nothrow_constructible<Rectangleu, Size2D>::value);
}

void StridedDimensionsTest::convertExternalExplicit() {
Size2D a{12, 37};

Rectanglei b{a};
CORRADE_COMPARE(b.rows, 12);
CORRADE_COMPARE(b.cols, 37);

Size2D c{b};
CORRADE_COMPARE(c[0], 12);
CORRADE_COMPARE(c[1], 37);

constexpr Rectanglei cb{Sizes};
CORRADE_COMPARE(cb.rows, 34);
CORRADE_COMPARE(cb.cols, 67);

constexpr Size2D cc{cb};
CORRADE_COMPARE(cc[0], 34);
CORRADE_COMPARE(cc[1], 67);

/* Shouldn't be implicitly convertible */
CORRADE_VERIFY(std::is_convertible<Rectangleu, Size2D>::value);
CORRADE_VERIFY(std::is_convertible<Size2D, Rectangleu>::value);
CORRADE_VERIFY(!std::is_convertible<Rectanglei, Size2D>::value);
CORRADE_VERIFY(!std::is_convertible<Size2D, Rectanglei>::value);

CORRADE_VERIFY(std::is_nothrow_constructible<Size2D, Rectanglei>::value);
CORRADE_VERIFY(std::is_nothrow_constructible<Rectanglei, Size2D>::value);
}

void StridedDimensionsTest::convertExternalStaticArrayView() {
Expand Down