mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
horrible_cast
This commit is contained in:
parent
7c063f8c17
commit
74f080361e
@ -505,8 +505,8 @@ static ipc::buff_t recv(ipc::handle_t h, std::size_t tm) {
|
||||
if (buf != nullptr) {
|
||||
if (recycled) {
|
||||
return ipc::buff_t{buf, msg_size, [](void* pmid, std::size_t size) {
|
||||
release_storage(reinterpret_cast<ipc::storage_id_t>(pmid) - 1, size);
|
||||
}, reinterpret_cast<void*>(buf_id + 1)};
|
||||
release_storage(ipc::detail::horrible_cast<ipc::storage_id_t>(pmid) - 1, size);
|
||||
}, ipc::detail::horrible_cast<void*>(buf_id + 1)};
|
||||
} else {
|
||||
return ipc::buff_t{buf, msg_size}; // no recycle
|
||||
}
|
||||
|
||||
@ -111,5 +111,17 @@ constexpr const T& (min)(const T& a, const T& b) {
|
||||
|
||||
#endif/*__cplusplus < 201703L*/
|
||||
|
||||
template <typename T, typename U>
|
||||
auto horrible_cast(U rhs) noexcept
|
||||
-> typename std::enable_if<std::is_trivially_copyable<T>::value
|
||||
&& std::is_trivially_copyable<U>::value, T>::type {
|
||||
union {
|
||||
T t;
|
||||
U u;
|
||||
} r = {};
|
||||
r.u = rhs;
|
||||
return r.t;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace ipc
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user