Skip to content

Commit f7cc399

Browse files
Refactor Application Framework.
Make the ANSI logger compile, update examples_tests
1 parent 81aad96 commit f7cc399

File tree

5 files changed

+65
-36
lines changed

5 files changed

+65
-36
lines changed

include/nbl/system/CApplicationAndroid.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "nbl/system/IApplicationFramework.h"
77
#include "nbl/system/CStdoutLoggerAndroid.h"
88

9+
// TODO: move to .cpp file
910
#ifdef _NBL_PLATFORM_ANDROID_
1011
#include <android_native_app_glue.h>
1112
#include <android/sensor.h>
@@ -15,7 +16,7 @@
1516
namespace nbl::system
1617
{
1718
#ifdef _NBL_PLATFORM_ANDROID_
18-
19+
// TODO: fill rewrite of this thing, also rename to `IApplicationAndroid`
1920
class CApplicationAndroid : public IApplicationFramework
2021
{
2122
public:
@@ -140,6 +141,11 @@ class CApplicationAndroid : public IApplicationFramework
140141
bool keepPolling() const { return eventPoller.continuePredicate(); }
141142
};
142143

144+
#define NBL_MAIN_FUNC(AppClass, ...) void android_main(android_app* app) \
145+
{\
146+
nbl::system::IApplicationFramework::main<AppClass>(app __VA_OPT__(,) __VA_ARGS__);\
147+
}
148+
143149
#endif
144150
}
145151

include/nbl/system/CColoredStdoutLoggerANSI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace nbl::system
99
{
1010

1111
// logging using ANSI escape codes
12-
class NBL_API2 CColoredStdoutLoggerANSI : public IThreadsafeLogger
12+
class CColoredStdoutLoggerANSI : public IThreadsafeLogger
1313
{
1414
public:
15-
CColoredStdoutLoggerANSI(core::bitflag<E_LOG_LEVEL> logLevelMask = ILogger::defaultLogMask()) : IThreadsafeLogger(logLevelMask) {}
15+
inline CColoredStdoutLoggerANSI(core::bitflag<E_LOG_LEVEL> logLevelMask = ILogger::defaultLogMask()) : IThreadsafeLogger(logLevelMask) {}
1616

1717
private:
1818
// more info about how this works: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797

include/nbl/system/IApplicationFramework.h

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace nbl::system
2020
{
2121

22-
class IApplicationFramework
22+
class IApplicationFramework : public core::IReferenceCounted
2323
{
2424
public:
2525
// this is safe to call multiple times
@@ -52,13 +52,29 @@ class IApplicationFramework
5252
#endif
5353
}
5454

55+
// we take the derived class as Curiously Recurring Template Parameter
56+
template<class CRTP> requires std::is_base_of_v<IApplicationFramework,CRTP>
57+
static int main(int argc, char** argv)
58+
{
59+
path CWD = system::path(argv[0]).parent_path().generic_string() + "/";
60+
auto app = core::make_smart_refctd_ptr<CRTP>(CWD/"../../media/",CWD/"../../tmp/",CWD/"../",CWD);
61+
for (auto i=0; i<argc; i++)
62+
app->argv.emplace_back(argv[i]);
63+
64+
if (!app->onAppInitialized(nullptr))
65+
return -1;
66+
while (app->keepRunning())
67+
app->workLoopBody();
68+
return app->onAppTerminated() ? 0:(-2);
69+
}
70+
5571
static nbl::core::smart_refctd_ptr<ISystem> createSystem()
5672
{
5773
GlobalsInit();
5874
#ifdef _NBL_PLATFORM_WINDOWS_
5975
return nbl::core::make_smart_refctd_ptr<CSystemWin32>();
6076
#elif defined(_NBL_PLATFORM_ANDROID_)
61-
return nbl::core::make_smart_refctd_ptr<CSystemAndroid>(std::move(caller));
77+
return nullptr;
6278
#endif
6379
return nullptr;
6480
}
@@ -73,29 +89,26 @@ class IApplicationFramework
7389
GlobalsInit();
7490
}
7591

76-
virtual void setSystem(core::smart_refctd_ptr<ISystem>&& system) = 0;
92+
// DEPRECATED
93+
virtual void setSystem(core::smart_refctd_ptr<ISystem>&& system) {}
7794

78-
void onAppInitialized()
79-
{
80-
return onAppInitialized_impl();
81-
}
82-
void onAppTerminated()
83-
{
84-
return onAppTerminated_impl();
85-
}
95+
// Some platforms are weird, and you can't really do anything with the system unless you have some magical object that gets passed to the platform's entry point.
96+
// Therefore the specific `CPLATFORMSystem` is uncreatable out of thin air so you need to take an outside provided one
97+
virtual bool onAppInitialized(core::smart_refctd_ptr<ISystem>&& system=nullptr) {onAppInitialized_impl(std::move(system)); return true;}
98+
virtual bool onAppTerminated() {return true;}
8699

87100
virtual void workLoopBody() = 0;
88101
virtual bool keepRunning() = 0;
89102

90-
// TODO: refactor/hide
91-
std::vector<std::string> argv;
92-
93103
protected:
94104
~IApplicationFramework() {}
95105

96-
// TODO: why aren't these pure virtual, and why do we even need a `_impl()`suffix?
97-
virtual void onAppInitialized_impl() {}
98-
virtual void onAppTerminated_impl() {}
106+
// DEPRECATED
107+
virtual void onAppInitialized_impl(core::smart_refctd_ptr<ISystem>&& system) {assert(false);}
108+
virtual void onAppTerminated_impl() {assert(false);}
109+
110+
// for platforms with cmdline args
111+
core::vector<std::string> argv;
99112

100113
/*
101114
****************** Current Working Directories ********************
@@ -130,4 +143,14 @@ class IApplicationFramework
130143

131144
}
132145

146+
// get the correct entry point to declate
147+
#ifdef _NBL_PLATFORM_ANDROID_
148+
#include "nbl/system/CApplicationAndroid.h"
149+
#else
150+
#define NBL_MAIN_FUNC(AppClass) int main(int argc, char** argv) \
151+
{\
152+
return AppClass::main<AppClass>(argc,argv);\
153+
}
154+
#endif
155+
133156
#endif

include/nbl/system/IThreadsafeLogger.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ namespace nbl::system
99
{
1010
class IThreadsafeLogger : public ILogger
1111
{
12-
mutable std::mutex m_mutex;
13-
public:
14-
IThreadsafeLogger(core::bitflag<E_LOG_LEVEL> logLevelMask) : ILogger(logLevelMask) {}
15-
// Inherited via ILogger
16-
void log_impl(const std::string_view& fmtString, E_LOG_LEVEL logLevel, va_list args) override final
17-
{
18-
auto l = lock();
19-
threadsafeLog_impl(fmtString, logLevel, args);
20-
}
21-
private:
22-
virtual void threadsafeLog_impl(const std::string_view&, E_LOG_LEVEL logLevel, va_list args) = 0;
12+
mutable std::mutex m_mutex;
13+
public:
14+
inline IThreadsafeLogger(core::bitflag<E_LOG_LEVEL> logLevelMask) : ILogger(logLevelMask) {}
15+
// Inherited via ILogger
16+
inline void log_impl(const std::string_view& fmtString, E_LOG_LEVEL logLevel, va_list args) override final
17+
{
18+
auto l = lock();
19+
threadsafeLog_impl(fmtString, logLevel, args);
20+
}
21+
private:
22+
virtual void threadsafeLog_impl(const std::string_view&, E_LOG_LEVEL logLevel, va_list args) = 0;
2323

24-
std::unique_lock<std::mutex> lock() const
25-
{
26-
return std::unique_lock<std::mutex>(m_mutex);
27-
}
24+
inline std::unique_lock<std::mutex> lock() const
25+
{
26+
return std::unique_lock<std::mutex>(m_mutex);
27+
}
2828

2929
};
3030
}

0 commit comments

Comments
 (0)