subrepo:
  subdir:   "libraries"
  merged:   "07af583b"
upstream:
  origin:   "https://github.com/Atmosphere-NX/Atmosphere-libs"
  branch:   "master"
  commit:   "07af583b"
git-subrepo:
  version:  "0.4.0"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "5d6aba9"
This commit is contained in:
Michael Scire
2019-12-09 03:57:37 -08:00
committed by SciresM
parent 28717bfd27
commit 0105455086
294 changed files with 29915 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../sf_common.hpp"
#include "sf_cmif_service_object_holder.hpp"
namespace ams::sf::cmif {
struct DomainObjectId {
u32 value;
constexpr void SetValue(u32 new_value) { this->value = new_value; }
};
static_assert(std::is_trivial<DomainObjectId>::value && sizeof(DomainObjectId) == sizeof(u32), "DomainObjectId");
inline constexpr bool operator==(const DomainObjectId &lhs, const DomainObjectId &rhs) {
return lhs.value == rhs.value;
}
inline constexpr bool operator!=(const DomainObjectId &lhs, const DomainObjectId &rhs) {
return lhs.value != rhs.value;
}
inline constexpr bool operator<(const DomainObjectId &lhs, const DomainObjectId &rhs) {
return lhs.value < rhs.value;
}
inline constexpr bool operator<=(const DomainObjectId &lhs, const DomainObjectId &rhs) {
return lhs.value <= rhs.value;
}
inline constexpr bool operator>(const DomainObjectId &lhs, const DomainObjectId &rhs) {
return lhs.value > rhs.value;
}
inline constexpr bool operator>=(const DomainObjectId &lhs, const DomainObjectId &rhs) {
return lhs.value >= rhs.value;
}
constexpr inline const DomainObjectId InvalidDomainObjectId = { .value = 0 };
class ServerDomainBase {
public:
virtual Result ReserveIds(DomainObjectId *out_ids, size_t count) = 0;
virtual void ReserveSpecificIds(const DomainObjectId *ids, size_t count) = 0;
virtual void UnreserveIds(const DomainObjectId *ids, size_t count) = 0;
virtual void RegisterObject(DomainObjectId id, ServiceObjectHolder &&obj) = 0;
virtual ServiceObjectHolder UnregisterObject(DomainObjectId id) = 0;
virtual ServiceObjectHolder GetObject(DomainObjectId id) = 0;
};
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../sf_common.hpp"
#include "sf_cmif_domain_api.hpp"
#include "sf_cmif_domain_service_object.hpp"
namespace ams::sf::cmif {
class ServerDomainManager {
NON_COPYABLE(ServerDomainManager);
NON_MOVEABLE(ServerDomainManager);
private:
class Domain;
struct Entry {
NON_COPYABLE(Entry);
NON_MOVEABLE(Entry);
util::IntrusiveListNode free_list_node;
util::IntrusiveListNode domain_list_node;
Domain *owner;
ServiceObjectHolder object;
explicit Entry() : owner(nullptr) { /* ... */ }
};
class Domain final : public DomainServiceObject {
NON_COPYABLE(Domain);
NON_MOVEABLE(Domain);
private:
using EntryList = typename util::IntrusiveListMemberTraits<&Entry::domain_list_node>::ListType;
private:
ServerDomainManager *manager;
EntryList entries;
public:
explicit Domain(ServerDomainManager *m) : manager(m) { /* ... */ }
~Domain();
void DestroySelf();
virtual ServerDomainBase *GetServerDomain() override final {
return static_cast<ServerDomainBase *>(this);
}
virtual Result ReserveIds(DomainObjectId *out_ids, size_t count) override final;
virtual void ReserveSpecificIds(const DomainObjectId *ids, size_t count) override final;
virtual void UnreserveIds(const DomainObjectId *ids, size_t count) override final;
virtual void RegisterObject(DomainObjectId id, ServiceObjectHolder &&obj) override final;
virtual ServiceObjectHolder UnregisterObject(DomainObjectId id) override final;
virtual ServiceObjectHolder GetObject(DomainObjectId id) override final;
};
public:
using DomainEntryStorage = TYPED_STORAGE(Entry);
using DomainStorage = TYPED_STORAGE(Domain);
private:
class EntryManager {
private:
using EntryList = typename util::IntrusiveListMemberTraits<&Entry::free_list_node>::ListType;
private:
os::Mutex lock;
EntryList free_list;
Entry *entries;
size_t num_entries;
public:
EntryManager(DomainEntryStorage *entry_storage, size_t entry_count);
~EntryManager();
Entry *AllocateEntry();
void FreeEntry(Entry *);
void AllocateSpecificEntries(const DomainObjectId *ids, size_t count);
inline DomainObjectId GetId(Entry *e) {
const size_t index = e - this->entries;
AMS_ASSERT(index < this->num_entries);
return DomainObjectId{ u32(index + 1) };
}
inline Entry *GetEntry(DomainObjectId id) {
if (id == InvalidDomainObjectId) {
return nullptr;
}
const size_t index = id.value - 1;
if (!(index < this->num_entries)) {
return nullptr;
}
return this->entries + index;
}
};
private:
os::Mutex entry_owner_lock;
EntryManager entry_manager;
private:
virtual void *AllocateDomain() = 0;
virtual void FreeDomain(void *) = 0;
protected:
ServerDomainManager(DomainEntryStorage *entry_storage, size_t entry_count) : entry_manager(entry_storage, entry_count) { /* ... */ }
inline DomainServiceObject *AllocateDomainServiceObject() {
void *storage = this->AllocateDomain();
if (storage == nullptr) {
return nullptr;
}
return new (storage) Domain(this);
}
public:
static void DestroyDomainServiceObject(DomainServiceObject *obj) {
static_cast<Domain *>(obj)->DestroySelf();
}
};
}

View File

@@ -0,0 +1,136 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "sf_cmif_service_dispatch.hpp"
#include "sf_cmif_domain_api.hpp"
#include "sf_cmif_server_message_processor.hpp"
namespace ams::sf::cmif {
class DomainServiceObjectDispatchTable : public impl::ServiceDispatchTableBase {
private:
Result ProcessMessageImpl(ServiceDispatchContext &ctx, ServerDomainBase *domain, const cmif::PointerAndSize &in_raw_data) const;
Result ProcessMessageForMitmImpl(ServiceDispatchContext &ctx, ServerDomainBase *domain, const cmif::PointerAndSize &in_raw_data) const;
public:
Result ProcessMessage(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const;
Result ProcessMessageForMitm(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const;
};
class DomainServiceObjectProcessor : public ServerMessageProcessor {
private:
ServerMessageProcessor *impl_processor;
ServerDomainBase *domain;
DomainObjectId *in_object_ids;
DomainObjectId *out_object_ids;
size_t num_in_objects;
ServerMessageRuntimeMetadata impl_metadata;
public:
DomainServiceObjectProcessor(ServerDomainBase *d, DomainObjectId *in_obj_ids, size_t num_in_objs) : domain(d), in_object_ids(in_obj_ids), num_in_objects(num_in_objs) {
AMS_ASSERT(this->domain != nullptr);
AMS_ASSERT(this->in_object_ids != nullptr);
this->impl_processor = nullptr;
this->out_object_ids = nullptr;
this->impl_metadata = {};
}
constexpr size_t GetInObjectCount() const {
return this->num_in_objects;
}
constexpr size_t GetOutObjectCount() const {
return this->impl_metadata.GetOutObjectCount();
}
constexpr size_t GetImplOutHeadersSize() const {
return this->impl_metadata.GetOutHeadersSize();
}
constexpr size_t GetImplOutDataTotalSize() const {
return this->impl_metadata.GetOutDataSize() + this->impl_metadata.GetOutHeadersSize();
}
public:
/* Used to enabled templated message processors. */
virtual void SetImplementationProcessor(ServerMessageProcessor *impl) override final {
if (this->impl_processor == nullptr) {
this->impl_processor = impl;
} else {
this->impl_processor->SetImplementationProcessor(impl);
}
this->impl_metadata = this->impl_processor->GetRuntimeMetadata();
}
virtual const ServerMessageRuntimeMetadata GetRuntimeMetadata() const override final {
const auto runtime_metadata = this->impl_processor->GetRuntimeMetadata();
return ServerMessageRuntimeMetadata {
.in_data_size = static_cast<u16>(runtime_metadata.GetInDataSize() + runtime_metadata.GetInObjectCount() * sizeof(DomainObjectId)),
.out_data_size = static_cast<u16>(runtime_metadata.GetOutDataSize() + runtime_metadata.GetOutObjectCount() * sizeof(DomainObjectId)),
.in_headers_size = static_cast<u8>(runtime_metadata.GetInHeadersSize() + sizeof(CmifDomainInHeader)),
.out_headers_size = static_cast<u8>(runtime_metadata.GetOutHeadersSize() + sizeof(CmifDomainOutHeader)),
.in_object_count = 0,
.out_object_count = 0,
};
}
virtual Result PrepareForProcess(const ServiceDispatchContext &ctx, const ServerMessageRuntimeMetadata runtime_metadata) const override final;
virtual Result GetInObjects(ServiceObjectHolder *in_objects) const override final;
virtual HipcRequest PrepareForReply(const cmif::ServiceDispatchContext &ctx, PointerAndSize &out_raw_data, const ServerMessageRuntimeMetadata runtime_metadata) override final;
virtual void PrepareForErrorReply(const cmif::ServiceDispatchContext &ctx, PointerAndSize &out_raw_data, const ServerMessageRuntimeMetadata runtime_metadata) override final;
virtual void SetOutObjects(const cmif::ServiceDispatchContext &ctx, const HipcRequest &response, ServiceObjectHolder *out_objects, DomainObjectId *ids) override final;
};
class DomainServiceObject : public IServiceObject, public ServerDomainBase {
friend class DomainServiceObjectDispatchTable;
public:
static constexpr inline DomainServiceObjectDispatchTable s_CmifServiceDispatchTable{};
private:
virtual ServerDomainBase *GetServerDomain() = 0;
public:
/* TODO: Implement to use domain object processor. */
};
class MitmDomainServiceObject : public DomainServiceObject{};
static_assert(sizeof(DomainServiceObject) == sizeof(MitmDomainServiceObject));
template<>
struct ServiceDispatchTraits<DomainServiceObject> {
static_assert(std::is_base_of<sf::IServiceObject, DomainServiceObject>::value, "DomainServiceObject must derive from sf::IServiceObject");
static_assert(!std::is_base_of<sf::IMitmServiceObject, DomainServiceObject>::value, "DomainServiceObject must not derive from sf::IMitmServiceObject");
using ProcessHandlerType = decltype(ServiceDispatchMeta::ProcessHandler);
using DispatchTableType = DomainServiceObjectDispatchTable;
static constexpr ProcessHandlerType ProcessHandlerImpl = &impl::ServiceDispatchTableBase::ProcessMessage<DispatchTableType>;
static constexpr inline ServiceDispatchMeta Meta{&DomainServiceObject::s_CmifServiceDispatchTable, ProcessHandlerImpl};
};
template<>
struct ServiceDispatchTraits<MitmDomainServiceObject> {
static_assert(std::is_base_of<DomainServiceObject, MitmDomainServiceObject>::value, "MitmDomainServiceObject must derive from DomainServiceObject");
using ProcessHandlerType = decltype(ServiceDispatchMeta::ProcessHandler);
using DispatchTableType = DomainServiceObjectDispatchTable;
static constexpr ProcessHandlerType ProcessHandlerImpl = &impl::ServiceDispatchTableBase::ProcessMessageForMitm<DispatchTableType>;
static constexpr inline ServiceDispatchMeta Meta{&DomainServiceObject::s_CmifServiceDispatchTable, ProcessHandlerImpl};
};
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../sf_common.hpp"
namespace ams::sf::cmif {
class PointerAndSize {
private:
uintptr_t pointer;
size_t size;
public:
constexpr PointerAndSize() : pointer(0), size(0) { /* ... */ }
constexpr PointerAndSize(uintptr_t ptr, size_t sz) : pointer(ptr), size(sz) { /* ... */ }
constexpr PointerAndSize(void *ptr, size_t sz) : PointerAndSize(reinterpret_cast<uintptr_t>(ptr), sz) { /* ... */ }
constexpr void *GetPointer() const {
return reinterpret_cast<void *>(this->pointer);
}
constexpr uintptr_t GetAddress() const {
return this->pointer;
}
constexpr size_t GetSize() const {
return this->size;
}
};
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../sf_service_object.hpp"
#include "sf_cmif_pointer_and_size.hpp"
namespace ams::sf::cmif {
/* Forward declare ServiceDispatchContext, ServiceObjectHolder. */
struct ServiceDispatchContext;
class ServiceObjectHolder;
struct DomainObjectId;
/* This is needed for non-templated domain message processing. */
struct ServerMessageRuntimeMetadata {
u16 in_data_size;
u16 out_data_size;
u8 in_headers_size;
u8 out_headers_size;
u8 in_object_count;
u8 out_object_count;
constexpr size_t GetInDataSize() const {
return size_t(this->in_data_size);
}
constexpr size_t GetOutDataSize() const {
return size_t(this->out_data_size);
}
constexpr size_t GetInHeadersSize() const {
return size_t(this->in_headers_size);
}
constexpr size_t GetOutHeadersSize() const {
return size_t(this->out_headers_size);
}
constexpr size_t GetInObjectCount() const {
return size_t(this->in_object_count);
}
constexpr size_t GetOutObjectCount() const {
return size_t(this->out_object_count);
}
constexpr size_t GetUnfixedOutPointerSizeOffset() const {
return this->GetInDataSize() + this->GetInHeadersSize() + 0x10 /* padding. */;
}
};
static_assert(std::is_pod<ServerMessageRuntimeMetadata>::value, "std::is_pod<ServerMessageRuntimeMetadata>::value");
static_assert(sizeof(ServerMessageRuntimeMetadata) == sizeof(u64), "sizeof(ServerMessageRuntimeMetadata)");
class ServerMessageProcessor {
public:
/* Used to enabled templated message processors. */
virtual void SetImplementationProcessor(ServerMessageProcessor *impl) = 0;
virtual const ServerMessageRuntimeMetadata GetRuntimeMetadata() const = 0;
virtual Result PrepareForProcess(const ServiceDispatchContext &ctx, const ServerMessageRuntimeMetadata runtime_metadata) const = 0;
virtual Result GetInObjects(ServiceObjectHolder *in_objects) const = 0;
virtual HipcRequest PrepareForReply(const cmif::ServiceDispatchContext &ctx, PointerAndSize &out_raw_data, const ServerMessageRuntimeMetadata runtime_metadata) = 0;
virtual void PrepareForErrorReply(const cmif::ServiceDispatchContext &ctx, PointerAndSize &out_raw_data, const ServerMessageRuntimeMetadata runtime_metadata) = 0;
virtual void SetOutObjects(const cmif::ServiceDispatchContext &ctx, const HipcRequest &response, ServiceObjectHolder *out_objects, DomainObjectId *ids) = 0;
};
}

View File

@@ -0,0 +1,150 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../sf_service_object.hpp"
#include "sf_cmif_pointer_and_size.hpp"
#include "sf_cmif_server_message_processor.hpp"
namespace ams::sf::hipc {
class ServerSessionManager;
class ServerSession;
}
namespace ams::sf::cmif {
class ServerMessageProcessor;
struct HandlesToClose {
Handle handles[8];
size_t num_handles;
};
struct ServiceDispatchContext {
sf::IServiceObject *srv_obj;
hipc::ServerSessionManager *manager;
hipc::ServerSession *session;
ServerMessageProcessor *processor;
HandlesToClose *handles_to_close;
const PointerAndSize pointer_buffer;
const PointerAndSize in_message_buffer;
const PointerAndSize out_message_buffer;
const HipcParsedRequest request;
};
struct ServiceCommandMeta {
hos::Version hosver_low;
hos::Version hosver_high;
u32 cmd_id;
Result (*handler)(CmifOutHeader **out_header_ptr, ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data);
constexpr inline bool Matches(u32 cmd_id, hos::Version hosver) const {
return this->cmd_id == cmd_id && this->hosver_low <= hosver && hosver <= this->hosver_high;
}
constexpr inline decltype(handler) GetHandler() const {
return this->handler;
}
};
static_assert(std::is_pod<ServiceCommandMeta>::value && sizeof(ServiceCommandMeta) == 0x10, "sizeof(ServiceCommandMeta)");
namespace impl {
class ServiceDispatchTableBase {
protected:
Result ProcessMessageImpl(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data, const ServiceCommandMeta *entries, const size_t entry_count) const;
Result ProcessMessageForMitmImpl(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data, const ServiceCommandMeta *entries, const size_t entry_count) const;
public:
/* CRTP. */
template<typename T>
Result ProcessMessage(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const {
static_assert(std::is_base_of<ServiceDispatchTableBase, T>::value, "ServiceDispatchTableBase::Process<T>");
return static_cast<const T *>(this)->ProcessMessage(ctx, in_raw_data);
}
template<typename T>
Result ProcessMessageForMitm(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const {
static_assert(std::is_base_of<ServiceDispatchTableBase, T>::value, "ServiceDispatchTableBase::ProcessForMitm<T>");
return static_cast<const T *>(this)->ProcessMessageForMitm(ctx, in_raw_data);
}
};
template<size_t N, class = std::make_index_sequence<N>>
class ServiceDispatchTableImpl;
template<size_t N, size_t... Is>
class ServiceDispatchTableImpl<N, std::index_sequence<Is...>> : public ServiceDispatchTableBase {
private:
template<size_t>
using EntryType = ServiceCommandMeta;
private:
const std::array<ServiceCommandMeta, N> entries;
public:
explicit constexpr ServiceDispatchTableImpl(EntryType<Is>... args) : entries { args... } { /* ... */ }
Result ProcessMessage(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const {
return this->ProcessMessageImpl(ctx, in_raw_data, this->entries.data(), this->entries.size());
}
Result ProcessMessageForMitm(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const {
return this->ProcessMessageForMitmImpl(ctx, in_raw_data, this->entries.data(), this->entries.size());
}
};
}
template<typename ...Entries>
class ServiceDispatchTable : public impl::ServiceDispatchTableImpl<sizeof...(Entries)> {
public:
explicit constexpr ServiceDispatchTable(Entries... entries) : impl::ServiceDispatchTableImpl<sizeof...(Entries)>(entries...) { /* ... */ }
};
#define DEFINE_SERVICE_DISPATCH_TABLE \
template<typename ServiceImpl> \
static constexpr inline ::ams::sf::cmif::ServiceDispatchTable s_CmifServiceDispatchTable
struct ServiceDispatchMeta {
const impl::ServiceDispatchTableBase *DispatchTable;
Result (impl::ServiceDispatchTableBase::*ProcessHandler)(ServiceDispatchContext &, const cmif::PointerAndSize &) const;
constexpr uintptr_t GetServiceId() const {
return reinterpret_cast<uintptr_t>(this->DispatchTable);
}
};
template<typename T>
struct ServiceDispatchTraits {
static_assert(std::is_base_of<sf::IServiceObject, T>::value, "ServiceObjects must derive from sf::IServiceObject");
using ProcessHandlerType = decltype(ServiceDispatchMeta::ProcessHandler);
static constexpr inline auto DispatchTable = T::template s_CmifServiceDispatchTable<T>;
using DispatchTableType = decltype(DispatchTable);
static constexpr ProcessHandlerType ProcessHandlerImpl = ServiceObjectTraits<T>::IsMitmServiceObject ? (&impl::ServiceDispatchTableBase::ProcessMessageForMitm<DispatchTableType>)
: (&impl::ServiceDispatchTableBase::ProcessMessage<DispatchTableType>);
static constexpr inline ServiceDispatchMeta Meta{&DispatchTable, ProcessHandlerImpl};
};
template<typename T>
NX_CONSTEXPR const ServiceDispatchMeta *GetServiceDispatchMeta() {
return &ServiceDispatchTraits<T>::Meta;
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../sf_service_object.hpp"
#include "sf_cmif_service_dispatch.hpp"
namespace ams::sf::cmif {
class ServiceObjectHolder {
private:
std::shared_ptr<sf::IServiceObject> srv;
const ServiceDispatchMeta *dispatch_meta;
private:
/* Copy constructor. */
ServiceObjectHolder(const ServiceObjectHolder &o) : srv(o.srv), dispatch_meta(o.dispatch_meta) { /* ... */ }
ServiceObjectHolder &operator=(const ServiceObjectHolder &o) = delete;
public:
/* Default constructor, null all members. */
ServiceObjectHolder() : srv(nullptr), dispatch_meta(nullptr) { /* ... */ }
~ServiceObjectHolder() {
this->dispatch_meta = nullptr;
}
/* Ensure correct type id at runtime through template constructor. */
template<typename ServiceImpl>
constexpr explicit ServiceObjectHolder(std::shared_ptr<ServiceImpl> &&s) {
this->srv = std::move(s);
this->dispatch_meta = GetServiceDispatchMeta<ServiceImpl>();
}
/* Move constructor, assignment operator. */
ServiceObjectHolder(ServiceObjectHolder &&o) : srv(std::move(o.srv)), dispatch_meta(std::move(o.dispatch_meta)) {
o.dispatch_meta = nullptr;
}
ServiceObjectHolder &operator=(ServiceObjectHolder &&o) {
ServiceObjectHolder tmp(std::move(o));
tmp.Swap(*this);
return *this;
}
/* State management. */
void Swap(ServiceObjectHolder &o) {
std::swap(this->srv, o.srv);
std::swap(this->dispatch_meta, o.dispatch_meta);
}
void Reset() {
this->srv = nullptr;
this->dispatch_meta = nullptr;
}
ServiceObjectHolder Clone() const {
return ServiceObjectHolder(*this);
}
/* Boolean operators. */
explicit constexpr operator bool() const {
return this->dispatch_meta != nullptr;
}
constexpr bool operator!() const {
return this->dispatch_meta == nullptr;
}
/* Getters. */
constexpr uintptr_t GetServiceId() const {
if (this->dispatch_meta) {
return this->dispatch_meta->GetServiceId();
}
return 0;
}
template<typename ServiceImpl>
constexpr inline bool IsServiceObjectValid() const {
return this->GetServiceId() == GetServiceDispatchMeta<ServiceImpl>()->GetServiceId();
}
template<typename ServiceImpl>
inline std::shared_ptr<ServiceImpl> GetServiceObject() const {
if (this->GetServiceId() == GetServiceDispatchMeta<ServiceImpl>()->GetServiceId()) {
return std::static_pointer_cast<ServiceImpl>(this->srv);
}
return nullptr;
}
inline sf::IServiceObject *GetServiceObjectUnsafe() const {
return this->srv.get();
}
/* Processing. */
Result ProcessMessage(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const;
};
}