2022-02-22 20:33:45 +08:00
|
|
|
// Copyright 2022 Memgraph Ltd.
|
2021-10-03 18:07:04 +08:00
|
|
|
//
|
|
|
|
// Use of this software is governed by the Business Source License
|
|
|
|
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
|
|
|
|
// License, and you may not use this file except in compliance with the Business Source License.
|
|
|
|
//
|
|
|
|
// As of the Change Date specified in that file, in accordance with
|
|
|
|
// the Business Source License, use of this software will be governed
|
|
|
|
// by the Apache License, Version 2.0, included in the file
|
|
|
|
// licenses/APL.txt.
|
|
|
|
|
2018-07-05 03:32:07 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
namespace memgraph::utils {
|
2018-07-05 03:32:07 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
constexpr typename std::underlying_type<T>::type UnderlyingCast(T e) {
|
|
|
|
return static_cast<typename std::underlying_type<T>::type>(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* uint to int conversion in C++ is a bit tricky. Take a look here
|
|
|
|
* https://stackoverflow.com/questions/14623266/why-cant-i-reinterpret-cast-uint-to-int
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* @tparam TDest Returned datatype.
|
|
|
|
* @tparam TSrc Input datatype.
|
|
|
|
*
|
|
|
|
* @return "copy casted" value.
|
|
|
|
*/
|
|
|
|
template <typename TDest, typename TSrc>
|
|
|
|
TDest MemcpyCast(TSrc src) {
|
|
|
|
TDest dest;
|
2021-02-18 22:32:43 +08:00
|
|
|
static_assert(sizeof(dest) == sizeof(src), "MemcpyCast expects source and destination to be of same size");
|
|
|
|
static_assert(std::is_arithmetic<TSrc>::value, "MemcpyCast expects source is an arithmetic type");
|
|
|
|
static_assert(std::is_arithmetic<TDest>::value, "MemcypCast expects destination is an arithmetic type");
|
2018-07-05 03:32:07 +08:00
|
|
|
std::memcpy(&dest, &src, sizeof(src));
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:33:45 +08:00
|
|
|
} // namespace memgraph::utils
|