2018-12-19 22:57:56 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-19 22:57:56 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2020-11-06 15:09:47 +01:00
|
|
|
#include "td/utils/type_traits.h"
|
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
#include <cstring>
|
2019-01-18 14:24:26 +01:00
|
|
|
#include <type_traits>
|
2018-12-19 22:57:56 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-12-19 23:46:55 +01:00
|
|
|
namespace detail {
|
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
template <class T>
|
|
|
|
class As {
|
|
|
|
public:
|
2018-12-19 23:46:55 +01:00
|
|
|
explicit As(void *ptr) : ptr_(ptr) {
|
2018-12-19 22:57:56 +01:00
|
|
|
}
|
2018-12-19 23:46:55 +01:00
|
|
|
|
|
|
|
As(const As &new_value) = delete;
|
|
|
|
As &operator=(const As &) = delete;
|
2018-12-19 22:57:56 +01:00
|
|
|
As(As &&) = default;
|
2023-02-08 13:28:36 +01:00
|
|
|
As &operator=(As &&new_value) && noexcept {
|
2018-12-19 23:46:55 +01:00
|
|
|
std::memcpy(ptr_, new_value.ptr_, sizeof(T));
|
2018-12-19 22:57:56 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2018-12-19 23:46:55 +01:00
|
|
|
~As() = default;
|
|
|
|
|
2020-07-14 16:14:35 +02:00
|
|
|
As &operator=(const T &new_value) && {
|
2018-12-19 23:46:55 +01:00
|
|
|
std::memcpy(ptr_, &new_value, sizeof(T));
|
2018-12-19 22:57:56 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T() const {
|
|
|
|
T res;
|
2018-12-19 23:46:55 +01:00
|
|
|
std::memcpy(&res, ptr_, sizeof(T));
|
2018-12-19 22:57:56 +01:00
|
|
|
return res;
|
|
|
|
}
|
2019-01-29 16:16:52 +01:00
|
|
|
bool operator==(const As &other) const {
|
|
|
|
return this->operator T() == other.operator T();
|
|
|
|
}
|
2018-12-19 22:57:56 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
void *ptr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class ConstAs {
|
|
|
|
public:
|
2018-12-19 23:46:55 +01:00
|
|
|
explicit ConstAs(const void *ptr) : ptr_(ptr) {
|
2018-12-19 22:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
operator T() const {
|
|
|
|
T res;
|
2018-12-19 23:46:55 +01:00
|
|
|
std::memcpy(&res, ptr_, sizeof(T));
|
2018-12-19 22:57:56 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const void *ptr_;
|
|
|
|
};
|
|
|
|
|
2018-12-19 23:46:55 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
2019-01-18 14:24:26 +01:00
|
|
|
template <class ToT, class FromT,
|
2019-02-13 00:45:08 +01:00
|
|
|
std::enable_if_t<TD_IS_TRIVIALLY_COPYABLE(ToT) && TD_IS_TRIVIALLY_COPYABLE(FromT), int> = 0>
|
2018-12-19 23:46:55 +01:00
|
|
|
detail::As<ToT> as(FromT *from) {
|
|
|
|
return detail::As<ToT>(from);
|
2018-12-19 22:57:56 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:24:26 +01:00
|
|
|
template <class ToT, class FromT,
|
2019-02-13 00:45:08 +01:00
|
|
|
std::enable_if_t<TD_IS_TRIVIALLY_COPYABLE(ToT) && TD_IS_TRIVIALLY_COPYABLE(FromT), int> = 0>
|
2021-06-02 04:21:15 +02:00
|
|
|
detail::ConstAs<ToT> as(const FromT *from) {
|
2018-12-19 23:46:55 +01:00
|
|
|
return detail::ConstAs<ToT>(from);
|
2018-12-19 22:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|