Skip to content

Commit

Permalink
fix and test
Browse files Browse the repository at this point in the history
  • Loading branch information
alxbilger committed Jan 9, 2025
1 parent c431880 commit 452b3ec
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
11 changes: 9 additions & 2 deletions Sofa/framework/Type/src/sofa/type/trait/Rebind.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

namespace sofa::type
{
template<class T, class OtherType>
concept CanTypeRebind = requires
{
typename T::template rebind_to<OtherType>;
};


/**
* Depending on the type _T, has a public member typedef to. Otherwise, there is no member typedef (this is the
* case of this implementation).
Expand All @@ -37,7 +44,7 @@ namespace sofa::type
* \tparam _T Type that does have a nested ::rebind_to member
*/
template<class _T, class _OtherType>
requires requires { _T::template rebind_to<_OtherType>; }
requires CanTypeRebind<_T, _OtherType>
struct Rebind<_T, _OtherType>
{
using to = typename _T::template rebind_to<_OtherType>;
Expand All @@ -49,7 +56,7 @@ namespace sofa::type
* \tparam _T Type that does NOT have a nested ::rebind_to member
*/
template<template<class> class _T, class A, class _OtherType>
requires (!requires { _T<A>::template rebind_to<_OtherType>; })
requires !CanTypeRebind<_T<A>, _OtherType>
struct Rebind<_T<A>, _OtherType>
{
using to = _T<_OtherType>;
Expand Down
1 change: 1 addition & 0 deletions Sofa/framework/Type/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(SOURCE_FILES
MatTypes_test.cpp
Material_test.cpp
Quater_test.cpp
Rebind_test.cpp
RGBAColor_test.cpp
StrongType_test.cpp
SVector_test.cpp
Expand Down
59 changes: 59 additions & 0 deletions Sofa/framework/Type/test/Rebind_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/type/trait/Rebind.h>
#include <sofa/type/vector.h>

static_assert(sofa::type::CanTypeRebind<sofa::type::vector<float>, int>);
static_assert(sofa::type::CanTypeRebind<sofa::type::vector<int>, int>);

static_assert(
std::is_same_v<
sofa::type::rebind_to<sofa::type::vector<float>, int>,
sofa::type::vector<int>
>);
static_assert(
std::is_same_v<
sofa::type::rebind_to<sofa::type::vector<int>, int>,
sofa::type::vector<int>
>);

template<class T>
struct DummyNoRebind{};

static_assert(!sofa::type::CanTypeRebind<DummyNoRebind<float>, int>);

static_assert(
std::is_same_v<
sofa::type::rebind_to<DummyNoRebind<float>, int>,
DummyNoRebind<int>
>);

template<class T>
struct DummyWithConstraintRebind
{
template<class U>
requires std::is_integral_v<U>
using rebind_to = U;
};

static_assert(sofa::type::CanTypeRebind<DummyWithConstraintRebind<float>, int>);
static_assert(!sofa::type::CanTypeRebind<DummyWithConstraintRebind<float>, std::string>);
18 changes: 11 additions & 7 deletions Sofa/framework/Type/test/vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class vector_test : public NumericTest<>,
void checkVector(const std::vector<std::string>& params) ;
void checkVectorAccessFailure() const;

void checkRebind();
void checkRebind() const;
};

template<class T>
Expand Down Expand Up @@ -103,14 +103,18 @@ void vector_test<T>::checkVectorAccessFailure() const
}

template <class T>
void vector_test<T>::checkRebind()
void vector_test<T>::checkRebind() const
{
constexpr bool hasRebind = sofa::type::HasRebindTypedef<vector<T>, int>::value;
constexpr bool hasRebind = sofa::type::CanTypeRebind<vector<T>, int>;
static_assert(hasRebind);
EXPECT_TRUE(hasRebind);
using rebinded = typename sofa::type::Rebind<vector<T>, int >::to;
using vec_int = vector<int>;
constexpr bool isRebindOK = std::is_same_v<rebinded, vec_int >;
EXPECT_TRUE(isRebindOK);
if constexpr (hasRebind)
{
using rebinded = typename sofa::type::Rebind<vector<T>, int >::to;
using vec_int = vector<int>;
constexpr bool isRebindOK = std::is_same_v<rebinded, vec_int >;
EXPECT_TRUE(isRebindOK);
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 452b3ec

Please sign in to comment.