net/SocketAddress: add CastTo()
This commit is contained in:

committed by
Max Kellermann

parent
32ce9ce919
commit
273771ffec
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user