Skip to content

Commit aaab470

Browse files
committed
Move policy code into wire.h so it can be shared with val.h and bind.h.
1 parent fe1d2ee commit aaab470

File tree

2 files changed

+180
-175
lines changed

2 files changed

+180
-175
lines changed

system/include/emscripten/bind.h

Lines changed: 0 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -283,181 +283,6 @@ struct InitFunc {
283283

284284
} // end namespace internal
285285

286-
////////////////////////////////////////////////////////////////////////////////
287-
// POLICIES
288-
////////////////////////////////////////////////////////////////////////////////
289-
290-
template<int Index>
291-
struct arg {
292-
static constexpr int index = Index + 1;
293-
};
294-
295-
struct ret_val {
296-
static constexpr int index = 0;
297-
};
298-
299-
/*
300-
template<typename Slot>
301-
struct allow_raw_pointer {
302-
template<typename InputType, int Index>
303-
struct Transform {
304-
typedef typename std::conditional<
305-
Index == Slot::index,
306-
internal::AllowedRawPointer<typename std::remove_pointer<InputType>::type>,
307-
InputType
308-
>::type type;
309-
};
310-
};
311-
*/
312-
313-
// allow all raw pointers
314-
struct allow_raw_pointers {
315-
template<typename InputType, int Index>
316-
struct Transform {
317-
// Use decay to handle references to pointers e.g.(T*&)->(T*).
318-
typedef typename std::decay<InputType>::type DecayedType;
319-
typedef typename std::conditional<
320-
std::is_pointer<DecayedType>::value,
321-
internal::AllowedRawPointer<typename std::remove_pointer<DecayedType>::type>,
322-
InputType
323-
>::type type;
324-
};
325-
};
326-
327-
// this is temporary until arg policies are reworked
328-
template<typename Slot>
329-
struct allow_raw_pointer : public allow_raw_pointers {
330-
};
331-
332-
struct async {
333-
template<typename InputType, int Index>
334-
struct Transform {
335-
typedef InputType type;
336-
};
337-
};
338-
339-
struct pure_virtual {
340-
template<typename InputType, int Index>
341-
struct Transform {
342-
typedef InputType type;
343-
};
344-
};
345-
346-
template<typename Slot>
347-
struct nonnull {
348-
static_assert(std::is_same<Slot, ret_val>::value, "Only nonnull return values are currently supported.");
349-
template<typename InputType, int Index>
350-
struct Transform {
351-
typedef InputType type;
352-
};
353-
};
354-
355-
namespace return_value_policy {
356-
357-
struct take_ownership : public allow_raw_pointers {};
358-
struct reference : public allow_raw_pointers {};
359-
360-
} // end namespace return_value_policy
361-
362-
namespace internal {
363-
364-
#if __cplusplus >= 201703L
365-
template <typename... Args> using conjunction = std::conjunction<Args...>;
366-
template <typename... Args> using disjunction = std::disjunction<Args...>;
367-
#else
368-
// Helper available in C++14.
369-
template <bool _Test, class _T1, class _T2>
370-
using conditional_t = typename std::conditional<_Test, _T1, _T2>::type;
371-
372-
template<class...> struct conjunction : std::true_type {};
373-
template<class B1> struct conjunction<B1> : B1 {};
374-
template<class B1, class... Bn>
375-
struct conjunction<B1, Bn...>
376-
: conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
377-
378-
template<class...> struct disjunction : std::false_type {};
379-
template<class B1> struct disjunction<B1> : B1 {};
380-
template<class B1, class... Bn>
381-
struct disjunction<B1, Bn...>
382-
: conditional_t<bool(B1::value), disjunction<Bn...>, B1> {};
383-
#endif
384-
385-
template<typename... Policies>
386-
struct isPolicy;
387-
388-
template<typename... Rest>
389-
struct isPolicy<return_value_policy::take_ownership, Rest...> {
390-
static constexpr bool value = true;
391-
};
392-
393-
template<typename... Rest>
394-
struct isPolicy<return_value_policy::reference, Rest...> {
395-
static constexpr bool value = true;
396-
};
397-
398-
template<typename... Rest>
399-
struct isPolicy<emscripten::async, Rest...> {
400-
static constexpr bool value = true;
401-
};
402-
403-
template <typename T, typename... Rest>
404-
struct isPolicy<emscripten::allow_raw_pointer<T>, Rest...> {
405-
static constexpr bool value = true;
406-
};
407-
408-
template<typename... Rest>
409-
struct isPolicy<allow_raw_pointers, Rest...> {
410-
static constexpr bool value = true;
411-
};
412-
413-
template<typename... Rest>
414-
struct isPolicy<emscripten::pure_virtual, Rest...> {
415-
static constexpr bool value = true;
416-
};
417-
418-
template<typename T, typename... Rest>
419-
struct isPolicy<emscripten::nonnull<T>, Rest...> {
420-
static constexpr bool value = true;
421-
};
422-
423-
template<typename T, typename... Rest>
424-
struct isPolicy<T, Rest...> {
425-
static constexpr bool value = isPolicy<Rest...>::value;
426-
};
427-
428-
template<>
429-
struct isPolicy<> {
430-
static constexpr bool value = false;
431-
};
432-
433-
template<typename ReturnType, typename... Rest>
434-
struct GetReturnValuePolicy {
435-
using tag = rvp::default_tag;
436-
};
437-
438-
template<typename ReturnType, typename... Rest>
439-
struct GetReturnValuePolicy<ReturnType, return_value_policy::take_ownership, Rest...> {
440-
using tag = rvp::take_ownership;
441-
};
442-
443-
template<typename ReturnType, typename... Rest>
444-
struct GetReturnValuePolicy<ReturnType, return_value_policy::reference, Rest...> {
445-
using tag = rvp::reference;
446-
};
447-
448-
template<typename ReturnType, typename T, typename... Rest>
449-
struct GetReturnValuePolicy<ReturnType, T, Rest...> {
450-
using tag = GetReturnValuePolicy<ReturnType, Rest...>::tag;
451-
};
452-
453-
template<typename... Policies>
454-
using isAsync = disjunction<std::is_same<async, Policies>...>;
455-
456-
template<typename... Policies>
457-
using isNonnullReturn = disjunction<std::is_same<nonnull<ret_val>, Policies>...>;
458-
459-
}
460-
461286
////////////////////////////////////////////////////////////////////////////////
462287
// select_overload and select_const
463288
////////////////////////////////////////////////////////////////////////////////

system/include/emscripten/wire.h

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,186 @@ struct BindingType<memory_view<ElementType>> {
492492
}
493493
};
494494

495+
}
496+
497+
////////////////////////////////////////////////////////////////////////////////
498+
// POLICIES
499+
////////////////////////////////////////////////////////////////////////////////
500+
501+
template<int Index>
502+
struct arg {
503+
static constexpr int index = Index + 1;
504+
};
505+
506+
struct ret_val {
507+
static constexpr int index = 0;
508+
};
509+
510+
/*
511+
template<typename Slot>
512+
struct allow_raw_pointer {
513+
template<typename InputType, int Index>
514+
struct Transform {
515+
typedef typename std::conditional<
516+
Index == Slot::index,
517+
internal::AllowedRawPointer<typename std::remove_pointer<InputType>::type>,
518+
InputType
519+
>::type type;
520+
};
521+
};
522+
*/
523+
524+
// allow all raw pointers
525+
struct allow_raw_pointers {
526+
template<typename InputType, int Index>
527+
struct Transform {
528+
// Use decay to handle references to pointers e.g.(T*&)->(T*).
529+
typedef typename std::decay<InputType>::type DecayedType;
530+
typedef typename std::conditional<
531+
std::is_pointer<DecayedType>::value,
532+
internal::AllowedRawPointer<typename std::remove_pointer<DecayedType>::type>,
533+
InputType
534+
>::type type;
535+
};
536+
};
537+
538+
// this is temporary until arg policies are reworked
539+
template<typename Slot>
540+
struct allow_raw_pointer : public allow_raw_pointers {
541+
};
542+
543+
struct async {
544+
template<typename InputType, int Index>
545+
struct Transform {
546+
typedef InputType type;
547+
};
548+
};
549+
550+
struct pure_virtual {
551+
template<typename InputType, int Index>
552+
struct Transform {
553+
typedef InputType type;
554+
};
555+
};
556+
557+
template<typename Slot>
558+
struct nonnull {
559+
static_assert(std::is_same<Slot, ret_val>::value, "Only nonnull return values are currently supported.");
560+
template<typename InputType, int Index>
561+
struct Transform {
562+
typedef InputType type;
563+
};
564+
};
565+
566+
namespace return_value_policy {
567+
568+
struct take_ownership : public allow_raw_pointers {};
569+
struct reference : public allow_raw_pointers {};
570+
571+
} // end namespace return_value_policy
572+
573+
namespace internal {
574+
575+
#if __cplusplus >= 201703L
576+
template <typename... Args> using conjunction = std::conjunction<Args...>;
577+
template <typename... Args> using disjunction = std::disjunction<Args...>;
578+
#else
579+
// Helper available in C++14.
580+
template <bool _Test, class _T1, class _T2>
581+
using conditional_t = typename std::conditional<_Test, _T1, _T2>::type;
582+
583+
template<class...> struct conjunction : std::true_type {};
584+
template<class B1> struct conjunction<B1> : B1 {};
585+
template<class B1, class... Bn>
586+
struct conjunction<B1, Bn...>
587+
: conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
588+
589+
template<class...> struct disjunction : std::false_type {};
590+
template<class B1> struct disjunction<B1> : B1 {};
591+
template<class B1, class... Bn>
592+
struct disjunction<B1, Bn...>
593+
: conditional_t<bool(B1::value), disjunction<Bn...>, B1> {};
594+
#endif
595+
596+
template<typename... Policies>
597+
struct isPolicy;
598+
599+
template<typename... Rest>
600+
struct isPolicy<return_value_policy::take_ownership, Rest...> {
601+
static constexpr bool value = true;
602+
};
603+
604+
template<typename... Rest>
605+
struct isPolicy<return_value_policy::reference, Rest...> {
606+
static constexpr bool value = true;
607+
};
608+
609+
template<typename... Rest>
610+
struct isPolicy<emscripten::async, Rest...> {
611+
static constexpr bool value = true;
612+
};
613+
614+
template <typename T, typename... Rest>
615+
struct isPolicy<emscripten::allow_raw_pointer<T>, Rest...> {
616+
static constexpr bool value = true;
617+
};
618+
619+
template<typename... Rest>
620+
struct isPolicy<allow_raw_pointers, Rest...> {
621+
static constexpr bool value = true;
622+
};
623+
624+
template<typename... Rest>
625+
struct isPolicy<emscripten::pure_virtual, Rest...> {
626+
static constexpr bool value = true;
627+
};
628+
629+
template<typename T, typename... Rest>
630+
struct isPolicy<emscripten::nonnull<T>, Rest...> {
631+
static constexpr bool value = true;
632+
};
633+
634+
template<typename T, typename... Rest>
635+
struct isPolicy<T, Rest...> {
636+
static constexpr bool value = isPolicy<Rest...>::value;
637+
};
638+
639+
template<>
640+
struct isPolicy<> {
641+
static constexpr bool value = false;
642+
};
643+
644+
template<typename T>
645+
struct isNotPolicy {
646+
static constexpr bool value = !isPolicy<T>::value;
647+
};
648+
649+
template<typename ReturnType, typename... Rest>
650+
struct GetReturnValuePolicy {
651+
using tag = rvp::default_tag;
652+
};
653+
654+
template<typename ReturnType, typename... Rest>
655+
struct GetReturnValuePolicy<ReturnType, return_value_policy::take_ownership, Rest...> {
656+
using tag = rvp::take_ownership;
657+
};
658+
659+
template<typename ReturnType, typename... Rest>
660+
struct GetReturnValuePolicy<ReturnType, return_value_policy::reference, Rest...> {
661+
using tag = rvp::reference;
662+
};
663+
664+
template<typename ReturnType, typename T, typename... Rest>
665+
struct GetReturnValuePolicy<ReturnType, T, Rest...> {
666+
using tag = GetReturnValuePolicy<ReturnType, Rest...>::tag;
667+
};
668+
669+
template<typename... Policies>
670+
using isAsync = disjunction<std::is_same<async, Policies>...>;
671+
672+
template<typename... Policies>
673+
using isNonnullReturn = disjunction<std::is_same<nonnull<ret_val>, Policies>...>;
674+
495675
} // namespace internal
496676

497677
} // namespace emscripten

0 commit comments

Comments
 (0)