Skip to content

Commit

Permalink
Constrain the Property object at compile-time
Browse files Browse the repository at this point in the history
The property struct/class requires a public member function signature
`apply(hid_t) const`. Describe the constrain in the C++20 concepts
syntax. The primarily goal is to make compiler errors more readable for
average users. Average users are those who do not understand SFINAE.
  • Loading branch information
antonysigma committed Jul 20, 2023
1 parent ba65fd4 commit 24cee49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions include/highfive/H5PropertyList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ class PropertyListBase: public Object {
friend T details::get_plist(const U&, hid_t (*f)(hid_t));
};

/// \interface PropertyInterface
/// \brief HDF5 file property object
///
/// A property is an object which is expected to have a method with the
/// following signature `void apply(hid_t hid) const`
///
/// \sa Instructions to document C++20 concepts with Doxygen: https://github.com/doxygen/doxygen/issues/2732#issuecomment-509629967
///
/// \cond
#if __cplusplus >= 202002L
template<typename P>
concept PropertyInterface = requires(P p, const hid_t hid)
{
{ p.apply(hid) };
};

#else
#define PropertyInterface typename
#endif
/// \endcond

///
/// \brief HDF5 property Lists
///
Expand All @@ -149,8 +170,8 @@ class PropertyList: public PropertyListBase {
/// Add a property to this property list.
/// A property is an object which is expected to have a method with the
/// following signature void apply(hid_t hid) const
///
template <typename P>
/// \tparam PropertyInterface
template <PropertyInterface P>
void add(const P& property);

///
Expand Down
2 changes: 1 addition & 1 deletion include/highfive/bits/H5PropertyList_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ inline void PropertyList<T>::_initializeIfNeeded() {
}

template <PropertyType T>
template <typename P>
template <PropertyInterface P>
inline void PropertyList<T>::add(const P& property) {
_initializeIfNeeded();
property.apply(_hid);
Expand Down

0 comments on commit 24cee49

Please sign in to comment.