Skip to content

Commit

Permalink
[Type] Convert is_vector trait to concept (sofa-framework#5201)
Browse files Browse the repository at this point in the history
* [Type] Concert is_vector trait to concept

* fix

* use the concept
  • Loading branch information
alxbilger authored Jan 17, 2025
1 parent 84e79d1 commit d83d7ed
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class BaseContext::GetObjectsCallBackT : public BaseContext::GetObjectsCallBack
GetObjectsCallBackT(Container* d) : dest(d) {}
void operator()(void* ptr) override
{
if constexpr (sofa::type::trait::is_vector<Container>::value)
if constexpr (sofa::type::trait::is_vector<Container>)
{
dest->push_back(reinterpret_cast<T*>(ptr));
}
Expand Down
5 changes: 2 additions & 3 deletions Sofa/framework/Helper/src/sofa/helper/accessor/ReadAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ class ReadAccessor<FixedArrayLikeType, std::enable_if_t<sofa::type::trait::is_fi
ReadAccessor(const container_type& c) : Inherit(c) {}
};

template<class VectorLikeType>
class ReadAccessor<VectorLikeType,
std::enable_if_t<sofa::type::trait::is_vector<VectorLikeType>::value> >
template<sofa::type::trait::is_vector VectorLikeType>
class ReadAccessor<VectorLikeType>
: public ReadAccessorVector< VectorLikeType >
{
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 vector like object //////////////////////
/// ReadAccessor implementation class for vector types
template<class T>
template<sofa::type::trait::is_vector T>
class ReadAccessorVector
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ class WriteAccessor<FixedArrayLikeType, std::enable_if_t<sofa::type::trait::is_f
WriteAccessor(container_type& c) : Inherit(c) {}
};

template<class VectorLikeType>
class WriteAccessor<VectorLikeType,
std::enable_if_t<sofa::type::trait::is_vector<VectorLikeType>::value>>
template<sofa::type::trait::is_vector VectorLikeType>
class WriteAccessor<VectorLikeType>
: public WriteAccessorVector< VectorLikeType >
{
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 vector types
template<class T>
template<sofa::type::trait::is_vector T>
class WriteAccessorVector
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ class WriteOnlyAccessor : public WriteAccessor<T, Enable>
explicit WriteOnlyAccessor(container_type& container) : WriteAccessor<T, Enable>(container) {}
};

template<class VectorLikeType>
class WriteOnlyAccessor<VectorLikeType,
std::enable_if_t<sofa::type::trait::is_vector<VectorLikeType>::value> >
template<sofa::type::trait::is_vector VectorLikeType>
class WriteOnlyAccessor<VectorLikeType>
: public WriteAccessorVector< VectorLikeType >
{
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ class CompressedRowSparseMatrixMechanical final // final is used to allow the co

/// equal result = this * v
/// @warning The block sizes must be compatible ie v.size() must be a multiple of block size.
template< typename V1, typename V2, std::enable_if_t<sofa::type::trait::is_vector<V1>::value && sofa::type::trait::is_vector<V2>::value, int> = 0 >
template< sofa::type::trait::is_vector V1, sofa::type::trait::is_vector V2>
void mul( V2& result, const V1& v ) const
{
this-> template tmul< Real, V2, V1 >(result, v);
Expand Down
36 changes: 7 additions & 29 deletions Sofa/framework/Type/src/sofa/type/trait/is_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,16 @@ namespace sofa::type::trait

/// Detect if a type T has iterator/const iterator function, operator[](size_t) and is dynamically resizable (resize function)
template<typename T>
struct is_vector
concept is_vector = requires(std::remove_cv_t<T> t, const std::remove_cv_t<T> ct)
{
typedef typename std::remove_const<T>::type test_type;
{t.begin()} -> std::convertible_to<typename T::iterator>;
{t.end()} -> std::convertible_to<typename T::iterator>;

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(pt->resize(1)) * = nullptr,
typename A::iterator * = nullptr,
typename A::const_iterator * = nullptr,
typename A::value_type * = nullptr) {
{ct.begin()} -> std::convertible_to<typename T::const_iterator>;
{ct.end()} -> std::convertible_to<typename T::const_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;
}

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>;
t.resize(1);
};

}

0 comments on commit d83d7ed

Please sign in to comment.