mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-07-04 16:42:14 +02:00
Stratosphere: Add a utility predicate function to test for equality with a reference element
This can be used to rewrite some common raw loops using algorithms instead
This commit is contained in:
parent
0de99b0006
commit
f65983c5a7
37
stratosphere/libstratosphere/include/meta_tools.hpp
Normal file
37
stratosphere/libstratosphere/include/meta_tools.hpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct class_of;
|
||||||
|
|
||||||
|
template<typename Ret, typename C>
|
||||||
|
struct class_of<Ret C::*> {
|
||||||
|
using type = C;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using class_of_t = typename class_of<T>::type;
|
||||||
|
|
||||||
|
template<typename Mem, typename T, typename C = class_of_t<Mem>>
|
||||||
|
struct member_equals_fn_helper {
|
||||||
|
T ref;
|
||||||
|
Mem mem_fn;
|
||||||
|
|
||||||
|
bool operator()(const C& val) const {
|
||||||
|
return (std::mem_fn(mem_fn)(val) == ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator()(C&& val) const {
|
||||||
|
return (std::mem_fn(mem_fn)(std::move(val)) == ref);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template<typename Mem, typename T>
|
||||||
|
auto member_equals_fn(Mem mem, T ref) {
|
||||||
|
return detail::member_equals_fn_helper<Mem, T>{std::move(ref), std::move(mem)};
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include "ldr_launch_queue.hpp"
|
#include "ldr_launch_queue.hpp"
|
||||||
|
#include "meta_tools.hpp"
|
||||||
|
|
||||||
static std::array<LaunchQueue::LaunchItem, LAUNCH_QUEUE_SIZE> g_launch_queue = {0};
|
static std::array<LaunchQueue::LaunchItem, LAUNCH_QUEUE_SIZE> g_launch_queue = {0};
|
||||||
|
|
||||||
@ -46,13 +47,12 @@ Result LaunchQueue::add_item(const LaunchItem *item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int LaunchQueue::get_index(u64 tid) {
|
int LaunchQueue::get_index(u64 tid) {
|
||||||
for(unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
auto it = std::find_if(g_launch_queue.begin(), g_launch_queue.end(), member_equals_fn(&LaunchQueue::LaunchItem::tid, tid));
|
||||||
if(g_launch_queue[i].tid == tid) {
|
if (it == g_launch_queue.end()) {
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return LAUNCH_QUEUE_FULL;
|
return LAUNCH_QUEUE_FULL;
|
||||||
}
|
}
|
||||||
|
return std::distance(g_launch_queue.begin(), it);
|
||||||
|
}
|
||||||
|
|
||||||
int LaunchQueue::get_free_index(u64 tid) {
|
int LaunchQueue::get_free_index(u64 tid) {
|
||||||
for(unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
for(unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stratosphere/servicesession.hpp>
|
#include <stratosphere/servicesession.hpp>
|
||||||
#include "sm_registration.hpp"
|
#include "sm_registration.hpp"
|
||||||
|
#include "meta_tools.hpp"
|
||||||
|
|
||||||
static std::array<Registration::Process, REGISTRATION_LIST_MAX_PROCESS> g_process_list = {0};
|
static std::array<Registration::Process, REGISTRATION_LIST_MAX_PROCESS> g_process_list = {0};
|
||||||
static std::array<Registration::Service, REGISTRATION_LIST_MAX_SERVICE> g_service_list = {0};
|
static std::array<Registration::Service, REGISTRATION_LIST_MAX_SERVICE> g_service_list = {0};
|
||||||
@ -21,12 +22,11 @@ u64 GetServiceNameLength(u64 service) {
|
|||||||
|
|
||||||
/* Utilities. */
|
/* Utilities. */
|
||||||
Registration::Process *Registration::GetProcessForPid(u64 pid) {
|
Registration::Process *Registration::GetProcessForPid(u64 pid) {
|
||||||
for (auto &process : g_process_list) {
|
auto process_it = std::find_if(g_process_list.begin(), g_process_list.end(), member_equals_fn(&Process::pid, pid));
|
||||||
if (process.pid == pid) {
|
if (process_it == g_process_list.end()) {
|
||||||
return &process;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
return &*process_it;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Registration::Process *Registration::GetFreeProcess() {
|
Registration::Process *Registration::GetFreeProcess() {
|
||||||
@ -34,12 +34,11 @@ Registration::Process *Registration::GetFreeProcess() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Registration::Service *Registration::GetService(u64 service_name) {
|
Registration::Service *Registration::GetService(u64 service_name) {
|
||||||
for (auto &service : g_service_list) {
|
auto service_it = std::find_if(g_service_list.begin(), g_service_list.end(), member_equals_fn(&Service::service_name, service_name));
|
||||||
if (service.service_name == service_name) {
|
if (service_it == g_service_list.end()) {
|
||||||
return &service;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
return &*service_it;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Registration::Service *Registration::GetFreeService() {
|
Registration::Service *Registration::GetFreeService() {
|
||||||
@ -168,12 +167,7 @@ Result Registration::UnregisterProcess(u64 pid) {
|
|||||||
|
|
||||||
/* Service management. */
|
/* Service management. */
|
||||||
bool Registration::HasService(u64 service) {
|
bool Registration::HasService(u64 service) {
|
||||||
for (unsigned int i = 0; i < REGISTRATION_LIST_MAX_SERVICE; i++) {
|
return std::any_of(g_service_list.begin(), g_service_list.end(), member_equals_fn(&Service::service_name, service));
|
||||||
if (g_service_list[i].service_name == service) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Result Registration::GetServiceHandle(u64 pid, u64 service, Handle *out) {
|
Result Registration::GetServiceHandle(u64 pid, u64 service, Handle *out) {
|
||||||
|
Loading…
Reference in New Issue
Block a user