Skip to content

Commit

Permalink
fix: Put template<...> on a separate line
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Aug 2, 2024
1 parent 3907c89 commit a522f09
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 54 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ReflowComments: true
# Line breaking options
BreakBeforeBraces: Attach
BreakConstructorInitializers: BeforeColon
AlwaysBreakTemplateDeclarations: true
AllowShortFunctionsOnASingleLine: Empty
IndentCaseLabels: true
NamespaceIndentation: Inner
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace margelo::nitro {

using namespace facebook;

template <typename T> struct GlobalRefDeleter {
template <typename T>
struct GlobalRefDeleter {
explicit GlobalRefDeleter(jni::global_ref<typename T::javaobject> ref) : _ref(ref) {}

void operator()(T* ptr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class HybridObject : public jsi::HostObject, public std::enable_shared_from_this
* Get the `std::shared_ptr` instance of this HybridObject.
* The HybridObject must be managed inside a `shared_ptr` already, otherwise this will fail.
*/
template <typename Derived> std::shared_ptr<Derived> shared() {
template <typename Derived>
std::shared_ptr<Derived> shared() {
return std::static_pointer_cast<Derived>(shared_from_this());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace margelo::nitro {

using namespace facebook;

template <typename T> class PointerHolder final : public HybridObject {
template <typename T>
class PointerHolder final : public HybridObject {
protected:
// no default constructor
PointerHolder() = delete;
Expand Down
9 changes: 6 additions & 3 deletions packages/react-native-nitro-modules/cpp/jsi/JSICache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ using namespace facebook;

static constexpr auto CACHE_PROP_NAME = "__nitroModulesJSICache";

template <typename T> class JSICache;
template <typename T>
class JSICache;

template <typename T> class JSICacheReference final {
template <typename T>
class JSICacheReference final {
public:
JSICacheReference() = delete;
JSICacheReference(const JSICacheReference&) = delete;
Expand Down Expand Up @@ -70,7 +72,8 @@ template <typename T> class JSICacheReference final {
* `jsi::Pointer`s are managed by a `jsi::Runtime`, and will be deleted if the `jsi::Runtime`
* is deleted - even if there are still strong references to the `jsi::Pointer`.
*/
template <typename T = jsi::Pointer> class JSICache final : public jsi::NativeState {
template <typename T = jsi::Pointer>
class JSICache final : public jsi::NativeState {
public:
explicit JSICache(jsi::Runtime* runtime) : _runtime(runtime) {}

Expand Down
93 changes: 62 additions & 31 deletions packages/react-native-nitro-modules/cpp/jsi/JSIConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ namespace margelo::nitro {
using namespace facebook;

// Unknown type (error)
template <typename ArgType, typename Enable = void> struct JSIConverter final {
template <typename ArgType, typename Enable = void>
struct JSIConverter final {
JSIConverter() = delete;

static inline ArgType fromJSI(jsi::Runtime&, const jsi::Value&) {
Expand All @@ -53,11 +54,13 @@ template <typename ArgType, typename Enable = void> struct JSIConverter final {
}

private:
template <typename> struct always_false : std::false_type {};
template <typename>
struct always_false : std::false_type {};
};

// int <> number
template <> struct JSIConverter<int> {
template <>
struct JSIConverter<int> {
static inline int fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return static_cast<int>(arg.asNumber());
}
Expand All @@ -67,7 +70,8 @@ template <> struct JSIConverter<int> {
};

// std::monostate <> null
template <> struct JSIConverter<std::monostate> {
template <>
struct JSIConverter<std::monostate> {
static inline std::monostate fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return std::monostate();
}
Expand All @@ -77,7 +81,8 @@ template <> struct JSIConverter<std::monostate> {
};

// double <> number
template <> struct JSIConverter<double> {
template <>
struct JSIConverter<double> {
static inline double fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return arg.asNumber();
}
Expand All @@ -87,7 +92,8 @@ template <> struct JSIConverter<double> {
};

// float <> number
template <> struct JSIConverter<float> {
template <>
struct JSIConverter<float> {
static inline float fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return static_cast<float>(arg.asNumber());
}
Expand All @@ -97,7 +103,8 @@ template <> struct JSIConverter<float> {
};

// int64_t <> BigInt
template <> struct JSIConverter<int64_t> {
template <>
struct JSIConverter<int64_t> {
static inline double fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
return arg.asBigInt(runtime).asInt64(runtime);
}
Expand All @@ -107,7 +114,8 @@ template <> struct JSIConverter<int64_t> {
};

// uint64_t <> BigInt
template <> struct JSIConverter<uint64_t> {
template <>
struct JSIConverter<uint64_t> {
static inline double fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
return arg.asBigInt(runtime).asUint64(runtime);
}
Expand All @@ -117,7 +125,8 @@ template <> struct JSIConverter<uint64_t> {
};

// bool <> boolean
template <> struct JSIConverter<bool> {
template <>
struct JSIConverter<bool> {
static inline bool fromJSI(jsi::Runtime&, const jsi::Value& arg) {
return arg.asBool();
}
Expand All @@ -127,7 +136,8 @@ template <> struct JSIConverter<bool> {
};

// std::string <> string
template <> struct JSIConverter<std::string> {
template <>
struct JSIConverter<std::string> {
static inline std::string fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
return arg.asString(runtime).utf8(runtime);
}
Expand All @@ -137,7 +147,8 @@ template <> struct JSIConverter<std::string> {
};

// std::optional<T> <> T | undefined
template <typename TInner> struct JSIConverter<std::optional<TInner>> {
template <typename TInner>
struct JSIConverter<std::optional<TInner>> {
static inline std::optional<TInner> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
if (arg.isUndefined() || arg.isNull()) {
return std::nullopt;
Expand All @@ -155,7 +166,8 @@ template <typename TInner> struct JSIConverter<std::optional<TInner>> {
};

// std::future<T> <> Promise<T>
template <typename TResult> struct JSIConverter<std::future<TResult>> {
template <typename TResult>
struct JSIConverter<std::future<TResult>> {
static inline std::future<TResult> fromJSI(jsi::Runtime&, const jsi::Value&) {
throw std::runtime_error("Promise cannot be converted to a native type - it needs to be awaited first!");
}
Expand Down Expand Up @@ -210,16 +222,20 @@ template <typename TResult> struct JSIConverter<std::future<TResult>> {
}
};

template <typename T> struct future_traits {
template <typename T>
struct future_traits {
using type = void;
};
template <typename T> struct future_traits<std::future<T>> {
template <typename T>
struct future_traits<std::future<T>> {
using type = T;
};
template <typename T> using future_traits_t = typename future_traits<std::remove_reference_t<T>>::type;
template <typename T>
using future_traits_t = typename future_traits<std::remove_reference_t<T>>::type;

// [](Args...) -> T {} <> (Args...) => T
template <typename ReturnType, typename... Args> struct JSIConverter<std::function<ReturnType(Args...)>> {
template <typename ReturnType, typename... Args>
struct JSIConverter<std::function<ReturnType(Args...)>> {
// std::future<T> -> T
using ResultingType = future_traits_t<ReturnType>;

Expand Down Expand Up @@ -311,7 +327,8 @@ template <typename ReturnType, typename... Args> struct JSIConverter<std::functi
};

// std::vector<T> <> T[]
template <typename ElementType> struct JSIConverter<std::vector<ElementType>> {
template <typename ElementType>
struct JSIConverter<std::vector<ElementType>> {
static inline std::vector<ElementType> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
jsi::Array array = arg.asObject(runtime).asArray(runtime);
size_t length = array.size(runtime);
Expand All @@ -335,7 +352,8 @@ template <typename ElementType> struct JSIConverter<std::vector<ElementType>> {
};

// std::unordered_map<std::string, T> <> Record<string, T>
template <typename ValueType> struct JSIConverter<std::unordered_map<std::string, ValueType>> {
template <typename ValueType>
struct JSIConverter<std::unordered_map<std::string, ValueType>> {
static inline std::unordered_map<std::string, ValueType> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
jsi::Object object = arg.asObject(runtime);
jsi::Array propertyNames = object.getPropertyNames(runtime);
Expand All @@ -361,7 +379,8 @@ template <typename ValueType> struct JSIConverter<std::unordered_map<std::string
};

// std::tuple<A, B, C> <> [A, B, C]
template <typename... Types> struct JSIConverter<std::tuple<Types...>> {
template <typename... Types>
struct JSIConverter<std::tuple<Types...>> {
static inline std::tuple<Types...> fromJSI(jsi::Runtime& runtime, const jsi::Value& value) {
jsi::Object object = value.asObject(runtime);
jsi::Array array = object.asArray(runtime);
Expand Down Expand Up @@ -396,11 +415,14 @@ template <typename... Types> struct JSIConverter<std::tuple<Types...>> {
};

// Helper struct to check if a type is present in a parameter pack
template <typename T, typename... Types> struct is_in_pack : std::disjunction<std::is_same<T, Types>...> {};
template <typename T, typename... Types> inline constexpr bool is_in_pack_v = is_in_pack<T, Types...>::value;
template <typename T, typename... Types>
struct is_in_pack : std::disjunction<std::is_same<T, Types>...> {};
template <typename T, typename... Types>
inline constexpr bool is_in_pack_v = is_in_pack<T, Types...>::value;

// std::variant<A, B, C> <> A | B | C
template <typename... Types> struct JSIConverter<std::variant<Types...>> {
template <typename... Types>
struct JSIConverter<std::variant<Types...>> {
static inline std::variant<Types...> fromJSI(jsi::Runtime& runtime, const jsi::Value& value) {
if (value.isNull()) {
if constexpr (is_in_pack_v<std::monostate, Types...>) {
Expand Down Expand Up @@ -465,7 +487,8 @@ template <typename... Types> struct JSIConverter<std::variant<Types...>> {
};

// AnyValue <> Record<K, V>
template <> struct JSIConverter<AnyValue> {
template <>
struct JSIConverter<AnyValue> {
static inline AnyValue fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
return JSIConverter<AnyValue::variant>::fromJSI(runtime, arg);
}
Expand All @@ -475,7 +498,8 @@ template <> struct JSIConverter<AnyValue> {
};

// AnyMap <> Record<K, V>
template <> struct JSIConverter<std::shared_ptr<AnyMap>> {
template <>
struct JSIConverter<std::shared_ptr<AnyMap>> {
static inline std::shared_ptr<AnyMap> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
jsi::Object object = arg.asObject(runtime);
jsi::Array propNames = object.getPropertyNames(runtime);
Expand All @@ -500,7 +524,8 @@ template <> struct JSIConverter<std::shared_ptr<AnyMap>> {
};

// MutableBuffer <> ArrayBuffer
template <> struct JSIConverter<std::shared_ptr<jsi::MutableBuffer>> {
template <>
struct JSIConverter<std::shared_ptr<jsi::MutableBuffer>> {
static inline std::shared_ptr<ArrayBuffer> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
jsi::Object object = arg.asObject(runtime);
if (!object.isArrayBuffer(runtime)) [[unlikely]] {
Expand All @@ -515,9 +540,12 @@ template <> struct JSIConverter<std::shared_ptr<jsi::MutableBuffer>> {
};

// HybridObject <> {}
template <typename T> struct is_shared_ptr_to_host_object : std::false_type {};
template <typename T> struct is_shared_ptr_to_host_object<std::shared_ptr<T>> : std::is_base_of<jsi::HostObject, T> {};
template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_host_object<T>::value>> {
template <typename T>
struct is_shared_ptr_to_host_object : std::false_type {};
template <typename T>
struct is_shared_ptr_to_host_object<std::shared_ptr<T>> : std::is_base_of<jsi::HostObject, T> {};
template <typename T>
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_host_object<T>::value>> {
using TPointee = typename T::element_type;

static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
Expand Down Expand Up @@ -564,9 +592,12 @@ template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_h
};

// NativeState <> {}
template <typename T> struct is_shared_ptr_to_native_state : std::false_type {};
template <typename T> struct is_shared_ptr_to_native_state<std::shared_ptr<T>> : std::is_base_of<jsi::NativeState, T> {};
template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_native_state<T>::value>> {
template <typename T>
struct is_shared_ptr_to_native_state : std::false_type {};
template <typename T>
struct is_shared_ptr_to_native_state<std::shared_ptr<T>> : std::is_base_of<jsi::NativeState, T> {};
template <typename T>
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_native_state<T>::value>> {
using TPointee = typename T::element_type;

static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class Dispatcher : public jsi::NativeState {
* Run the given function asynchronously on the Thread this Dispatcher is managing,
* and return a future that will hold the result of the function.
*/
template <typename T> std::future<T> runAsyncAwaitable(std::function<T()>&& function) {
template <typename T>
std::future<T> runAsyncAwaitable(std::function<T()>&& function) {
// 1. Create Promise that can be shared between this and dispatcher thread
auto promise = std::make_shared<std::promise<T>>();
std::future<T> future = promise->get_future();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace margelo::nitro {

template <typename T> BorrowingReference<T>::BorrowingReference(const OwningReference<T>& ref) {
template <typename T>
BorrowingReference<T>::BorrowingReference(const OwningReference<T>& ref) {
_value = ref._value;
_isDeleted = ref._isDeleted;
_strongRefCount = ref._strongRefCount;
Expand All @@ -20,7 +21,8 @@ template <typename T> BorrowingReference<T>::BorrowingReference(const OwningRefe
(*_weakRefCount)++;
}

template <typename T> OwningReference<T> BorrowingReference<T>::lock() {
template <typename T>
OwningReference<T> BorrowingReference<T>::lock() {
std::unique_lock lock(*_mutex);

if (*_isDeleted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
namespace margelo::nitro {

// forward-declaration to avoid duplicate symbols
template <typename T> class OwningReference;
template <typename T>
class OwningReference;

/**
A `BorrowingReference<T>` is a weak reference to a pointer created by `OwningReference<T>`.
It can be locked to gain a strong `OwningReference<T>` again if it has not been deleted yet.
*/
template <typename T> class BorrowingReference final {
template <typename T>
class BorrowingReference final {
private:
explicit BorrowingReference(const OwningReference<T>& ref);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
namespace margelo::nitro {

// By default, it is false (class does not exist)
template <typename T, typename = std::void_t<>> struct does_class_exist : std::false_type {};
template <typename T, typename = std::void_t<>>
struct does_class_exist : std::false_type {};

// If sizeof(T) can be a type (number), the type exists - so it is true
template <typename T> struct does_class_exist<T, std::void_t<decltype(sizeof(T))>> : std::true_type {};
template <typename T>
struct does_class_exist<T, std::void_t<decltype(sizeof(T))>> : std::true_type {};

// Direct value accessor for does_class_exist
template <typename T> inline constexpr bool does_class_exist_v = does_class_exist<T>::value;
template <typename T>
inline constexpr bool does_class_exist_v = does_class_exist<T>::value;

} // namespace margelo::nitro
3 changes: 2 additions & 1 deletion packages/react-native-nitro-modules/cpp/utils/NitroHash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ constexpr uint64_t hashString(const char* str, size_t length) {
*
* String length is known at compile time.
*/
template <size_t N> constexpr uint64_t hashString(const char (&str)[N]) {
template <size_t N>
constexpr uint64_t hashString(const char (&str)[N]) {
return hashString(str, N - 1); // N includes the null terminator, so subtract 1
}

Expand Down
Loading

0 comments on commit a522f09

Please sign in to comment.