Skip to content

Commit

Permalink
Check in str_arg_char<StrT> if StrT has size() member
Browse files Browse the repository at this point in the history
Before it was checked StrT has length() member.

This allows other containers (e.g. std::vector) to be used as input.
  • Loading branch information
rmisev committed Nov 7, 2024
1 parent 35d2441 commit a0c32ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 8 additions & 8 deletions include/upa/str_arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ namespace detail {
template<class>
auto test_data(long) -> void;

// test class T has length() member
// test class T has size() member
template<class T>
auto test_length(int) -> decltype(std::declval<T>().length());
auto test_size(int) -> decltype(std::declval<T>().size());
template<class>
auto test_length(long) -> void;
auto test_size(long) -> void;

// T::data() return type (void - if no such member)
template<class T>
using data_member_t = decltype(detail::test_data<T>(0));

// T::length() return type (void - if no such member)
// T::size() return type (void - if no such member)
template<class T>
using length_member_t = decltype(detail::test_length<T>(0));
using size_member_t = decltype(detail::test_size<T>(0));
} // namespace detail


Expand All @@ -172,16 +172,16 @@ struct str_arg_char<CharT*> : std::remove_cv<CharT> {
}
};

// String that has data() and length() members
// String that has data() and size() members
template<class StrT>
struct str_arg_char<StrT> : std::enable_if<
std::is_pointer<detail::data_member_t<StrT>>::value &&
is_size_type<detail::length_member_t<StrT>>::value,
is_size_type<detail::size_member_t<StrT>>::value,
remove_cvptr_t<detail::data_member_t<StrT>>> {

template <class STR, typename T = typename STR::value_type>
static str_arg<T> to_str_arg(const STR& str) {
return { str.data(), str.length() };
return { str.data(), str.size() };
}
};

Expand Down
6 changes: 6 additions & 0 deletions test/test-str_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "upa/str_arg.h"
//#include "doctest-main.h"
#include <array>
#include <vector>


// Function to test
Expand Down Expand Up @@ -86,6 +88,10 @@ inline void test_char() {
procfn(upa::str_arg<CharT>{cptr, cptr + N});
procfn(upa::str_arg<CharT>{vptr, vptr + N});

// has data() and size() members
procfn(std::array<CharT, N>{ '1', '2', '3'});
procfn(std::vector<CharT>{ '1', '2', '3'});

// std::basic_string
const std::basic_string<CharT> str(arr);
procfn(str);
Expand Down

0 comments on commit a0c32ab

Please sign in to comment.