Move as from common.h to as.h.

GitOrigin-RevId: 3ecc4b4c4ff6c9b070d30cf017e22a8acce0af09
This commit is contained in:
levlam 2018-12-20 00:57:56 +03:00
parent 22eb4e1cb2
commit 3850a4da7f
14 changed files with 76 additions and 52 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -8,6 +8,7 @@
#include "td/telegram/Global.h"
#include "td/utils/as.h"
#include "td/utils/misc.h"
#include "td/utils/Slice.h"

View File

@ -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

64
tdutils/td/utils/as.h Normal file
View File

@ -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 <cstring>
namespace td {
template <class T>
class As {
public:
As(void *ptr) : ptr_(ptr) {
}
As(As &&) = default;
As<T> &operator=(const As &new_value) && {
memcpy(ptr_, new_value.ptr_, sizeof(T));
return *this;
}
As<T> &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 T>
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 <class ToT, class FromT>
As<ToT> as(FromT *from) {
return As<ToT>(from);
}
template <class ToT, class FromT>
const ConstAs<ToT> as(const FromT *from) {
return ConstAs<ToT>(from);
}
} // namespace td

View File

@ -45,7 +45,6 @@
#include "td/utils/int_types.h"
#include "td/utils/unique_ptr.h"
#include <cstring> // temporary for std::memcpy
#include <string>
#include <vector>
@ -108,55 +107,4 @@ struct Auto {
}
};
template <class T>
class As {
public:
As(void *ptr) : ptr_(ptr) {
}
As(As &&) = default;
As<T> &operator=(const As &new_value) && {
memcpy(ptr_, new_value.ptr_, sizeof(T));
return *this;
}
As<T> &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 T>
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 <class ToT, class FromT>
As<ToT> as(FromT *from) {
return As<ToT>(from);
}
template <class ToT, class FromT>
const ConstAs<ToT> as(const FromT *from) {
return ConstAs<ToT>(from);
}
} // namespace td

View File

@ -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"