Skip to content

Commit

Permalink
[wpinet] uv::AddrToName: Add StringAssignable constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Jan 3, 2025
1 parent 9f1ba3b commit 5f50801
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions wpinet/src/main/native/include/wpinet/uv/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@

namespace wpi::uv {

namespace detail {
template <typename T>
concept StringAssignable = requires(T a, const char* p) {
{ a.assign(p, p) };
};
} // namespace detail

/**
* Convert a binary structure containing an IPv4 address to a string.
* @param addr Binary structure
* @param ip Output string (any type that has `assign(char*, char*)`)
* @param port Output port number
* @return Error (same as `uv_ip4_name()`).
*/
template <typename T>
template <detail::StringAssignable T>
int AddrToName(const sockaddr_in& addr, T* ip, unsigned int* port) {
char name[128];
int err = uv_ip4_name(&addr, name, 128);
Expand All @@ -43,7 +50,7 @@ int AddrToName(const sockaddr_in& addr, T* ip, unsigned int* port) {
* @param port Output port number
* @return Error (same as `uv_ip6_name()`).
*/
template <typename T>
template <detail::StringAssignable T>
int AddrToName(const sockaddr_in6& addr, T* ip, unsigned int* port) {
char name[128];
int err = uv_ip6_name(&addr, name, 128);
Expand All @@ -63,7 +70,7 @@ int AddrToName(const sockaddr_in6& addr, T* ip, unsigned int* port) {
* @param port Output port number
* @return Error (same as `uv_ip6_name()`).
*/
template <typename T>
template <detail::StringAssignable T>
int AddrToName(const sockaddr_storage& addr, T* ip, unsigned int* port) {
if (addr.ss_family == AF_INET) {
return AddrToName(reinterpret_cast<const sockaddr_in&>(addr), ip, port);
Expand All @@ -82,7 +89,7 @@ int AddrToName(const sockaddr_storage& addr, T* ip, unsigned int* port) {
* @param ip Output string (any type that has `assign(char*, char*)`)
* @return Error (same as `uv_inet_ntop()`).
*/
template <typename T>
template <detail::StringAssignable T>
int AddrToName(const in_addr& addr, T* ip) {
char name[128];
int err = uv_inet_ntop(AF_INET, &addr, name, 128);
Expand All @@ -100,7 +107,7 @@ int AddrToName(const in_addr& addr, T* ip) {
* @param ip Output string (any type that has `assign(char*, char*)`)
* @return Error (same as `uv_inet_ntop()`).
*/
template <typename T>
template <detail::StringAssignable T>
int AddrToName(const in6_addr& addr, T* ip) {
char name[128];
int err = uv_inet_ntop(AF_INET6, &addr, name, 128);
Expand Down

0 comments on commit 5f50801

Please sign in to comment.