Commit ccd10f94 authored by Philip Chimento's avatar Philip Chimento 🚮

maint: Use concise C++17 type traits

Now that we have C++17, we can make these a bit more readable.
parent ff05be33
...@@ -193,13 +193,11 @@ GJS_USE inline gboolean gjs_arg_get<gboolean, GI_TYPE_TAG_BOOLEAN>( ...@@ -193,13 +193,11 @@ GJS_USE inline gboolean gjs_arg_get<gboolean, GI_TYPE_TAG_BOOLEAN>(
} }
template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID> template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
inline std::enable_if_t<!std::is_pointer<T>::value> gjs_arg_unset( inline std::enable_if_t<!std::is_pointer_v<T>> gjs_arg_unset(GIArgument* arg) {
GIArgument* arg) {
gjs_arg_set<T, TAG>(arg, static_cast<T>(0)); gjs_arg_set<T, TAG>(arg, static_cast<T>(0));
} }
template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID> template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
inline std::enable_if_t<std::is_pointer<T>::value> gjs_arg_unset( inline std::enable_if_t<std::is_pointer_v<T>> gjs_arg_unset(GIArgument* arg) {
GIArgument* arg) {
gjs_arg_set<T, TAG>(arg, nullptr); gjs_arg_set<T, TAG>(arg, nullptr);
} }
...@@ -480,14 +480,14 @@ create_hash_table_for_key_type(GITypeInfo *key_param_info) ...@@ -480,14 +480,14 @@ create_hash_table_for_key_type(GITypeInfo *key_param_info)
template <typename T> template <typename T>
GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t< GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t<
std::is_integral<T>::value && std::is_signed<T>::value, bool> std::is_integral_v<T> && std::is_signed_v<T>, bool>
js_value_convert(JSContext* cx, const JS::HandleValue& value, T* out) { js_value_convert(JSContext* cx, const JS::HandleValue& value, T* out) {
return JS::ToInt32(cx, value, out); return JS::ToInt32(cx, value, out);
} }
template <typename T> template <typename T>
GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t< GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t<
std::is_integral<T>::value && std::is_unsigned<T>::value, bool> std::is_integral_v<T> && std::is_unsigned_v<T>, bool>
js_value_convert(JSContext* cx, const JS::HandleValue& value, T* out) { js_value_convert(JSContext* cx, const JS::HandleValue& value, T* out) {
return JS::ToUint32(cx, value, out); return JS::ToUint32(cx, value, out);
} }
...@@ -498,7 +498,7 @@ GJS_JSAPI_RETURN_CONVENTION static bool hashtable_int_key( ...@@ -498,7 +498,7 @@ GJS_JSAPI_RETURN_CONVENTION static bool hashtable_int_key(
void** pointer_out) { void** pointer_out) {
Container i; Container i;
static_assert(std::is_integral<IntType>::value, "Need an integer"); static_assert(std::is_integral_v<IntType>, "Need an integer");
static_assert(std::numeric_limits<Container>::max() >= static_assert(std::numeric_limits<Container>::max() >=
std::numeric_limits<IntType>::max(), std::numeric_limits<IntType>::max(),
"Max possible Container value must be at least the max possible IntType value"); "Max possible Container value must be at least the max possible IntType value");
...@@ -525,7 +525,7 @@ GJS_JSAPI_RETURN_CONVENTION static bool hashtable_int_key( ...@@ -525,7 +525,7 @@ GJS_JSAPI_RETURN_CONVENTION static bool hashtable_int_key(
template <typename IntType> template <typename IntType>
GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t< GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t<
std::is_signed<IntType>::value, bool> std::is_signed_v<IntType>, bool>
hashtable_int_key(JSContext* cx, const JS::HandleValue& value, hashtable_int_key(JSContext* cx, const JS::HandleValue& value,
bool* out_of_range, void** pointer_out) { bool* out_of_range, void** pointer_out) {
return hashtable_int_key<IntType, int32_t>(cx, value, out_of_range, return hashtable_int_key<IntType, int32_t>(cx, value, out_of_range,
...@@ -534,7 +534,7 @@ hashtable_int_key(JSContext* cx, const JS::HandleValue& value, ...@@ -534,7 +534,7 @@ hashtable_int_key(JSContext* cx, const JS::HandleValue& value,
template <typename IntType> template <typename IntType>
GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t< GJS_JSAPI_RETURN_CONVENTION static inline std::enable_if_t<
std::is_unsigned<IntType>::value, bool> std::is_unsigned_v<IntType>, bool>
hashtable_int_key(JSContext* cx, const JS::HandleValue& value, hashtable_int_key(JSContext* cx, const JS::HandleValue& value,
bool* out_of_range, void** pointer_out) { bool* out_of_range, void** pointer_out) {
return hashtable_int_key<IntType, uint32_t>(cx, value, out_of_range, return hashtable_int_key<IntType, uint32_t>(cx, value, out_of_range,
......
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
#include <glib-object.h> #include <glib-object.h>
#include <glib.h> #include <glib.h>
#include <js/AllocPolicy.h>
#include <js/GCHashTable.h> #include <js/GCHashTable.h>
#include <js/GCVector.h> #include <js/GCVector.h>
#include <js/Promise.h> #include <js/Promise.h>
...@@ -54,6 +53,9 @@ namespace mozilla { ...@@ -54,6 +53,9 @@ namespace mozilla {
template <class Key> template <class Key>
struct DefaultHasher; struct DefaultHasher;
} }
namespace js {
class SystemAllocPolicy;
}
class GjsAtoms; class GjsAtoms;
class JSTracer; class JSTracer;
...@@ -70,7 +72,7 @@ using GTypeTable = ...@@ -70,7 +72,7 @@ using GTypeTable =
struct Dummy {}; struct Dummy {};
using GTypeNotUint64 = using GTypeNotUint64 =
std::conditional_t<!std::is_same<GType, uint64_t>::value, GType, Dummy>; std::conditional_t<!std::is_same_v<GType, uint64_t>, GType, Dummy>;
// The GC sweep method should ignore FundamentalTable and GTypeTable's key types // The GC sweep method should ignore FundamentalTable and GTypeTable's key types
namespace JS { namespace JS {
......
...@@ -33,8 +33,6 @@ ...@@ -33,8 +33,6 @@
#include <stdint.h> #include <stdint.h>
#include <type_traits> // for is_same
#include "gjs/macros.h" #include "gjs/macros.h"
enum class GjsGlobalType { enum class GjsGlobalType {
...@@ -95,18 +93,18 @@ JS::Value get_global_slot(JSObject* global, uint32_t slot); ...@@ -95,18 +93,18 @@ JS::Value get_global_slot(JSObject* global, uint32_t slot);
template <typename Slot> template <typename Slot>
inline void gjs_set_global_slot(JSObject* global, Slot slot, JS::Value value) { inline void gjs_set_global_slot(JSObject* global, Slot slot, JS::Value value) {
static_assert(std::is_same<GjsBaseGlobalSlot, Slot>::value || static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
std::is_same<GjsGlobalSlot, Slot>::value || std::is_same_v<GjsGlobalSlot, Slot> ||
std::is_same<GjsDebuggerGlobalSlot, Slot>::value, std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
"Must use a GJS global slot enum"); "Must use a GJS global slot enum");
detail::set_global_slot(global, static_cast<uint32_t>(slot), value); detail::set_global_slot(global, static_cast<uint32_t>(slot), value);
} }
template <typename Slot> template <typename Slot>
inline JS::Value gjs_get_global_slot(JSObject* global, Slot slot) { inline JS::Value gjs_get_global_slot(JSObject* global, Slot slot) {
static_assert(std::is_same<GjsBaseGlobalSlot, Slot>::value || static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
std::is_same<GjsGlobalSlot, Slot>::value || std::is_same_v<GjsGlobalSlot, Slot> ||
std::is_same<GjsDebuggerGlobalSlot, Slot>::value, std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
"Must use a GJS global slot enum"); "Must use a GJS global slot enum");
return detail::get_global_slot(global, static_cast<uint32_t>(slot)); return detail::get_global_slot(global, static_cast<uint32_t>(slot));
} }
......
...@@ -189,16 +189,10 @@ assign(JSContext *cx, ...@@ -189,16 +189,10 @@ assign(JSContext *cx,
/* Special case: treat pointer-to-enum as pointer-to-int, but use enable_if to /* Special case: treat pointer-to-enum as pointer-to-int, but use enable_if to
* prevent instantiation for any other types besides pointer-to-enum */ * prevent instantiation for any other types besides pointer-to-enum */
template<typename T, template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
typename std::enable_if<std::is_enum<T>::value, int>::type = 0> GJS_ALWAYS_INLINE static inline void assign(JSContext* cx, char c,
GJS_ALWAYS_INLINE bool nullable,
static inline void JS::HandleValue value, T* ref) {
assign(JSContext *cx,
char c,
bool nullable,
JS::HandleValue value,
T *ref)
{
/* Sadly, we cannot use std::underlying_type<T> here; the underlying type of /* Sadly, we cannot use std::underlying_type<T> here; the underlying type of
* an enum is implementation-defined, so it would not be clear what letter * an enum is implementation-defined, so it would not be clear what letter
* to use in the format string. For the same reason, we can only support * to use in the format string. For the same reason, we can only support
...@@ -212,9 +206,8 @@ assign(JSContext *cx, ...@@ -212,9 +206,8 @@ assign(JSContext *cx,
/* Force JS::RootedObject * to be converted to JS::MutableHandleObject, /* Force JS::RootedObject * to be converted to JS::MutableHandleObject,
* see overload in jsapi-util-args.cpp */ * see overload in jsapi-util-args.cpp */
template <typename T, template <typename T, typename std::enable_if_t<
typename std::enable_if<!std::is_same<T, JS::RootedObject*>::value, !std::is_same_v<T, JS::RootedObject*>, int> = 0>
int>::type = 0>
static inline void free_if_necessary(T param_ref G_GNUC_UNUSED) {} static inline void free_if_necessary(T param_ref G_GNUC_UNUSED) {}
GJS_ALWAYS_INLINE GJS_ALWAYS_INLINE
......
...@@ -204,7 +204,7 @@ class GjsMaybeOwned { ...@@ -204,7 +204,7 @@ class GjsMaybeOwned {
/* Use debug_addr() only for debug logging, because it is unbarriered. */ /* Use debug_addr() only for debug logging, because it is unbarriered. */
template <typename U = T> template <typename U = T>
GJS_USE const void* debug_addr( GJS_USE const void* debug_addr(
std::enable_if_t<std::is_pointer<U>::value>* = nullptr) const { std::enable_if_t<std::is_pointer_v<U>>* = nullptr) const {
return m_root ? m_root->get() : m_heap.unbarrieredGet(); return m_root ? m_root->get() : m_heap.unbarrieredGet();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment