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

100 lines
3.4 KiB
C++
Raw Normal View History

2020-10-30 23:36:11 +01:00
/*
* Copyright (c) Atmosphère-NX
2020-10-30 23:36:11 +01: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/ddsf/ddsf_types.hpp>
#include <stratosphere/ddsf/impl/ddsf_for_each.hpp>
#include <stratosphere/ddsf/ddsf_i_castable.hpp>
namespace ams::ddsf {
class ISession;
class IDevice;
Result OpenSession(IDevice *device, ISession *session, AccessMode access_mode);
void CloseSession(ISession *session);
class ISession : public ICastable {
friend Result OpenSession(IDevice *device, ISession *session, AccessMode mode);
friend void CloseSession(ISession *session);
public:
AMS_DDSF_CASTABLE_ROOT_TRAITS(ams::ddsf::IDevice);
private:
2021-10-10 09:14:06 +02:00
util::IntrusiveListNode m_list_node;
IDevice *m_device;
AccessMode m_access_mode;
2020-10-30 23:36:11 +01:00
public:
using ListTraits = util::IntrusiveListMemberTraits<&ISession::m_list_node>;
2020-10-30 23:36:11 +01:00
using List = typename ListTraits::ListType;
friend class util::IntrusiveList<ISession, util::IntrusiveListMemberTraits<&ISession::m_list_node>>;
2020-10-30 23:36:11 +01:00
private:
void AttachDevice(IDevice *dev, AccessMode mode) {
AMS_ASSERT(dev != nullptr);
AMS_ASSERT(!this->IsOpen());
2021-10-10 09:14:06 +02:00
m_device = dev;
m_access_mode = mode;
2020-10-30 23:36:11 +01:00
AMS_ASSERT(this->IsOpen());
}
void DetachDevice() {
AMS_ASSERT(this->IsOpen());
2021-10-10 09:14:06 +02:00
m_device = nullptr;
m_access_mode = AccessMode_None;
2020-10-30 23:36:11 +01:00
AMS_ASSERT(!this->IsOpen());
}
public:
2021-10-10 09:14:06 +02:00
ISession() : m_list_node(), m_device(nullptr), m_access_mode() { /* ... */ }
2020-10-30 23:36:11 +01:00
protected:
~ISession() { this->DetachDevice(); AMS_ASSERT(!this->IsOpen()); }
public:
void AddTo(List &list) {
list.push_back(*this);
}
void RemoveFrom(List list) {
list.erase(list.iterator_to(*this));
}
bool IsLinkedToList() const {
2021-10-10 09:14:06 +02:00
return m_list_node.IsLinked();
2020-10-30 23:36:11 +01:00
}
IDevice &GetDevice() {
AMS_ASSERT(this->IsOpen());
2021-10-10 09:14:06 +02:00
return *m_device;
2020-10-30 23:36:11 +01:00
}
const IDevice &GetDevice() const {
AMS_ASSERT(this->IsOpen());
2021-10-10 09:14:06 +02:00
return *m_device;
2020-10-30 23:36:11 +01:00
}
bool IsOpen() const {
2021-10-10 09:14:06 +02:00
return m_device != nullptr;
2020-10-30 23:36:11 +01:00
}
bool CheckAccess(AccessMode mode) const {
AMS_ASSERT(this->IsOpen());
2021-10-10 09:14:06 +02:00
return ((~m_access_mode) & mode) == 0;
2020-10-30 23:36:11 +01:00
}
bool CheckExclusiveWrite() const {
return this->CheckAccess(AccessMode_Write) && !this->CheckAccess(AccessMode_Shared);
}
};
}