sf: support service objects which must themselves be shared pointers

This commit is contained in:
Michael Scire 2020-07-08 15:07:40 -07:00
parent aa2d1e15ab
commit 2d9f5b6942
2 changed files with 42 additions and 1 deletions

View file

@ -125,6 +125,13 @@ namespace ams::sf::impl {
return static_cast<ImplPointer *>(_this)->NAME(std::forward<Arguments>(args)...); \
}
#define AMS_SF_IMPL_DECLARE_INTERFACE_FUNCTION_INVOKER_SHARED_POINTER(CLASSNAME, CMD_ID, RETURN, NAME, ARGS, VERSION_MIN, VERSION_MAX) \
template<typename ...Arguments> \
requires std::same_as<std::tuple<Arguments...>, AMS_SF_IMPL_HELPER_FUNCTION_ARGS(CLASSNAME, NAME)> \
static RETURN NAME##Invoker (CLASSNAME *_this, Arguments &&... args) { \
return static_cast<ImplSharedPointer *>(_this)->NAME(std::forward<Arguments>(args)...); \
}
#define AMS_SF_IMPL_DECLARE_INTERFACE_FUNCTION_IMPL(CLASSNAME, CMD_ID, RETURN, NAME, ARGS, VERSION_MIN, VERSION_MAX) \
template<typename ...Arguments> \
requires (std::same_as<std::tuple<Arguments...>, AMS_SF_IMPL_HELPER_FUNCTION_ARGS(CLASSNAME, NAME)> && \
@ -241,6 +248,29 @@ namespace ams::sf::impl {
static constexpr CommandPointerTable CommandPointerTableImpl = { \
CMD_MACRO(CLASSNAME, AMS_SF_IMPL_DEFINE_INTERFACE_COMMAND_POINTER_TABLE_MEMBER) \
}; \
}; \
\
class ImplSharedPointer : public S { \
private: \
std::shared_ptr<T> impl; \
public: \
constexpr ImplSharedPointer(std::shared_ptr<T> &&t) \
: S(std::addressof(CommandPointerTableImpl)), impl(std::move(t)) \
{ \
/* ... */ \
} \
ALWAYS_INLINE T &GetImpl() { return *this->impl; } \
ALWAYS_INLINE const T &GetImpl() const { return *this->impl; } \
private: \
CMD_MACRO(CLASSNAME, AMS_SF_IMPL_DECLARE_INTERFACE_FUNCTION_INVOKER_SHARED_POINTER) \
public: \
CMD_MACRO(CLASSNAME, AMS_SF_IMPL_DECLARE_INTERFACE_FUNCTION_IMPL_PTR) \
private: \
CMD_MACRO(CLASSNAME, AMS_SF_IMPL_DEFINE_INTERFACE_IMPL_FUNCTION_POINTER_HOLDER) \
public: \
static constexpr CommandPointerTable CommandPointerTableImpl = { \
CMD_MACRO(CLASSNAME, AMS_SF_IMPL_DEFINE_INTERFACE_COMMAND_POINTER_TABLE_MEMBER) \
}; \
}; \
static_assert(Is##CLASSNAME<ImplHolder>); \
}; \
@ -253,6 +283,10 @@ namespace ams::sf::impl {
template<typename T> requires (!std::same_as<CLASSNAME, T>&& Is##CLASSNAME<T>) \
using ImplPointer = typename ImplGenerator<CLASSNAME, T>::ImplPointer; \
\
template<typename T> requires (!std::same_as<CLASSNAME, T>&& Is##CLASSNAME<T> && \
std::derived_from<T, std::enable_shared_from_this<T>>) \
using ImplSharedPointer = typename ImplGenerator<CLASSNAME, T>::ImplSharedPointer; \
\
AMS_SF_CMIF_IMPL_DEFINE_SERVICE_DISPATCH_TABLE { \
CMD_MACRO(CLASSNAME, AMS_SF_IMPL_DEFINE_CMIF_SERVICE_COMMAND_META_TABLE_ENTRY) \
}; \

View file

@ -50,11 +50,18 @@ namespace ams::sf {
{ T::ShouldMitm(c) } -> std::same_as<bool>;
};
template<typename Interface, typename Impl, typename... Arguments> requires std::constructible_from<Impl, Arguments...>
template<typename Interface, typename Impl, typename... Arguments>
requires std::constructible_from<Impl, Arguments...>
constexpr ALWAYS_INLINE std::shared_ptr<typename Interface::ImplHolder<Impl>> MakeShared(Arguments &&... args) {
return std::make_shared<typename Interface::ImplHolder<Impl>>(std::forward<Arguments>(args)...);
}
template<typename Interface, typename Impl, typename... Arguments>
requires (std::constructible_from<Impl, Arguments...> && std::derived_from<Impl, std::enable_shared_from_this<Impl>>)
constexpr ALWAYS_INLINE std::shared_ptr<typename Interface::ImplSharedPointer<Impl>> MakeShared(Arguments &&... args) {
return std::make_shared<typename Interface::ImplSharedPointer<Impl>>(std::make_shared<Impl>(std::forward<Arguments>(args)...));
}
template<typename Interface, typename Impl>
constexpr ALWAYS_INLINE std::shared_ptr<typename Interface::ImplPointer<Impl>> GetSharedPointerTo(Impl *impl) {
return std::make_shared<typename Interface::ImplPointer<Impl>>(impl);