Add CppSupport with backported utilities (#4525)

This commit is contained in:
Alexander Shabalin
2020-11-19 16:05:21 +03:00
committed by Stanislav Erokhin
parent 0caf0b5e11
commit e7ab525213
5 changed files with 79 additions and 41 deletions
@@ -0,0 +1,51 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
#ifndef RUNTIME_CPP_SUPPORT_H
#define RUNTIME_CPP_SUPPORT_H
#include <type_traits>
#include <memory>
// A collection of backported utilities from future C++ versions.
namespace kotlin {
namespace std_support {
////////////////////////// C++14 //////////////////////////
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <typename T>
using make_unsigned_t = typename std::make_unsigned<T>::type;
////////////////////////// C++17 //////////////////////////
template <typename T>
constexpr bool is_trivially_destructible_v = std::is_trivially_destructible<T>::value;
template <typename T>
constexpr bool is_nothrow_default_constructible_v = std::is_nothrow_default_constructible<T>::value;
template <typename T>
constexpr bool is_nothrow_destructible_v = std::is_nothrow_destructible<T>::value;
template <typename T>
constexpr bool is_copy_constructible_v = std::is_copy_constructible<T>::value;
template <typename T>
constexpr bool is_copy_assignable_v = std::is_copy_assignable<T>::value;
template <typename T>
constexpr bool is_move_constructible_v = std::is_move_constructible<T>::value;
template <typename T>
constexpr bool is_move_assignable_v = std::is_move_assignable<T>::value;
template <typename T>
constexpr bool is_nothrow_move_constructible_v = std::is_nothrow_move_constructible<T>::value;
template <typename T>
constexpr bool is_nothrow_move_assignable_v = std::is_nothrow_move_assignable<T>::value;
} // namespace std_support
} // namespace kotlin
#endif // RUNTIME_CPP_SUPPORT_H
@@ -8,6 +8,7 @@
#include <type_traits>
#include "CppSupport.hpp"
#include "Memory.h"
// TODO: Generalize for uses outside this file.
@@ -37,8 +38,8 @@ class KRefSharedHolder {
ForeignRefContext context_;
};
static_assert(std::is_trivially_destructible<KRefSharedHolder>::value,
"KRefSharedHolder destructor is not guaranteed to be called.");
static_assert(
kotlin::std_support::is_trivially_destructible_v<KRefSharedHolder>, "KRefSharedHolder destructor is not guaranteed to be called.");
class BackRefFromAssociatedObject {
public:
@@ -66,7 +67,8 @@ class BackRefFromAssociatedObject {
volatile int refCount;
};
static_assert(std::is_trivially_destructible<BackRefFromAssociatedObject>::value,
"BackRefFromAssociatedObject destructor is not guaranteed to be called.");
static_assert(
kotlin::std_support::is_trivially_destructible_v<BackRefFromAssociatedObject>,
"BackRefFromAssociatedObject destructor is not guaranteed to be called.");
#endif // RUNTIME_MEMORYSHAREDREFS_HPP
@@ -21,6 +21,7 @@
#include <limits>
#include <type_traits>
#include "CppSupport.hpp"
#include "KAssert.h"
#include "Exceptions.h"
#include "Memory.h"
@@ -50,7 +51,7 @@ OBJ_GETTER0(Kotlin_native_internal_undefined) {
}
void* Kotlin_interop_malloc(KLong size, KInt align) {
if (size < 0 || static_cast<std::make_unsigned<decltype(size)>::type>(size) > std::numeric_limits<size_t>::max()) {
if (size < 0 || static_cast<kotlin::std_support::make_unsigned_t<decltype(size)>>(size) > std::numeric_limits<size_t>::max()) {
return nullptr;
}
RuntimeAssert(align > 0, "Unsupported alignment");
@@ -9,6 +9,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "CppSupport.hpp"
#include "Types.h"
#include "Utils.hpp"
@@ -20,8 +21,7 @@ public:
explicit ScopedStrictMockFunction(Mock** globalMockLocation) : globalMockLocation_(globalMockLocation) {
RuntimeCheck(globalMockLocation != nullptr, "ScopedStrictMockFunction needs non-null global mock location");
RuntimeCheck(*globalMockLocation == nullptr, "ScopedStrictMockFunction needs null global mock");
// TODO: Use make_unique when sysroots on Linux get updated.
mock_ = std::unique_ptr<Mock>(new Mock());
mock_ = kotlin::std_support::make_unique<Mock>();
*globalMockLocation_ = mock_.get();
}
@@ -5,40 +5,24 @@
#include "Utils.hpp"
#include <type_traits>
#include "gtest/gtest.h"
namespace {
#include "CppSupport.hpp"
// TODO: use std variants when we move to C++17
template <typename T>
constexpr bool is_nothrow_default_constructible_v = std::is_nothrow_default_constructible<T>::value;
template <typename T>
constexpr bool is_nothrow_destructible_v = std::is_nothrow_destructible<T>::value;
template <typename T>
constexpr bool is_copy_constructible_v = std::is_copy_constructible<T>::value;
template <typename T>
constexpr bool is_copy_assignable_v = std::is_copy_assignable<T>::value;
template <typename T>
constexpr bool is_move_constructible_v = std::is_move_constructible<T>::value;
template <typename T>
constexpr bool is_move_assignable_v = std::is_move_assignable<T>::value;
template <typename T>
constexpr bool is_nothrow_move_constructible_v = std::is_nothrow_move_constructible<T>::value;
template <typename T>
constexpr bool is_nothrow_move_assignable_v = std::is_nothrow_move_assignable<T>::value;
using namespace kotlin;
namespace {
struct A {
int field;
};
class MoveOnlyImpl : private kotlin::MoveOnly {
class MoveOnlyImpl : private MoveOnly {
public:
A a;
};
class PinnedImpl : private kotlin::Pinned {
class PinnedImpl : private Pinned {
public:
A a;
};
@@ -46,21 +30,21 @@ public:
} // namespace
TEST(UtilsTest, MoveOnlyImpl) {
static_assert(is_nothrow_default_constructible_v<MoveOnlyImpl>, "Must be nothrow default constructible");
static_assert(is_nothrow_destructible_v<MoveOnlyImpl>, "Must be nothrow destructible");
static_assert(!is_copy_constructible_v<MoveOnlyImpl>, "Must not be copy constructible");
static_assert(!is_copy_assignable_v<MoveOnlyImpl>, "Must not be copy assignable");
static_assert(is_nothrow_move_constructible_v<MoveOnlyImpl>, "Must be nothrow move constructible");
static_assert(is_nothrow_move_assignable_v<MoveOnlyImpl>, "Must be nothrow move assignable");
static_assert(std_support::is_nothrow_default_constructible_v<MoveOnlyImpl>, "Must be nothrow default constructible");
static_assert(std_support::is_nothrow_destructible_v<MoveOnlyImpl>, "Must be nothrow destructible");
static_assert(!std_support::is_copy_constructible_v<MoveOnlyImpl>, "Must not be copy constructible");
static_assert(!std_support::is_copy_assignable_v<MoveOnlyImpl>, "Must not be copy assignable");
static_assert(std_support::is_nothrow_move_constructible_v<MoveOnlyImpl>, "Must be nothrow move constructible");
static_assert(std_support::is_nothrow_move_assignable_v<MoveOnlyImpl>, "Must be nothrow move assignable");
static_assert(sizeof(MoveOnlyImpl) == sizeof(A), "Must not increase size");
}
TEST(UtilsTest, PinnedImpl) {
static_assert(is_nothrow_default_constructible_v<PinnedImpl>, "Must be nothrow default constructible");
static_assert(is_nothrow_destructible_v<PinnedImpl>, "Must be nothrow destructible");
static_assert(!is_copy_constructible_v<PinnedImpl>, "Must not be copy constructible");
static_assert(!is_copy_assignable_v<PinnedImpl>, "Must not be copy assignable");
static_assert(!is_move_constructible_v<PinnedImpl>, "Must not be move constructible");
static_assert(!is_move_assignable_v<PinnedImpl>, "Must not be move assignable");
static_assert(std_support::is_nothrow_default_constructible_v<PinnedImpl>, "Must be nothrow default constructible");
static_assert(std_support::is_nothrow_destructible_v<PinnedImpl>, "Must be nothrow destructible");
static_assert(!std_support::is_copy_constructible_v<PinnedImpl>, "Must not be copy constructible");
static_assert(!std_support::is_copy_assignable_v<PinnedImpl>, "Must not be copy assignable");
static_assert(!std_support::is_move_constructible_v<PinnedImpl>, "Must not be move constructible");
static_assert(!std_support::is_move_assignable_v<PinnedImpl>, "Must not be move assignable");
static_assert(sizeof(PinnedImpl) == sizeof(A), "Must not increase size");
}