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 <type_traits>
#include "CppSupport.hpp"
#include "Memory.h" #include "Memory.h"
// TODO: Generalize for uses outside this file. // TODO: Generalize for uses outside this file.
@@ -37,8 +38,8 @@ class KRefSharedHolder {
ForeignRefContext context_; ForeignRefContext context_;
}; };
static_assert(std::is_trivially_destructible<KRefSharedHolder>::value, static_assert(
"KRefSharedHolder destructor is not guaranteed to be called."); kotlin::std_support::is_trivially_destructible_v<KRefSharedHolder>, "KRefSharedHolder destructor is not guaranteed to be called.");
class BackRefFromAssociatedObject { class BackRefFromAssociatedObject {
public: public:
@@ -66,7 +67,8 @@ class BackRefFromAssociatedObject {
volatile int refCount; volatile int refCount;
}; };
static_assert(std::is_trivially_destructible<BackRefFromAssociatedObject>::value, static_assert(
"BackRefFromAssociatedObject destructor is not guaranteed to be called."); kotlin::std_support::is_trivially_destructible_v<BackRefFromAssociatedObject>,
"BackRefFromAssociatedObject destructor is not guaranteed to be called.");
#endif // RUNTIME_MEMORYSHAREDREFS_HPP #endif // RUNTIME_MEMORYSHAREDREFS_HPP
@@ -21,6 +21,7 @@
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
#include "CppSupport.hpp"
#include "KAssert.h" #include "KAssert.h"
#include "Exceptions.h" #include "Exceptions.h"
#include "Memory.h" #include "Memory.h"
@@ -50,7 +51,7 @@ OBJ_GETTER0(Kotlin_native_internal_undefined) {
} }
void* Kotlin_interop_malloc(KLong size, KInt align) { 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; return nullptr;
} }
RuntimeAssert(align > 0, "Unsupported alignment"); RuntimeAssert(align > 0, "Unsupported alignment");
@@ -9,6 +9,7 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "CppSupport.hpp"
#include "Types.h" #include "Types.h"
#include "Utils.hpp" #include "Utils.hpp"
@@ -20,8 +21,7 @@ public:
explicit ScopedStrictMockFunction(Mock** globalMockLocation) : globalMockLocation_(globalMockLocation) { explicit ScopedStrictMockFunction(Mock** globalMockLocation) : globalMockLocation_(globalMockLocation) {
RuntimeCheck(globalMockLocation != nullptr, "ScopedStrictMockFunction needs non-null global mock location"); RuntimeCheck(globalMockLocation != nullptr, "ScopedStrictMockFunction needs non-null global mock location");
RuntimeCheck(*globalMockLocation == nullptr, "ScopedStrictMockFunction needs null global mock"); RuntimeCheck(*globalMockLocation == nullptr, "ScopedStrictMockFunction needs null global mock");
// TODO: Use make_unique when sysroots on Linux get updated. mock_ = kotlin::std_support::make_unique<Mock>();
mock_ = std::unique_ptr<Mock>(new Mock());
*globalMockLocation_ = mock_.get(); *globalMockLocation_ = mock_.get();
} }
@@ -5,40 +5,24 @@
#include "Utils.hpp" #include "Utils.hpp"
#include <type_traits>
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace { #include "CppSupport.hpp"
// TODO: use std variants when we move to C++17 using namespace kotlin;
template <typename T>
constexpr bool is_nothrow_default_constructible_v = std::is_nothrow_default_constructible<T>::value; namespace {
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;
struct A { struct A {
int field; int field;
}; };
class MoveOnlyImpl : private kotlin::MoveOnly { class MoveOnlyImpl : private MoveOnly {
public: public:
A a; A a;
}; };
class PinnedImpl : private kotlin::Pinned { class PinnedImpl : private Pinned {
public: public:
A a; A a;
}; };
@@ -46,21 +30,21 @@ public:
} // namespace } // namespace
TEST(UtilsTest, MoveOnlyImpl) { TEST(UtilsTest, MoveOnlyImpl) {
static_assert(is_nothrow_default_constructible_v<MoveOnlyImpl>, "Must be nothrow default constructible"); static_assert(std_support::is_nothrow_default_constructible_v<MoveOnlyImpl>, "Must be nothrow default constructible");
static_assert(is_nothrow_destructible_v<MoveOnlyImpl>, "Must be nothrow destructible"); static_assert(std_support::is_nothrow_destructible_v<MoveOnlyImpl>, "Must be nothrow destructible");
static_assert(!is_copy_constructible_v<MoveOnlyImpl>, "Must not be copy constructible"); static_assert(!std_support::is_copy_constructible_v<MoveOnlyImpl>, "Must not be copy constructible");
static_assert(!is_copy_assignable_v<MoveOnlyImpl>, "Must not be copy assignable"); static_assert(!std_support::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(std_support::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_move_assignable_v<MoveOnlyImpl>, "Must be nothrow move assignable");
static_assert(sizeof(MoveOnlyImpl) == sizeof(A), "Must not increase size"); static_assert(sizeof(MoveOnlyImpl) == sizeof(A), "Must not increase size");
} }
TEST(UtilsTest, PinnedImpl) { TEST(UtilsTest, PinnedImpl) {
static_assert(is_nothrow_default_constructible_v<PinnedImpl>, "Must be nothrow default constructible"); static_assert(std_support::is_nothrow_default_constructible_v<PinnedImpl>, "Must be nothrow default constructible");
static_assert(is_nothrow_destructible_v<PinnedImpl>, "Must be nothrow destructible"); static_assert(std_support::is_nothrow_destructible_v<PinnedImpl>, "Must be nothrow destructible");
static_assert(!is_copy_constructible_v<PinnedImpl>, "Must not be copy constructible"); static_assert(!std_support::is_copy_constructible_v<PinnedImpl>, "Must not be copy constructible");
static_assert(!is_copy_assignable_v<PinnedImpl>, "Must not be copy assignable"); static_assert(!std_support::is_copy_assignable_v<PinnedImpl>, "Must not be copy assignable");
static_assert(!is_move_constructible_v<PinnedImpl>, "Must not be move constructible"); static_assert(!std_support::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_move_assignable_v<PinnedImpl>, "Must not be move assignable");
static_assert(sizeof(PinnedImpl) == sizeof(A), "Must not increase size"); static_assert(sizeof(PinnedImpl) == sizeof(A), "Must not increase size");
} }