Atmosphere/libraries/libstratosphere/include/stratosphere/ddsf/ddsf_device_code_entry.hpp

111 lines
3.6 KiB
C++
Raw Normal View History

2020-10-30 22:36:11 +00:00
/*
* Copyright (c) Atmosphère-NX
2020-10-30 22:36:11 +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>
namespace ams::ddsf {
class IDevice;
class DeviceCodeEntry {
NON_COPYABLE(DeviceCodeEntry);
NON_MOVEABLE(DeviceCodeEntry);
private:
2021-10-10 07:14:06 +00:00
ams::DeviceCode m_device_code = ams::InvalidDeviceCode;
IDevice *m_device = nullptr;
2020-10-30 22:36:11 +00:00
public:
2021-10-10 07:14:06 +00:00
constexpr DeviceCodeEntry(ams::DeviceCode dc, IDevice *dev) : m_device_code(dc), m_device(dev) {
2020-10-30 22:36:11 +00:00
AMS_ASSERT(dev != nullptr);
}
constexpr ams::DeviceCode GetDeviceCode() const {
2021-10-10 07:14:06 +00:00
return m_device_code;
2020-10-30 22:36:11 +00:00
}
constexpr IDevice &GetDevice() {
2021-10-10 07:14:06 +00:00
return *m_device;
2020-10-30 22:36:11 +00:00
}
constexpr const IDevice &GetDevice() const {
2021-10-10 07:14:06 +00:00
return *m_device;
2020-10-30 22:36:11 +00:00
}
};
class DeviceCodeEntryHolder {
NON_COPYABLE(DeviceCodeEntryHolder);
NON_MOVEABLE(DeviceCodeEntryHolder);
private:
2021-10-10 07:14:06 +00:00
util::IntrusiveListNode m_list_node;
util::TypedStorage<DeviceCodeEntry> m_entry_storage;
bool m_is_constructed;
2020-10-30 22:36:11 +00:00
public:
2021-10-10 07:14:06 +00:00
using ListTraits = util::IntrusiveListMemberTraitsDeferredAssert<&DeviceCodeEntryHolder::m_list_node>;
2020-10-30 22:36:11 +00:00
using List = typename ListTraits::ListType;
2021-10-10 07:14:06 +00:00
friend class util::IntrusiveList<DeviceCodeEntryHolder, util::IntrusiveListMemberTraitsDeferredAssert<&DeviceCodeEntryHolder::m_list_node>>;
2020-10-30 22:36:11 +00:00
public:
2021-10-10 07:14:06 +00:00
DeviceCodeEntryHolder() : m_list_node(), m_entry_storage(), m_is_constructed(false) {
2020-10-30 22:36:11 +00:00
/* ... */
}
~DeviceCodeEntryHolder() {
if (this->IsConstructed()) {
this->Destroy();
}
}
void AddTo(List &list) {
list.push_back(*this);
}
void RemoveFrom(List list) {
list.erase(list.iterator_to(*this));
}
bool IsLinkedToList() const {
2021-10-10 07:14:06 +00:00
return m_list_node.IsLinked();
2020-10-30 22:36:11 +00:00
}
DeviceCodeEntry &Construct(DeviceCode dc, IDevice *dev) {
AMS_ASSERT(!this->IsConstructed());
2021-10-10 07:14:06 +00:00
DeviceCodeEntry *entry = util::ConstructAt(m_entry_storage, dc, dev);
m_is_constructed = true;
2020-10-30 22:36:11 +00:00
return *entry;
}
bool IsConstructed() const {
2021-10-10 07:14:06 +00:00
return m_is_constructed;
2020-10-30 22:36:11 +00:00
}
void Destroy() {
AMS_ASSERT(this->IsConstructed());
2021-10-10 07:14:06 +00:00
util::DestroyAt(m_entry_storage);
m_is_constructed = false;
2020-10-30 22:36:11 +00:00
}
DeviceCodeEntry &Get() {
AMS_ASSERT(this->IsConstructed());
2021-10-10 07:14:06 +00:00
return GetReference(m_entry_storage);
2020-10-30 22:36:11 +00:00
}
const DeviceCodeEntry &Get() const {
AMS_ASSERT(this->IsConstructed());
2021-10-10 07:14:06 +00:00
return GetReference(m_entry_storage);
2020-10-30 22:36:11 +00:00
}
};
static_assert(DeviceCodeEntryHolder::ListTraits::IsValid());
}