2021-04-09 07:06:03 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2021-04-09 07:06:03 +00:00
|
|
|
*
|
|
|
|
* 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 <vapours.hpp>
|
|
|
|
#include <stratosphere/tipc/tipc_common.hpp>
|
|
|
|
#include <stratosphere/tipc/tipc_service_object.hpp>
|
|
|
|
|
|
|
|
namespace ams::tipc {
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
class ObjectHolder {
|
2021-04-09 07:06:03 +00:00
|
|
|
public:
|
|
|
|
enum ObjectType : u8 {
|
|
|
|
ObjectType_Invalid = 0,
|
|
|
|
ObjectType_Port = 1,
|
|
|
|
ObjectType_Session = 2,
|
|
|
|
};
|
|
|
|
private:
|
2021-10-05 00:12:32 +00:00
|
|
|
os::NativeHandle m_handle;
|
2021-04-09 07:06:03 +00:00
|
|
|
ObjectType m_type;
|
|
|
|
bool m_managed;
|
|
|
|
tipc::ServiceObjectBase *m_object;
|
|
|
|
private:
|
2021-10-05 00:12:32 +00:00
|
|
|
void InitializeImpl(ObjectType type, os::NativeHandle handle, bool managed, tipc::ServiceObjectBase *object) {
|
2021-04-09 07:06:03 +00:00
|
|
|
/* Validate that the object isn't already constructed. */
|
|
|
|
AMS_ASSERT(m_type == ObjectType_Invalid);
|
|
|
|
|
|
|
|
/* Set all fields. */
|
|
|
|
m_handle = handle;
|
|
|
|
m_type = type;
|
|
|
|
m_managed = managed;
|
|
|
|
m_object = object;
|
|
|
|
}
|
|
|
|
public:
|
2021-10-05 00:12:32 +00:00
|
|
|
constexpr inline ObjectHolder() : m_handle(os::InvalidNativeHandle), m_type(ObjectType_Invalid), m_managed(false), m_object(nullptr) { /* ... */ }
|
2021-04-09 07:06:03 +00:00
|
|
|
|
2021-10-05 00:12:32 +00:00
|
|
|
void InitializeAsPort(os::NativeHandle handle) {
|
2021-04-09 07:06:03 +00:00
|
|
|
/* NOTE: Nintendo sets ports as managed, but this will cause a nullptr-deref if one is ever closed. */
|
|
|
|
/* This is theoretically a non-issue, as ports can't be closed, but we will set ours as unmanaged, */
|
|
|
|
/* just in case. */
|
|
|
|
this->InitializeImpl(ObjectType_Port, handle, false, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-10-05 00:12:32 +00:00
|
|
|
void InitializeAsSession(os::NativeHandle handle, bool managed, tipc::ServiceObjectBase *object) {
|
2021-04-09 07:06:03 +00:00
|
|
|
this->InitializeImpl(ObjectType_Session, handle, managed, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Destroy() {
|
|
|
|
/* Validate that the object is constructed. */
|
|
|
|
AMS_ASSERT(m_type != ObjectType_Invalid);
|
|
|
|
|
|
|
|
/* If we're managed, destroy the associated object. */
|
|
|
|
if (m_managed) {
|
|
|
|
if (auto * const deleter = m_object->GetDeleter(); deleter != nullptr) {
|
|
|
|
deleter->DeleteServiceObject(m_object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset all fields. */
|
2021-10-05 00:12:32 +00:00
|
|
|
m_handle = os::InvalidNativeHandle;
|
2021-04-09 07:06:03 +00:00
|
|
|
m_type = ObjectType_Invalid;
|
|
|
|
m_managed = false;
|
|
|
|
m_object = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-05 00:12:32 +00:00
|
|
|
constexpr os::NativeHandle GetHandle() const {
|
2021-04-09 07:06:03 +00:00
|
|
|
return m_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ObjectType GetType() const {
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr tipc::ServiceObjectBase *GetObject() const {
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|