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

93 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.hpp>
namespace ams::ddsf {
class EventHandlerManager;
class IEventHandler {
NON_COPYABLE(IEventHandler);
NON_MOVEABLE(IEventHandler);
friend class EventHandlerManager;
private:
os::MultiWaitHolderType holder;
2020-10-30 22:36:11 +00:00
uintptr_t user_data;
bool is_initialized;
bool is_registered;
private:
void Link(os::MultiWaitType *multi_wait) {
2020-10-30 22:36:11 +00:00
AMS_ASSERT(this->IsInitialized());
AMS_ASSERT(!this->IsRegistered());
AMS_ASSERT(multi_wait != nullptr);
os::LinkMultiWaitHolder(multi_wait, std::addressof(this->holder));
2020-10-30 22:36:11 +00:00
}
void Unlink() {
AMS_ASSERT(this->IsInitialized());
AMS_ASSERT(this->IsRegistered());
os::UnlinkMultiWaitHolder(std::addressof(this->holder));
2020-10-30 22:36:11 +00:00
}
static IEventHandler &ToEventHandler(os::MultiWaitHolderType *holder) {
2020-10-30 22:36:11 +00:00
AMS_ASSERT(holder != nullptr);
auto &event_handler = *reinterpret_cast<IEventHandler *>(os::GetMultiWaitHolderUserData(holder));
2020-10-30 22:36:11 +00:00
AMS_ASSERT(event_handler.IsInitialized());
return event_handler;
}
public:
IEventHandler() : holder(), user_data(0), is_initialized(false), is_registered(false) { /* ... */ }
~IEventHandler() {
if (this->IsRegistered()) {
this->Unlink();
}
if (this->IsInitialized()) {
this->Finalize();
}
}
bool IsInitialized() const { return this->is_initialized; }
bool IsRegistered() const { return this->is_registered; }
uintptr_t GetUserData() const { return this->user_data; }
void SetUserData(uintptr_t d) { this->user_data = d; }
template<typename T>
void Initialize(T *object) {
AMS_ASSERT(object != nullptr);
AMS_ASSERT(!this->IsInitialized());
os::InitializeMultiWaitHolder(std::addressof(this->holder), object);
os::SetMultiWaitHolderUserData(std::addressof(this->holder), reinterpret_cast<uintptr_t>(this));
2020-10-30 22:36:11 +00:00
this->is_initialized = true;
this->is_registered = false;
}
void Finalize() {
AMS_ASSERT(this->IsInitialized());
AMS_ASSERT(!this->IsRegistered());
os::FinalizeMultiWaitHolder(std::addressof(this->holder));
2020-10-30 22:36:11 +00:00
this->is_initialized = false;
this->is_registered = false;
}
protected:
virtual void HandleEvent() = 0;
};
}