Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable C++20 #221

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus") # enable correct __cplusplus macro
endif()

option(BUILD_SHARED_LIBS "Build shared library" ON)
option(BUILD_TESTING "Build and run tests" OFF)
option(BUILD_DOCUMENTATION "Build documentation" OFF)
Expand Down
18 changes: 14 additions & 4 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ std::ostream& operator<<(std::ostream& os, std::array<T, N> const& array)
return print_sequence(os, array, "[]");
}

template<typename Char, typename Traits, typename Alloc, typename = typename std::enable_if<!std::is_same<Char, char>::value>::type>
template<v8pp::detail::WideChar Char, typename Traits, typename Alloc>
std::ostream& operator<<(std::ostream& os, std::basic_string<Char, Traits, Alloc> const& string)
{
return print_sequence(os, string, "''");
}

template<typename Char, typename Traits, typename = typename std::enable_if_t<!std::is_same_v<Char, char>>>
template<v8pp::detail::WideChar Char, typename Traits>
std::ostream& operator<<(std::ostream& os, std::basic_string_view<Char, Traits> const& string_view)
{
return print_sequence(os, string_view, "''");
Expand All @@ -53,6 +53,16 @@ inline std::ostream& operator<<(std::ostream& os, char16_t const* str)
return print_sequence(os, std::basic_string_view<char16_t>(str), "''");
}

inline std::ostream& operator<<(std::ostream& os, char32_t ch)
{
return os << static_cast<int64_t>(ch);
}

inline std::ostream& operator<<(std::ostream& os, char32_t const* str)
{
return print_sequence(os, std::basic_string_view<char32_t>(str), "''");
}

inline std::ostream& operator<<(std::ostream& os, wchar_t ch)
{
return os << static_cast<int64_t>(ch);
Expand Down Expand Up @@ -141,10 +151,10 @@ std::ostream& operator<<(std::ostream& os, std::pair<First, Second> const& pair)
return os << pair.first << ": " << pair.second;
}

template<typename Enum, typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
template<typename Enum> requires std::is_enum_v<Enum>
std::ostream& operator<<(std::ostream& os, Enum value)
{
return os << static_cast<typename std::underlying_type<Enum>::type>(value);
return os << static_cast<typename std::underlying_type_t<Enum>>(value);
}

template<typename... Ts>
Expand Down
48 changes: 24 additions & 24 deletions test/test_call_from_v8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,47 @@ using arg_convert = typename call_from_v8_traits<F>::template arg_converter<
typename call_from_v8_traits<F>::template arg_type<Index>, Traits>;

// fundamental type converters
static_assert(std::is_same<arg_convert<decltype(y), 0, raw_ptr_traits>, v8pp::convert<int>>::value, "y(int)");
static_assert(std::is_same<arg_convert<decltype(y), 0, shared_ptr_traits>, v8pp::convert<int>>::value, "y(int)");
static_assert(std::same_as<arg_convert<decltype(y), 0, raw_ptr_traits>, v8pp::convert<int>>);
static_assert(std::same_as<arg_convert<decltype(y), 0, shared_ptr_traits>, v8pp::convert<int>>);

static_assert(std::is_same<arg_convert<decltype(z), 1, raw_ptr_traits>, v8pp::convert<int>>::value, "y(int)");
static_assert(std::is_same<arg_convert<decltype(z), 1, shared_ptr_traits>, v8pp::convert<int>>::value, "y(int)");
static_assert(std::same_as<arg_convert<decltype(z), 1, raw_ptr_traits>, v8pp::convert<int>>);
static_assert(std::same_as<arg_convert<decltype(z), 1, shared_ptr_traits>, v8pp::convert<int>>);

// cv arg converters
static void s(std::string, std::vector<int>&, std::shared_ptr<int> const&, std::string*, std::string const*) {}

static_assert(std::is_same<arg_convert<decltype(s), 0, raw_ptr_traits>, v8pp::convert<std::string>>::value, "s(string)");
static_assert(std::is_same<arg_convert<decltype(s), 0, shared_ptr_traits>, v8pp::convert<std::string>>::value, "s(string)");
static_assert(std::same_as<arg_convert<decltype(s), 0, raw_ptr_traits>, v8pp::convert<std::string>>);
static_assert(std::same_as<arg_convert<decltype(s), 0, shared_ptr_traits>, v8pp::convert<std::string>>);

static_assert(std::is_same<arg_convert<decltype(s), 1, raw_ptr_traits>, v8pp::convert<std::vector<int>>>::value, "s(vector<int>&)");
static_assert(std::is_same<arg_convert<decltype(s), 1, shared_ptr_traits>, v8pp::convert<std::vector<int>>>::value, "s(vector<int>&)");
static_assert(std::same_as<arg_convert<decltype(s), 1, raw_ptr_traits>, v8pp::convert<std::vector<int>>>);
static_assert(std::same_as<arg_convert<decltype(s), 1, shared_ptr_traits>, v8pp::convert<std::vector<int>>>);

static_assert(std::is_same<arg_convert<decltype(s), 2, raw_ptr_traits>, v8pp::convert<std::shared_ptr<int>>>::value, "s(std::shared_ptr<int> const&)");
static_assert(std::is_same<arg_convert<decltype(s), 2, shared_ptr_traits>, v8pp::convert<std::shared_ptr<int>>>::value, "s(std::shared_ptr<int> const&)");
static_assert(std::same_as<arg_convert<decltype(s), 2, raw_ptr_traits>, v8pp::convert<std::shared_ptr<int>>>);
static_assert(std::same_as<arg_convert<decltype(s), 2, shared_ptr_traits>, v8pp::convert<std::shared_ptr<int>>>);

static_assert(std::is_same<arg_convert<decltype(s), 3, raw_ptr_traits>, v8pp::convert<std::string*>>::value, "s(std::string*)");
static_assert(std::is_same<arg_convert<decltype(s), 3, shared_ptr_traits>, v8pp::convert<std::string*>>::value, "s(std::string*)");
static_assert(std::same_as<arg_convert<decltype(s), 3, raw_ptr_traits>, v8pp::convert<std::string*>>);
static_assert(std::same_as<arg_convert<decltype(s), 3, shared_ptr_traits>, v8pp::convert<std::string*>>);

static_assert(std::is_same<arg_convert<decltype(s), 4, raw_ptr_traits>, v8pp::convert<std::string const*>>::value, "s(std::string const*)");
static_assert(std::is_same<arg_convert<decltype(s), 4, shared_ptr_traits>, v8pp::convert<std::string const*>>::value, "s(std::string const*)");
static_assert(std::same_as<arg_convert<decltype(s), 4, raw_ptr_traits>, v8pp::convert<std::string const*>>);
static_assert(std::same_as<arg_convert<decltype(s), 4, shared_ptr_traits>, v8pp::convert<std::string const*>>);

// fundamental types cv arg converters
static void t(int, char&, bool const&, float*, char const*) {}

static_assert(std::is_same<arg_convert<decltype(t), 0, raw_ptr_traits>, v8pp::convert<int>>::value, "t(int)");
static_assert(std::is_same<arg_convert<decltype(t), 0, shared_ptr_traits>, v8pp::convert<int>>::value, "t(int)");
static_assert(std::same_as<arg_convert<decltype(t), 0, raw_ptr_traits>, v8pp::convert<int>>);
static_assert(std::same_as<arg_convert<decltype(t), 0, shared_ptr_traits>, v8pp::convert<int>>);

static_assert(std::is_same<arg_convert<decltype(t), 1, raw_ptr_traits>, v8pp::convert<char>>::value, "t(char&)");
static_assert(std::is_same<arg_convert<decltype(t), 1, shared_ptr_traits>, v8pp::convert<char>>::value, "t(char&)");
static_assert(std::same_as<arg_convert<decltype(t), 1, raw_ptr_traits>, v8pp::convert<char>>);
static_assert(std::same_as<arg_convert<decltype(t), 1, shared_ptr_traits>, v8pp::convert<char>>);

static_assert(std::is_same<arg_convert<decltype(t), 2, raw_ptr_traits>, v8pp::convert<bool>>::value, "t(bool const&)");
static_assert(std::is_same<arg_convert<decltype(t), 2, shared_ptr_traits>, v8pp::convert<bool>>::value, "t(bool const&)");
static_assert(std::same_as<arg_convert<decltype(t), 2, raw_ptr_traits>, v8pp::convert<bool>>);
static_assert(std::same_as<arg_convert<decltype(t), 2, shared_ptr_traits>, v8pp::convert<bool>>);

static_assert(std::is_same<arg_convert<decltype(t), 3, raw_ptr_traits>, v8pp::convert<float*>>::value, "t(float*)");
static_assert(std::is_same<arg_convert<decltype(t), 3, shared_ptr_traits>, v8pp::convert<float*>>::value, "t(float*)");
static_assert(std::same_as<arg_convert<decltype(t), 3, raw_ptr_traits>, v8pp::convert<float*>>);
static_assert(std::same_as<arg_convert<decltype(t), 3, shared_ptr_traits>, v8pp::convert<float*>>);

static_assert(std::is_same<arg_convert<decltype(t), 4, raw_ptr_traits>, v8pp::convert<char const*>>::value, "t(char const*)");
static_assert(std::is_same<arg_convert<decltype(t), 4, shared_ptr_traits>, v8pp::convert<char const*>>::value, "t(char const*)");
static_assert(std::same_as<arg_convert<decltype(t), 4, raw_ptr_traits>, v8pp::convert<char const*>>);
static_assert(std::same_as<arg_convert<decltype(t), 4, shared_ptr_traits>, v8pp::convert<char const*>>);

void test_call_from_v8()
{
Expand Down
10 changes: 5 additions & 5 deletions test/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ void test_class_()
.static_("my_static_const_var", 42, true)
;

static_assert(std::is_move_constructible<decltype(X_class)>::value, "");
static_assert(!std::is_move_assignable<decltype(X_class)>::value, "");
static_assert(!std::is_copy_assignable<decltype(X_class)>::value, "");
static_assert(!std::is_copy_constructible<decltype(X_class)>::value, "");
static_assert(std::is_move_constructible_v<decltype(X_class)>);
static_assert(!std::is_move_assignable_v<decltype(X_class)>);
static_assert(!std::is_copy_assignable_v<decltype(X_class)>);
static_assert(!std::is_copy_constructible_v<decltype(X_class)>);

v8pp::class_<Y, Traits> Y_class(isolate);
Y_class
Expand Down Expand Up @@ -324,7 +324,7 @@ void test_class_()
context.isolate()->RequestGarbageCollectionForTesting(
v8::Isolate::GarbageCollectionType::kFullGarbageCollection);

bool const use_shared_ptr = std::is_same<Traits, v8pp::shared_ptr_traits>::value;
bool constexpr use_shared_ptr = std::same_as<Traits, v8pp::shared_ptr_traits>;

check_eq("Y count after GC", Y::instance_count,
1 + 2 * use_shared_ptr); // y1 + (y2 + y3 when use_shared_ptr)
Expand Down
8 changes: 4 additions & 4 deletions test/test_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include <type_traits>

static_assert(std::is_move_constructible<v8pp::context>::value, "");
static_assert(std::is_move_assignable<v8pp::context>::value, "");
static_assert(!std::is_copy_assignable<v8pp::context>::value, "");
static_assert(!std::is_copy_constructible<v8pp::context>::value, "");
static_assert(std::is_move_constructible_v<v8pp::context>);
static_assert(std::is_move_assignable_v<v8pp::context>);
static_assert(!std::is_copy_assignable_v<v8pp::context>);
static_assert(!std::is_copy_constructible_v<v8pp::context>);

void test_context()
{
Expand Down
4 changes: 2 additions & 2 deletions test/test_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ void check_range(v8::Isolate* isolate)

T zero{ 0 };
T min, max;
if constexpr (std::is_same_v<T, int64_t>)
if constexpr (std::same_as<T, int64_t>)
{
min = V8_MIN_INT;
max = V8_MAX_INT;
}
else if constexpr (std::is_same_v<T, uint64_t>)
else if constexpr (std::same_as<T, uint64_t>)
{
min = 0;
max = V8_MAX_INT;
Expand Down
8 changes: 4 additions & 4 deletions test/test_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ static int x = 1;
static int get_x() { return x + 1; }
static void set_x(int v) { x = v - 1; }

static_assert(std::is_move_constructible<v8pp::module>::value, "");
static_assert(std::is_move_assignable<v8pp::module>::value, "");
static_assert(!std::is_copy_assignable<v8pp::module>::value, "");
static_assert(!std::is_copy_constructible<v8pp::module>::value, "");
static_assert(std::is_move_constructible_v<v8pp::module>);
static_assert(std::is_move_assignable_v<v8pp::module>);
static_assert(!std::is_copy_assignable_v<v8pp::module>);
static_assert(!std::is_copy_constructible_v<v8pp::module>);

void test_module()
{
Expand Down
28 changes: 14 additions & 14 deletions test/test_ptr_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ void test_raw_ptr_traits()
X x;
traits::pointer_type ptr = &x;

static_assert(std::is_same<traits::pointer_type, void*>::value, "raw pointer_type");
static_assert(std::is_same<traits::const_pointer_type, void const*>::value, "raw const_pointer_type");
static_assert(std::same_as<traits::pointer_type, void*>);
static_assert(std::same_as<traits::const_pointer_type, void const*>);

static_assert(std::is_same<traits::object_pointer_type<X>, X*>::value, "raw object_pointer_type");
static_assert(std::is_same<traits::object_const_pointer_type<X>, X const*>::value, "raw object_const_pointer_type");
static_assert(std::same_as<traits::object_pointer_type<X>, X*>);
static_assert(std::same_as<traits::object_const_pointer_type<X>, X const*>);

static_assert(std::is_same<traits::object_id, void*>::value, "raw object_id");
static_assert(std::is_same<traits::convert_ptr<X>, v8pp::convert<X*>>::value, "raw convert_ptr");
static_assert(std::is_same<traits::convert_ref<X>, v8pp::convert<X&>>::value, "raw convert_ref");
static_assert(std::same_as<traits::object_id, void*>);
static_assert(std::same_as<traits::convert_ptr<X>, v8pp::convert<X*>>);
static_assert(std::same_as<traits::convert_ref<X>, v8pp::convert<X&>>);

traits::object_id id = traits::pointer_id(ptr);
check_eq("raw_ptr_traits::pointer_id", id, &x);
Expand All @@ -145,15 +145,15 @@ void test_shared_ptr_traits()
std::shared_ptr<Y> y(Y::make(1), Y::done);
traits::pointer_type ptr = y;

static_assert(std::is_same<traits::pointer_type, std::shared_ptr<void>>::value, "shared pointer_type");
static_assert(std::is_same<traits::const_pointer_type, std::shared_ptr<void const>>::value, "shared const_pointer_type");
static_assert(std::same_as<traits::pointer_type, std::shared_ptr<void>>);
static_assert(std::same_as<traits::const_pointer_type, std::shared_ptr<void const>>);

static_assert(std::is_same<traits::object_pointer_type<Y>, std::shared_ptr<Y>>::value, "shared object_pointer_type");
static_assert(std::is_same<traits::object_const_pointer_type<Y>, std::shared_ptr<Y const>>::value, "shared object_const_pointer_type");
static_assert(std::same_as<traits::object_pointer_type<Y>, std::shared_ptr<Y>>);
static_assert(std::same_as<traits::object_const_pointer_type<Y>, std::shared_ptr<Y const>>);

static_assert(std::is_same<traits::object_id, void*>::value, "shared object_id");
static_assert(std::is_same<traits::convert_ptr<Y>, v8pp::convert<std::shared_ptr<Y>>>::value, "shared convert_ptr");
static_assert(std::is_same<traits::convert_ref<Y>, v8pp::convert<Y, v8pp::ref_from_shared_ptr>>::value, "shared convert_ref");
static_assert(std::same_as<traits::object_id, void*>);
static_assert(std::same_as<traits::convert_ptr<Y>, v8pp::convert<std::shared_ptr<Y>>>);
static_assert(std::same_as<traits::convert_ref<Y>, v8pp::convert<Y, v8pp::ref_from_shared_ptr>>);

traits::object_id id = traits::pointer_id(ptr);
check_eq("shared_ptr_traits::pointer_id", id, y.get());
Expand Down
10 changes: 5 additions & 5 deletions test/test_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ template<typename Ret, typename F>
void test_ret(F&&)
{
using R = typename v8pp::detail::function_traits<F>::return_type;
static_assert(std::is_same<Ret, R>::value, "wrong return_type");
static_assert(std::same_as<Ret, R>);
}

template<typename ArgsTuple, typename F>
void test_args(F&&)
{
using Args = typename v8pp::detail::function_traits<F>::arguments;
static_assert(std::is_same<ArgsTuple, Args>::value, "wrong arguments");
static_assert(std::same_as<ArgsTuple, Args>);
}

template<typename Ret, typename Derived, typename F>
Expand Down Expand Up @@ -134,9 +134,9 @@ void test_tuple_tail()
{
using v8pp::detail::tuple_tail;

static_assert(std::is_same<tuple_tail<std::tuple<int>>::type, std::tuple<>>::value, "");
static_assert(std::is_same<tuple_tail<std::tuple<int, char>>::type, std::tuple<char>>::value, "");
static_assert(std::is_same<tuple_tail<std::tuple<int, char, bool>>::type, std::tuple<char, bool>>::value, "");
static_assert(std::same_as<tuple_tail<std::tuple<int>>::type, std::tuple<>>);
static_assert(std::same_as<tuple_tail<std::tuple<int, char>>::type, std::tuple<char>>);
static_assert(std::same_as<tuple_tail<std::tuple<int, char, bool>>::type, std::tuple<char, bool>>);
}

int f() { return 1; }
Expand Down
18 changes: 7 additions & 11 deletions v8pp/call_from_v8.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef V8PP_CALL_FROM_V8_HPP_INCLUDED
#define V8PP_CALL_FROM_V8_HPP_INCLUDED
#pragma once

#include <functional>
#include <utility>
Expand All @@ -9,17 +8,16 @@
#include "v8pp/convert.hpp"
#include "v8pp/utility.hpp"

namespace v8pp { namespace detail {
namespace v8pp::detail {

template<typename F, size_t Offset = 0>
struct call_from_v8_traits
{
static constexpr size_t offset = Offset;
static constexpr bool is_mem_fun = std::is_member_function_pointer<F>::value;
static constexpr bool is_mem_fun = std::is_member_function_pointer_v<F>;
using arguments = typename function_traits<F>::arguments;

static constexpr size_t arg_count =
std::tuple_size<arguments>::value - is_mem_fun - offset;
static constexpr size_t arg_count = std::tuple_size_v<arguments> - is_mem_fun - offset;

template<size_t Index, bool>
struct tuple_element
Expand Down Expand Up @@ -56,11 +54,11 @@ struct call_from_v8_traits

template<typename F, size_t Offset, typename CallTraits = call_from_v8_traits<F>>
inline constexpr bool is_direct_args = CallTraits::arg_count == (Offset + 1) &&
std::is_same_v<typename CallTraits::template arg_type<Offset>, v8::FunctionCallbackInfo<v8::Value> const&>;
std::same_as<typename CallTraits::template arg_type<Offset>, v8::FunctionCallbackInfo<v8::Value> const&>;

template<typename F, size_t Offset = 0, typename CallTraits = call_from_v8_traits<F>>
inline constexpr bool is_first_arg_isolate = CallTraits::arg_count != (Offset + 0) &&
std::is_same_v<typename CallTraits::template arg_type<Offset>, v8::Isolate*>;
std::same_as<typename CallTraits::template arg_type<Offset>, v8::Isolate*>;

template<typename Traits, typename F, typename CallTraits, size_t... Indices, typename... ObjArg>
decltype(auto) call_from_v8_impl(F&& func, v8::FunctionCallbackInfo<v8::Value> const& args,
Expand Down Expand Up @@ -112,6 +110,4 @@ decltype(auto) call_from_v8(F&& func, v8::FunctionCallbackInfo<v8::Value> const&
}
}

}} // namespace v8pp::detail

#endif // V8PP_CALL_FROM_V8_HPP_INCLUDED
} // namespace v8pp::detail
5 changes: 1 addition & 4 deletions v8pp/call_v8.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef V8PP_CALL_V8_HPP_INCLUDED
#define V8PP_CALL_V8_HPP_INCLUDED
#pragma once

#include <v8.h>

Expand Down Expand Up @@ -33,5 +32,3 @@ v8::Local<v8::Value> call_v8(v8::Isolate* isolate, v8::Local<v8::Function> func,
}

} // namespace v8pp

#endif // V8PP_CALL_V8_HPP_INCLUDED
4 changes: 2 additions & 2 deletions v8pp/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#if !V8PP_HEADER_ONLY
#include "v8pp/class.ipp"

namespace v8pp { namespace detail {
namespace v8pp::detail {

template
class object_registry<raw_ptr_traits>;
Expand Down Expand Up @@ -33,6 +33,6 @@ template
object_registry<shared_ptr_traits>& classes::find<shared_ptr_traits>(v8::Isolate* isolate,
type_info const& type);

}} // namespace v8pp::detail
} // namespace v8pp::detail

#endif
Loading
Loading