diff --git a/td/mtproto/Handshake.cpp b/td/mtproto/Handshake.cpp index 49e766e5f..62e4679c6 100644 --- a/td/mtproto/Handshake.cpp +++ b/td/mtproto/Handshake.cpp @@ -10,6 +10,7 @@ #include "td/mtproto/mtproto_api.h" +#include "td/utils/as.h" #include "td/utils/buffer.h" #include "td/utils/crypto.h" #include "td/utils/format.h" diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index 4845eb47b..a4dce7403 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -16,6 +16,7 @@ #include "td/mtproto/TcpTransport.h" #include "td/mtproto/Transport.h" +#include "td/utils/as.h" #include "td/utils/format.h" #include "td/utils/Gzip.h" #include "td/utils/logging.h" diff --git a/td/mtproto/TcpTransport.cpp b/td/mtproto/TcpTransport.cpp index 81a29e743..bf09ea6e9 100644 --- a/td/mtproto/TcpTransport.cpp +++ b/td/mtproto/TcpTransport.cpp @@ -6,6 +6,7 @@ // #include "td/mtproto/TcpTransport.h" +#include "td/utils/as.h" #include "td/utils/logging.h" #include "td/utils/Random.h" #include "td/utils/Slice.h" diff --git a/td/mtproto/Transport.cpp b/td/mtproto/Transport.cpp index 4af65de10..589151426 100644 --- a/td/mtproto/Transport.cpp +++ b/td/mtproto/Transport.cpp @@ -9,6 +9,7 @@ #include "td/mtproto/AuthKey.h" #include "td/mtproto/crypto.h" +#include "td/utils/as.h" #include "td/utils/crypto.h" #include "td/utils/format.h" #include "td/utils/logging.h" diff --git a/td/mtproto/crypto.cpp b/td/mtproto/crypto.cpp index 056712214..1aa34a2d4 100644 --- a/td/mtproto/crypto.cpp +++ b/td/mtproto/crypto.cpp @@ -8,6 +8,7 @@ #include "td/mtproto/mtproto_api.h" +#include "td/utils/as.h" #include "td/utils/crypto.h" #include "td/utils/int_types.h" // for UInt256, UInt128, etc #include "td/utils/logging.h" diff --git a/td/telegram/SecretChatActor.cpp b/td/telegram/SecretChatActor.cpp index ce4784026..e971f38f9 100644 --- a/td/telegram/SecretChatActor.cpp +++ b/td/telegram/SecretChatActor.cpp @@ -19,6 +19,7 @@ #include "td/actor/MultiPromise.h" +#include "td/utils/as.h" #include "td/utils/crypto.h" #include "td/utils/format.h" #include "td/utils/logging.h" diff --git a/td/telegram/SecureStorage.cpp b/td/telegram/SecureStorage.cpp index f0b441693..cc9621565 100644 --- a/td/telegram/SecureStorage.cpp +++ b/td/telegram/SecureStorage.cpp @@ -6,6 +6,7 @@ // #include "td/telegram/SecureStorage.h" +#include "td/utils/as.h" #include "td/utils/format.h" #include "td/utils/logging.h" #include "td/utils/misc.h" diff --git a/td/telegram/files/FileDownloader.cpp b/td/telegram/files/FileDownloader.cpp index ff64de7ee..99ad9f744 100644 --- a/td/telegram/files/FileDownloader.cpp +++ b/td/telegram/files/FileDownloader.cpp @@ -13,6 +13,7 @@ #include "td/telegram/net/DcId.h" #include "td/telegram/UniqueId.h" +#include "td/utils/as.h" #include "td/utils/buffer.h" #include "td/utils/common.h" #include "td/utils/crypto.h" diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index c4d56f594..cb457741f 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -13,6 +13,7 @@ #include "td/telegram/net/DcId.h" #include "td/telegram/SecureStorage.h" +#include "td/utils/as.h" #include "td/utils/buffer.h" #include "td/utils/common.h" #include "td/utils/crypto.h" diff --git a/td/telegram/net/NetQuery.cpp b/td/telegram/net/NetQuery.cpp index d555e6121..195044376 100644 --- a/td/telegram/net/NetQuery.cpp +++ b/td/telegram/net/NetQuery.cpp @@ -8,6 +8,7 @@ #include "td/telegram/Global.h" +#include "td/utils/as.h" #include "td/utils/misc.h" #include "td/utils/Slice.h" diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index d477647d1..a933cd101 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -133,6 +133,7 @@ set(TDUTILS_SOURCE td/utils/port/detail/WineventPoll.h td/utils/AesCtrByteFlow.h + td/utils/as.h td/utils/base64.h td/utils/benchmark.h td/utils/BigNum.h diff --git a/tdutils/td/utils/as.h b/tdutils/td/utils/as.h new file mode 100644 index 000000000..cf8f17cb4 --- /dev/null +++ b/tdutils/td/utils/as.h @@ -0,0 +1,64 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// +// 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 + +#include + +namespace td { + +template +class As { + public: + As(void *ptr) : ptr_(ptr) { + } + As(As &&) = default; + As &operator=(const As &new_value) && { + memcpy(ptr_, new_value.ptr_, sizeof(T)); + return *this; + } + As &operator=(const T new_value) && { + memcpy(ptr_, &new_value, sizeof(T)); + return *this; + } + + operator T() const { + T res; + memcpy(&res, ptr_, sizeof(T)); + return res; + } + + private: + void *ptr_; +}; + +template +class ConstAs { + public: + ConstAs(const void *ptr) : ptr_(ptr) { + } + + operator T() const { + T res; + memcpy(&res, ptr_, sizeof(T)); + return res; + } + + private: + const void *ptr_; +}; + +template +As as(FromT *from) { + return As(from); +} + +template +const ConstAs as(const FromT *from) { + return ConstAs(from); +} + +} // namespace td diff --git a/tdutils/td/utils/common.h b/tdutils/td/utils/common.h index a9f7630c1..ba520d94a 100644 --- a/tdutils/td/utils/common.h +++ b/tdutils/td/utils/common.h @@ -45,7 +45,6 @@ #include "td/utils/int_types.h" #include "td/utils/unique_ptr.h" -#include // temporary for std::memcpy #include #include @@ -108,55 +107,4 @@ struct Auto { } }; -template -class As { - public: - As(void *ptr) : ptr_(ptr) { - } - As(As &&) = default; - As &operator=(const As &new_value) && { - memcpy(ptr_, new_value.ptr_, sizeof(T)); - return *this; - } - As &operator=(const T new_value) && { - memcpy(ptr_, &new_value, sizeof(T)); - return *this; - } - - operator T() const { - T res; - memcpy(&res, ptr_, sizeof(T)); - return res; - } - - private: - void *ptr_; -}; - -template -class ConstAs { - public: - ConstAs(const void *ptr) : ptr_(ptr) { - } - - operator T() const { - T res; - memcpy(&res, ptr_, sizeof(T)); - return res; - } - - private: - const void *ptr_; -}; - -template -As as(FromT *from) { - return As(from); -} - -template -const ConstAs as(const FromT *from) { - return ConstAs(from); -} - } // namespace td diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index 66b06649d..5f092074f 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -4,6 +4,7 @@ // 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) // +#include "td/utils/as.h" #include "td/utils/base64.h" #include "td/utils/BigNum.h" #include "td/utils/bits.h"