1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-26 04:02:11 +00:00

Implemented C++ scope guards.

Thanks @SciresM

These will be used for handling resource deinitialization in Borealis-related code.
This commit is contained in:
Pablo Curiel 2021-06-07 23:13:45 -04:00
parent 9c009753e3
commit 78f780a981
9 changed files with 187 additions and 56 deletions

View file

@ -81,7 +81,7 @@ CFLAGS += `aarch64-none-elf-pkg-config libxml-2.0 --cflags`
CFLAGS += `aarch64-none-elf-pkg-config json-c --cflags`
CFLAGS += `aarch64-none-elf-pkg-config libturbojpeg --cflags`
CXXFLAGS := $(CFLAGS) -std=c++1z -O2 -Wno-volatile -Wno-unused-parameter
CXXFLAGS := $(CFLAGS) -std=c++20 -O2 -Wno-volatile -Wno-unused-parameter
ASFLAGS := -g $(ARCH)
LDFLAGS := -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

View file

@ -1,5 +1,5 @@
/*
* common.h
* nxdt_includes.h
*
* Copyright (c) 2020-2021, DarkMatterCore <pabloacurielz@gmail.com>.
*
@ -21,9 +21,10 @@
#pragma once
#ifndef __COMMON_H__
#define __COMMON_H__
#ifndef __NXDT_INCLUDES_H__
#define __NXDT_INCLUDES_H__
/* C headers. */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@ -38,7 +39,6 @@
#include <sys/stat.h>
#include <assert.h>
#include <unistd.h>
#include <switch.h>
#ifndef __cplusplus
#include <stdatomic.h>
@ -47,19 +47,18 @@
#define _Atomic(X) std::atomic< X >
#endif
/* libnx header. */
#include <switch.h>
/* Global defines. */
#include "../defines.h"
/* File-based logger. */
#include "nxdt_log.h"
/* USB Mass Storage support. */
#include "ums.h"
#define FS_SYSMODULE_TID (u64)0x0100000000000000
#define BOOT_SYSMODULE_TID (u64)0x0100000000000005
#define SPL_SYSMODULE_TID (u64)0x0100000000000028
#define ES_SYSMODULE_TID (u64)0x0100000000000033
#define SYSTEM_UPDATE_TID (u64)0x0100000000000816
#define FAT32_FILESIZE_LIMIT (u64)0xFFFFFFFF /* 4 GiB - 1 (4294967295 bytes). */
#define NXDT_ASSERT(name, size) static_assert(sizeof(name) == (size), "Bad size for " #name "! Expected " #size ".")
/// Used to store version numbers expressed in dot notation: "{major}.{minor}.{micro}-{major_relstep}.{minor_relstep}".
/// Referenced by multiple header files.
typedef struct {
@ -93,4 +92,4 @@ typedef struct {
NXDT_ASSERT(VersionType2, 0x4);
#endif /* __COMMON_H__ */
#endif /* __NXDT_INCLUDES_H__ */

View file

@ -24,29 +24,15 @@
#ifndef __NXDT_UTILS_H__
#define __NXDT_UTILS_H__
#include "common.h"
/* Included here for convenience. */
#include "nxdt_includes.h"
#ifdef __cplusplus
extern "C" {
#endif
#define APP_BASE_PATH "sdmc:/switch/" APP_TITLE "/"
#define BIS_SYSTEM_PARTITION_MOUNT_NAME "sys:"
#define MEMBER_SIZE(type, member) sizeof(((type*)NULL)->member)
#define MAX_ELEMENTS(x) ((sizeof((x))) / (sizeof((x)[0])))
#define BIT_LONG(n) (1UL << (n))
#define ALIGN_UP(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
#define ALIGN_DOWN(x, y) ((x) & ~((y) - 1))
#define IS_ALIGNED(x, y) (((x) & ((y) - 1)) == 0)
#define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
#define SCOPED_LOCK(mtx) for(UtilsScopedLock scoped_lock __attribute__((__cleanup__(utilsUnlockScope))) = utilsLockScope(mtx); scoped_lock.cond; scoped_lock.cond = 0)
/* Scoped lock macro. */
#define SCOPED_LOCK(mtx) for(UtilsScopedLock ANONYMOUS_VARIABLE(scoped_lock) __attribute__((__cleanup__(utilsUnlockScope))) = utilsLockScope(mtx); ANONYMOUS_VARIABLE(scoped_lock).cond; ANONYMOUS_VARIABLE(scoped_lock).cond = 0)
/// Used by scoped locks.
typedef struct {

70
include/defines.h Normal file
View file

@ -0,0 +1,70 @@
/*
* defines.h
*
* Copyright (c) 2020-2021, DarkMatterCore <pabloacurielz@gmail.com>.
*
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
*
* nxdumptool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nxdumptool is distributed in the hope that 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef __DEFINES_H__
#define __DEFINES_H__
/* Broadly useful language defines. */
#define MEMBER_SIZE(type, member) sizeof(((type*)NULL)->member)
#define MAX_ELEMENTS(x) ((sizeof((x))) / (sizeof((x)[0])))
#define BIT_LONG(n) (1UL << (n))
#define ALIGN_UP(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
#define ALIGN_DOWN(x, y) ((x) & ~((y) - 1))
#define IS_ALIGNED(x, y) (((x) & ((y) - 1)) == 0)
#define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
#define CONCATENATE_IMPL(s1, s2) s1##s2
#define CONCATENATE(s1, s2) CONCATENATE_IMPL(s1, s2)
#define ANONYMOUS_VARIABLE(pref) CONCATENATE(pref, __LINE__)
#define NON_COPYABLE(cls) \
cls(const cls&) = delete; \
cls& operator=(const cls&) = delete
#define ALWAYS_INLINE inline __attribute__((always_inline))
#define ALWAYS_INLINE_LAMBDA __attribute__((always_inline))
#define NXDT_ASSERT(name, size) static_assert(sizeof(name) == (size), "Bad size for " #name "! Expected " #size ".")
/* Global constants used throughout the application. */
#define FS_SYSMODULE_TID (u64)0x0100000000000000
#define BOOT_SYSMODULE_TID (u64)0x0100000000000005
#define SPL_SYSMODULE_TID (u64)0x0100000000000028
#define ES_SYSMODULE_TID (u64)0x0100000000000033
#define SYSTEM_UPDATE_TID (u64)0x0100000000000816
#define FAT32_FILESIZE_LIMIT (u64)0xFFFFFFFF /* 4 GiB - 1 (4294967295 bytes). */
/* Path defines. */
#define APP_BASE_PATH "sdmc:/switch/" APP_TITLE "/"
#define BIS_SYSTEM_PARTITION_MOUNT_NAME "sys:"
#endif /* __DEFINES_H__ */

80
include/scope_guard.hpp Normal file
View file

@ -0,0 +1,80 @@
/*
* scope_guard.hpp
*
* Copyright (c) 2020-2021, DarkMatterCore <pabloacurielz@gmail.com>.
* Copyright (c) 2018-2021, SciresM.
*
* Scope guard logic lovingly taken from Andrei Alexandrescu's "Systemic Error Handling in C++".
*
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
*
* nxdumptool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nxdumptool is distributed in the hope that 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef __SCOPE_GUARD_HPP__
#define __SCOPE_GUARD_HPP__
#include "defines.h"
#define SCOPE_GUARD ::nxdt::utils::ScopeGuardOnExit() + [&]() ALWAYS_INLINE_LAMBDA
#define ON_SCOPE_EXIT auto ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE_) = SCOPE_GUARD
namespace nxdt::utils {
template<class F>
class ScopeGuard {
NON_COPYABLE(ScopeGuard);
private:
F f;
bool active;
public:
constexpr ALWAYS_INLINE ScopeGuard(F f) : f(std::move(f)), active(true) {}
constexpr ALWAYS_INLINE ~ScopeGuard()
{
if (active) f();
}
constexpr ALWAYS_INLINE void Cancel()
{
active = false;
}
constexpr ALWAYS_INLINE ScopeGuard(ScopeGuard&& rhs) : f(std::move(rhs.f)), active(rhs.active)
{
rhs.Cancel();
}
ScopeGuard &operator=(ScopeGuard&& rhs) = delete;
};
template<class F>
constexpr ALWAYS_INLINE ScopeGuard<F> MakeScopeGuard(F f)
{
return ScopeGuard<F>(std::move(f));
}
enum class ScopeGuardOnExit {};
template <typename F>
constexpr ALWAYS_INLINE ScopeGuard<F> operator+(ScopeGuardOnExit, F&& f)
{
return ScopeGuard<F>(std::forward<F>(f));
}
}
#endif /* __SCOPE_GUARD_HPP__ */

@ -1 +1 @@
Subproject commit 595ee205233322065bee12121e02cab5a80f70d6
Subproject commit 45a33d8a2bdb81b53378a8c6dbff1b3d6cb83ebd

View file

@ -1,5 +1,5 @@
{
"name": "Borealis Example App",
"name": "{0}",
"tabs": {
"first": "First tab",
"second": "Second tab",

View file

@ -666,7 +666,7 @@ static bool usbSendCommand(void)
/* Write command header first. */
if (!usbWrite(cmd_header, sizeof(UsbCommandHeader)))
{
LOG_MSG("Failed to write header for type 0x%X command!", cmd);
if (!g_usbDetectionThreadExitFlag) LOG_MSG("Failed to write header for type 0x%X command!", cmd);
status = UsbStatusType_WriteCommandFailed;
goto end;
}
@ -1221,7 +1221,7 @@ static bool usbTransferData(void *buf, u64 size, UsbDsEndpoint *endpoint)
/* If the USB session has already been established, then use a regular timeout value. */
rc = eventWait(&(endpoint->CompletionEvent), USB_TRANSFER_TIMEOUT * (u64)1000000000);
} else {
/* If we're starting a USB session, wait indefinitely inside a loop to let the user start the companion app. */
/* If we're starting a USB session, wait indefinitely inside a loop to let the user start the host script. */
int idx = 0;
Waiter completion_event_waiter = waiterForEvent(&(endpoint->CompletionEvent));
Waiter exit_event_waiter = waiterForUEvent(&g_usbDetectionThreadExitEvent);

View file

@ -21,6 +21,7 @@
#include <stdlib.h>
#include <nxdt_utils.h>
#include <scope_guard.hpp>
#include <borealis.hpp>
#include <string>
@ -44,11 +45,9 @@ std::vector<std::string> NOTIFICATIONS = {
int main(int argc, char* argv[])
{
if (!utilsInitializeResources(argc, (const char**)argv))
{
utilsCloseResources();
return EXIT_FAILURE;
}
ON_SCOPE_EXIT { utilsCloseResources(); };
if (!utilsInitializeResources(argc, (const char**)argv)) return EXIT_FAILURE;
// Init the app
brls::Logger::setLogLevel(brls::LogLevel::DEBUG);
@ -57,13 +56,12 @@ int main(int argc, char* argv[])
if (!brls::Application::init("main/name"_i18n))
{
brls::Logger::error("Unable to init Borealis application");
utilsCloseResources();
return EXIT_FAILURE;
}
// Create a sample view
brls::TabFrame* rootFrame = new brls::TabFrame();
rootFrame->setTitle("main/name"_i18n);
rootFrame->setTitle(i18n::getStr("main/name", APP_TITLE));
rootFrame->setIcon(BOREALIS_ASSET("icon/" APP_TITLE ".jpg" ));
brls::List* testList = new brls::List();
@ -114,15 +112,6 @@ int main(int argc, char* argv[])
brls::ListItem* crashItem = new brls::ListItem("main/divide/title"_i18n, "main/divide/description"_i18n);
crashItem->getClickEvent()->subscribe([](brls::View* view) { brls::Application::crash("main/divide/crash"_i18n); });
brls::ListItem* popupItem = new brls::ListItem("popup/open"_i18n);
popupItem->getClickEvent()->subscribe([](brls::View* view) {
brls::TabFrame* popupTabFrame = new brls::TabFrame();
popupTabFrame->addTab("popup/red"_i18n, new brls::Rectangle(nvgRGB(255, 0, 0)));
popupTabFrame->addTab("popup/green"_i18n, new brls::Rectangle(nvgRGB(0, 255, 0)));
popupTabFrame->addTab("popup/blue"_i18n, new brls::Rectangle(nvgRGB(0, 0, 255)));
brls::PopupFrame::open("popup/title"_i18n, BOREALIS_ASSET("icon/" APP_TITLE ".jpg"), popupTabFrame, "popup/subtitle/left"_i18n, "popup/subtitle/right"_i18n);
});
brls::ListItem* installerItem = new brls::ListItem("installer/open"_i18n);
installerItem->getClickEvent()->subscribe([](brls::View* view) {
brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame();
@ -135,6 +124,15 @@ int main(int argc, char* argv[])
brls::Application::pushView(stagedFrame);
});
brls::ListItem* popupItem = new brls::ListItem("popup/open"_i18n);
popupItem->getClickEvent()->subscribe([](brls::View* view) {
brls::TabFrame* popupTabFrame = new brls::TabFrame();
popupTabFrame->addTab("popup/red"_i18n, new brls::Rectangle(nvgRGB(255, 0, 0)));
popupTabFrame->addTab("popup/green"_i18n, new brls::Rectangle(nvgRGB(0, 255, 0)));
popupTabFrame->addTab("popup/blue"_i18n, new brls::Rectangle(nvgRGB(0, 0, 255)));
brls::PopupFrame::open("popup/title"_i18n, BOREALIS_ASSET("icon/" APP_TITLE ".jpg"), popupTabFrame, "popup/subtitle/left"_i18n, "popup/subtitle/right"_i18n);
});
brls::SelectListItem* layerSelectItem = new brls::SelectListItem("main/layers/title"_i18n, { "main/layers/layer1"_i18n, "main/layers/layer2"_i18n });
brls::InputListItem* keyboardItem = new brls::InputListItem("main/keyboard/string/title"_i18n, "main/keyboard/string/default"_i18n, "main/keyboard/string/help"_i18n, "", 16);
@ -199,8 +197,6 @@ int main(int argc, char* argv[])
// Run the app
while (brls::Application::mainLoop());
utilsCloseResources();
// Exit
return EXIT_SUCCESS;
}