Skip to content

Commit 62eeaae

Browse files
committed
Merge branch 'refactor/rework-process-request-ISystem' into add-missing-includes-for-non-pcf-builds
2 parents 61fa8cd + 68710de commit 62eeaae

File tree

6 files changed

+92
-52
lines changed

6 files changed

+92
-52
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef _NBL_SYSTEM_C_COLORED_STDOUT_LOGGER_ANSI_INCLUDED_
2+
#define _NBL_SYSTEM_C_COLORED_STDOUT_LOGGER_ANSI_INCLUDED_
3+
4+
#include "nbl/system/IThreadsafeLogger.h"
5+
6+
#include <string_view>
7+
8+
namespace nbl::system
9+
{
10+
11+
// logging using ANSI escape codes
12+
class NBL_API2 CColoredStdoutLoggerANSI : public IThreadsafeLogger
13+
{
14+
public:
15+
CColoredStdoutLoggerANSI(core::bitflag<E_LOG_LEVEL> logLevelMask = ILogger::defaultLogMask()) : IThreadsafeLogger(logLevelMask) {}
16+
17+
private:
18+
// more info about how this works: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
19+
virtual void threadsafeLog_impl(const std::string_view &fmt, E_LOG_LEVEL logLevel, va_list args) override
20+
{
21+
auto str = constructLogString(fmt, logLevel, args);
22+
switch (logLevel)
23+
{
24+
case ELL_DEBUG:
25+
printf("\x1b[37m%s", str.data()); // White
26+
break;
27+
case ELL_INFO:
28+
printf("\x1b[37m%s", str.data()); // White
29+
break;
30+
case ELL_WARNING:
31+
printf("\x1b[33m%s", str.data()); // yellow
32+
break;
33+
case ELL_ERROR:
34+
printf("\x1b[31m%s", str.data()); // red
35+
break;
36+
case ELL_PERFORMANCE:
37+
printf("\x1b[34m%s", str.data()); // blue
38+
break;
39+
case ELL_NONE:
40+
assert(false);
41+
break;
42+
}
43+
fflush(stdout);
44+
}
45+
};
46+
47+
} // namespace nbl::system
48+
49+
#endif

include/nbl/system/ISystem.h

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
#include "nbl/core/declarations.h"
88
#include "nbl/core/util/bitflag.h"
99

10-
#include <variant>
11-
1210
#include "nbl/system/ICancellableAsyncQueueDispatcher.h"
1311
#include "nbl/system/IFileArchive.h"
1412

13+
#include <variant>
1514

1615
namespace nbl::system
1716
{
@@ -26,35 +25,22 @@ class NBL_API2 ISystem : public core::IReferenceCounted
2625
protected:
2726
class ICaller;
2827
private:
29-
// add more request types if needed
30-
enum E_REQUEST_TYPE
31-
{
32-
ERT_CREATE_FILE,
33-
ERT_READ,
34-
ERT_WRITE
35-
};
36-
template <E_REQUEST_TYPE RT>
37-
struct SRequestParamsBase
38-
{
39-
static inline constexpr E_REQUEST_TYPE type = RT;
40-
};
41-
42-
struct SRequestParams_EMPTY
28+
struct SRequestParams_NOOP
4329
{
4430
};
45-
struct SRequestParams_CREATE_FILE : SRequestParamsBase<ERT_CREATE_FILE>
31+
struct SRequestParams_CREATE_FILE
4632
{
4733
char filename[MAX_FILENAME_LENGTH] {};
4834
IFileBase::E_CREATE_FLAGS flags;
4935
};
50-
struct SRequestParams_READ : SRequestParamsBase<ERT_READ>
36+
struct SRequestParams_READ
5137
{
5238
ISystemFile* file;
5339
void* buffer;
5440
size_t offset;
5541
size_t size;
5642
};
57-
struct SRequestParams_WRITE : SRequestParamsBase<ERT_WRITE>
43+
struct SRequestParams_WRITE
5844
{
5945
ISystemFile* file;
6046
const void* buffer;
@@ -63,13 +49,12 @@ class NBL_API2 ISystem : public core::IReferenceCounted
6349
};
6450
struct SRequestType : impl::ICancellableAsyncQueueDispatcherBase::request_base_t
6551
{
66-
E_REQUEST_TYPE type;
6752
std::variant<
68-
SRequestParams_EMPTY,
53+
SRequestParams_NOOP,
6954
SRequestParams_CREATE_FILE,
7055
SRequestParams_READ,
7156
SRequestParams_WRITE
72-
> params = SRequestParams_EMPTY();
57+
> params = SRequestParams_NOOP();
7358
};
7459
static inline constexpr uint32_t CircularBufferSize = 256u;
7560
class CAsyncQueue : public ICancellableAsyncQueueDispatcher<CAsyncQueue,SRequestType,CircularBufferSize>
@@ -83,7 +68,6 @@ class NBL_API2 ISystem : public core::IReferenceCounted
8368
template <typename FutureType, typename RequestParams>
8469
void request_impl(SRequestType& req, FutureType& future, RequestParams&& params)
8570
{
86-
req.type = params.type;
8771
req.params = std::move(params);
8872
base_t::associate_request_with_future(req, future);
8973
}
@@ -93,6 +77,10 @@ class NBL_API2 ISystem : public core::IReferenceCounted
9377
void init() {}
9478

9579
private:
80+
void handle_request(SRequestType& req, SRequestParams_NOOP& param);
81+
void handle_request(SRequestType& req, SRequestParams_CREATE_FILE& param);
82+
void handle_request(SRequestType& req, SRequestParams_READ& param);
83+
void handle_request(SRequestType& req, SRequestParams_WRITE& param);
9684
core::smart_refctd_ptr<ICaller> m_caller;
9785
};
9886
friend class ISystemFile;

include/nbl/system/ISystemPOSIX.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace nbl::system
1010
{
1111

12-
#if defined(__unix__)
12+
#if defined(_NBL_PLATFORM_LINUX_) || defined (_NBL_PLATFORM_ANDROID_)
1313
class ISystemPOSIX : public ISystem
1414
{
1515
protected:

src/nbl/system/CSystemLinux.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ using namespace nbl;
44
using namespace nbl::system;
55

66
#ifdef _NBL_PLATFORM_LINUX_
7+
8+
#include <sys/sysinfo.h>
79
ISystem::SystemInfo CSystemLinux::getSystemInfo() const
810
{
911
SystemInfo info;
1012

1113
// TODO
12-
info.cpuFrequencyHz = 3000000000u;
14+
// info.cpuFrequencyHz = 3000000000u;
1315

14-
sysinfo linuxSystemInfo;
16+
struct sysinfo linuxSystemInfo;
1517
sysinfo(&linuxSystemInfo);
1618
info.totalMemory = linuxSystemInfo.totalram;
1719
info.availableMemory = linuxSystemInfo.freeram;

src/nbl/system/ISystem.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -308,27 +308,25 @@ ISystem::FoundArchiveFile ISystem::findFileInArchive(const system::path& absolut
308308

309309
void ISystem::CAsyncQueue::process_request(SRequestType& req)
310310
{
311-
switch (req.type)
312-
{
313-
case ERT_CREATE_FILE:
314-
{
315-
auto& p = std::get<SRequestParams_CREATE_FILE>(req.params);
316-
base_t::notify_future<core::smart_refctd_ptr<IFile>>(req,m_caller->createFile(p.filename,p.flags));
317-
}
318-
break;
319-
case ERT_READ:
320-
{
321-
auto& p = std::get<SRequestParams_READ>(req.params);
322-
base_t::notify_future<size_t>(req,p.file->asyncRead(p.buffer, p.offset, p.size));
323-
}
324-
break;
325-
case ERT_WRITE:
326-
{
327-
auto& p = std::get<SRequestParams_WRITE>(req.params);
328-
base_t::notify_future<size_t>(req,p.file->asyncWrite(p.buffer, p.offset, p.size));
329-
}
330-
break;
331-
}
311+
std::visit([&](auto& visitor) {
312+
handle_request(req, visitor);
313+
}, req.params);
314+
}
315+
316+
void ISystem::CAsyncQueue::handle_request(SRequestType& req, SRequestParams_NOOP& param) {
317+
assert(false); // should never be called
318+
}
319+
320+
void ISystem::CAsyncQueue::handle_request(SRequestType& req, SRequestParams_CREATE_FILE& param) {
321+
base_t::notify_future<core::smart_refctd_ptr<IFile>>(req, m_caller->createFile(param.filename, param.flags));
322+
}
323+
324+
void ISystem::CAsyncQueue::handle_request(SRequestType& req, SRequestParams_READ& param) {
325+
base_t::notify_future<size_t>(req, param.file->asyncRead(param.buffer, param.offset, param.size));
326+
}
327+
328+
void ISystem::CAsyncQueue::handle_request(SRequestType& req, SRequestParams_WRITE& param) {
329+
base_t::notify_future<size_t>(req, param.file->asyncWrite(param.buffer, param.offset, param.size));
332330
}
333331

334332
bool ISystem::ICaller::invalidateMapping(IFile* file, size_t offset, size_t size)

src/nbl/system/ISystemPOSIX.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "nbl/system/ISystemPOSIX.h"
22
#include "nbl/system/CFilePOSIX.h"
33

4+
#include "nbl/system/IFile.h"
5+
46
using namespace nbl;
57
using namespace nbl::system;
68

7-
#if defined(__unix__)
9+
#if defined(_NBL_PLATFORM_LINUX_) || defined (_NBL_PLATFORM_ANDROID_)
10+
811
#include <fcntl.h>
912
#include <sys/mman.h>
1013
#include <sys/stat.h>
@@ -13,7 +16,7 @@ core::smart_refctd_ptr<ISystemFile> ISystemPOSIX::CCaller::createFile(const std:
1316
{
1417
const bool writeAccess = flags.value&IFile::ECF_WRITE;
1518
int createFlags = O_LARGEFILE|(writeAccess ? O_CREAT:0);
16-
switch (_flags.value&IFile::ECF_READ_WRITE))
19+
switch (flags.value&IFile::ECF_READ_WRITE)
1720
{
1821
case IFile::ECF_READ:
1922
createFlags |= O_RDONLY;
@@ -30,7 +33,7 @@ core::smart_refctd_ptr<ISystemFile> ISystemPOSIX::CCaller::createFile(const std:
3033
}
3134

3235
CFilePOSIX::native_file_handle_t _native = -1;
33-
auto filenameStream = _filename.string();
36+
auto filenameStream = filename.string();
3437
const char* name_c_str = filenameStream.c_str();
3538
// only create a new file if we're going to be writing
3639
if (writeAccess)
@@ -60,7 +63,7 @@ core::smart_refctd_ptr<ISystemFile> ISystemPOSIX::CCaller::createFile(const std:
6063

6164
// map if needed
6265
void* _mappedPtr = nullptr;
63-
if (_flags.value & ECF_MAPPABLE)
66+
if (flags.value & IFile::ECF_MAPPABLE)
6467
{
6568
const int mappingFlags = ((flags.value&IFile::ECF_READ) ? PROT_READ:0)|(writeAccess ? PROT_WRITE:0);
6669
_mappedPtr = mmap((caddr_t)0, _size, mappingFlags, MAP_PRIVATE, _native, 0);
@@ -71,6 +74,6 @@ core::smart_refctd_ptr<ISystemFile> ISystemPOSIX::CCaller::createFile(const std:
7174
}
7275
}
7376

74-
return core::make_smart_refctd_ptr<CFilePOSIX>(core::smart_refctd_ptr<ISystem>(m_system),filename,flags,_mappedPtr,_size,_native);
77+
return core::make_smart_refctd_ptr<CFilePOSIX>(core::smart_refctd_ptr<ISystem>(m_system),path(filename),flags,_mappedPtr,_size,_native);
7578
}
7679
#endif

0 commit comments

Comments
 (0)