Skip to content

Commit

Permalink
[Type] Convert is_fixed_array trait to concept (#5209)
Browse files Browse the repository at this point in the history
Co-authored-by: Frederick Roy <[email protected]>
  • Loading branch information
alxbilger and fredroy authored Jan 17, 2025
1 parent d83d7ed commit 1072217
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Sofa/framework/Helper/src/sofa/helper/accessor/ReadAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class ReadAccessor
const_reference operator* () const { return *vref; }
};

template<class FixedArrayLikeType>
class ReadAccessor<FixedArrayLikeType, std::enable_if_t<sofa::type::trait::is_fixed_array<FixedArrayLikeType>::value>>
template<sofa::type::trait::is_fixed_array FixedArrayLikeType>
class ReadAccessor<FixedArrayLikeType>
: public ReadAccessorFixedArray< FixedArrayLikeType >
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace sofa::helper
{
////////////////////////// ReadAccessor for wrapping around fixed array like object //////////////////////
/// ReadAccessor implementation class for fixed array types
template<class T>
template<type::trait::is_fixed_array T>
class ReadAccessorFixedArray
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class WriteAccessor
}
};

template<class FixedArrayLikeType>
class WriteAccessor<FixedArrayLikeType, std::enable_if_t<sofa::type::trait::is_fixed_array<FixedArrayLikeType>::value>>
template<type::trait::is_fixed_array FixedArrayLikeType>
class WriteAccessor<FixedArrayLikeType>
: public WriteAccessorFixedArray< FixedArrayLikeType >
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace sofa::helper
{

/// WriteAccessor implementation class for fixed array types
template<class T>
template<type::trait::is_fixed_array T>
class WriteAccessorFixedArray
{
public:
Expand Down
36 changes: 7 additions & 29 deletions Sofa/framework/Type/src/sofa/type/trait/is_fixed_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,17 @@ namespace sofa::type::trait

/// Detect if a type T has iterator/const iterator function, operator[](size_t) and defines a static size
template<typename T>
struct is_fixed_array
concept is_fixed_array = requires(std::remove_cv_t<T> t, const std::remove_cv_t<T> ct)
{
typedef typename std::remove_const<T>::type test_type;
T::static_size;

template<typename A>
static constexpr bool test(
A * pt,
A const * cpt = nullptr,
decltype(pt->begin()) * = nullptr,
decltype(pt->end()) * = nullptr,
decltype(cpt->begin()) * = nullptr,
decltype(cpt->end()) * = nullptr,
typename std::decay<decltype((*pt)[0])>::type * = nullptr, ///< Is there an operator[] ?
decltype(A::static_size) * = nullptr, ///< fixed array containers define static_size
typename A::iterator * = nullptr,
typename A::const_iterator * = nullptr,
typename A::value_type * = nullptr)
{
{t.begin()} -> std::convertible_to<typename T::iterator>;
{t.end()} -> std::convertible_to<typename T::iterator>;

typedef typename A::iterator iterator;
typedef typename A::const_iterator const_iterator;
return std::is_same<decltype(pt->begin()),iterator>::value
&& std::is_same<decltype(pt->end()),iterator>::value
&& std::is_same<decltype(cpt->begin()),const_iterator>::value
&& std::is_same<decltype(cpt->end()),const_iterator>::value;
}
{ct.begin()} -> std::convertible_to<typename T::const_iterator>;
{ct.end()} -> std::convertible_to<typename T::const_iterator>;

template<typename A>
static constexpr bool test(...) {
return false;
}

static const bool value = test<test_type>(nullptr);
{ t[0] } -> std::convertible_to<typename T::value_type>;
};

}

0 comments on commit 1072217

Please sign in to comment.