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

100 lines
3.4 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>
#include <stratosphere/os/os_sdk_mutex.hpp>
#include <stratosphere/ddsf/ddsf_types.hpp>
#include <stratosphere/ddsf/impl/ddsf_for_each.hpp>
#include <stratosphere/ddsf/ddsf_i_castable.hpp>
#include <stratosphere/ddsf/ddsf_i_device.hpp>
namespace ams::ddsf {
class IDriver : public ICastable {
public:
AMS_DDSF_CASTABLE_ROOT_TRAITS(ams::ddsf::IDriver);
private:
2021-10-10 07:14:06 +00:00
util::IntrusiveListNode m_list_node;
IDevice::List m_device_list;
mutable os::SdkMutex m_device_list_lock;
2020-10-30 22:36:11 +00:00
public:
2021-10-10 07:14:06 +00:00
using ListTraits = util::IntrusiveListMemberTraitsDeferredAssert<&IDriver::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<IDriver, util::IntrusiveListMemberTraitsDeferredAssert<&IDriver::m_list_node>>;
2020-10-30 22:36:11 +00:00
private:
public:
2021-10-10 07:14:06 +00:00
IDriver() : m_list_node(), m_device_list(), m_device_list_lock() {
m_device_list.clear();
2020-10-30 22:36:11 +00:00
}
protected:
~IDriver() {
2021-10-10 07:14:06 +00:00
m_device_list.clear();
2020-10-30 22:36:11 +00:00
}
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 07:14:06 +00:00
return m_list_node.IsLinked();
2020-10-30 22:36:11 +00:00
}
bool HasAnyDevice() const {
2021-10-10 07:14:06 +00:00
return !m_device_list.empty();
2020-10-30 22:36:11 +00:00
}
void RegisterDevice(IDevice *dev) {
AMS_ASSERT(dev != nullptr);
2021-10-10 07:14:06 +00:00
std::scoped_lock lk(m_device_list_lock);
2020-10-30 22:36:11 +00:00
dev->AttachDriver(this);
2021-10-10 07:14:06 +00:00
m_device_list.push_back(*dev);
2020-10-30 22:36:11 +00:00
}
void UnregisterDevice(IDevice *dev) {
AMS_ASSERT(dev != nullptr);
2021-10-10 07:14:06 +00:00
std::scoped_lock lk(m_device_list_lock);
m_device_list.erase(m_device_list.iterator_to(*dev));
2020-10-30 22:36:11 +00:00
dev->DetachDriver();
}
template<typename F>
Result ForEachDevice(F f, bool return_on_fail) {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_device_list_lock, m_device_list, f, return_on_fail);
2020-10-30 22:36:11 +00:00
}
template<typename F>
Result ForEachDevice(F f, bool return_on_fail) const {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_device_list_lock, m_device_list, f, return_on_fail);
2020-10-30 22:36:11 +00:00
}
template<typename F>
int ForEachDevice(F f) {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_device_list_lock, m_device_list, f);
2020-10-30 22:36:11 +00:00
}
template<typename F>
int ForEachDevice(F f) const {
2021-10-10 07:14:06 +00:00
return impl::ForEach(m_device_list_lock, m_device_list, f);
2020-10-30 22:36:11 +00:00
}
};
static_assert(IDriver::ListTraits::IsValid());
}