net/SocketAddress: add CastTo()

This commit is contained in:
Max Kellermann
2020-11-30 21:35:43 +01:00
committed by Max Kellermann
parent 32ce9ce919
commit 273771ffec
9 changed files with 46 additions and 36 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2012-2020 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -82,6 +82,19 @@ public:
return address;
}
/**
* Cast the "sockaddr" pointer to a different address type,
* e.g. "sockaddr_in". This is only legal after checking
* !IsNull() and GetFamily().
*/
template<typename T>
constexpr const T &CastTo() const noexcept {
/* cast through void to work around the bogus
alignment warning */
const void *q = reinterpret_cast<const void *>(address);
return *reinterpret_cast<const T *>(q);
}
constexpr size_type GetSize() const noexcept {
return size;
}