Skip to content

Commit a6ff1ae

Browse files
try not to pollute with windows headers
1 parent a1b82a3 commit a6ff1ae

9 files changed

+119
-91
lines changed

include/nbl/system/CColoredStdoutLoggerWin32.h

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,13 @@
66
namespace nbl::system
77
{
88
#ifdef _NBL_PLATFORM_WINDOWS_
9-
//instead of #include <Windows.h>
10-
#include "nbl/system/DefaultFuncPtrLoader.h"
11-
12-
class CColoredStdoutLoggerWin32 : public IThreadsafeLogger
9+
class NBL_API2 CColoredStdoutLoggerWin32 : public IThreadsafeLogger
1310
{
14-
HANDLE m_native_console;
11+
void* m_native_console;
1512

16-
public:
17-
CColoredStdoutLoggerWin32(core::bitflag<E_LOG_LEVEL> logLevelMask = ILogger::defaultLogMask()) : IThreadsafeLogger(logLevelMask)
18-
{
19-
m_native_console = GetStdHandle(STD_OUTPUT_HANDLE);
20-
}
13+
void threadsafeLog_impl(const std::string_view& fmt, E_LOG_LEVEL logLevel, va_list args) override;
2114

22-
private:
23-
virtual void threadsafeLog_impl(const std::string_view& fmt, E_LOG_LEVEL logLevel, va_list args) override
24-
{
25-
SetConsoleTextAttribute(m_native_console, getConsoleColor(logLevel));
26-
printf(constructLogString(fmt, logLevel, args).data());
27-
fflush(stdout);
28-
SetConsoleTextAttribute(m_native_console, 15); // restore to white
29-
}
30-
31-
int getConsoleColor(E_LOG_LEVEL level)
15+
inline int getConsoleColor(E_LOG_LEVEL level)
3216
{
3317
switch (level)
3418
{
@@ -60,6 +44,9 @@ class CColoredStdoutLoggerWin32 : public IThreadsafeLogger
6044
}
6145
return 0;
6246
}
47+
48+
public:
49+
CColoredStdoutLoggerWin32(core::bitflag<E_LOG_LEVEL> logLevelMask = ILogger::defaultLogMask());
6350
};
6451

6552
#endif

include/nbl/system/CSystemWin32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#include "nbl/system/ISystem.h"
66

77

8+
89
#ifdef _NBL_PLATFORM_WINDOWS_
10+
#define WIN32_LEAN_AND_MEAN
11+
#include <windows.h>
912
#include <delayimp.h>
1013

1114
namespace nbl::system

include/nbl/system/DefaultFuncPtrLoader.h

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,41 @@
1-
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2018-2023 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4-
54
#ifndef _NBL_SYSTEM_DEFAULT_FUNC_PTR_LOADER_H_INCLUDED_
65
#define _NBL_SYSTEM_DEFAULT_FUNC_PTR_LOADER_H_INCLUDED_
76

8-
#include "nbl/system/FuncPtrLoader.h"
97

10-
#if defined(_NBL_WINDOWS_API_)
11-
#define WIN32_LEAN_AND_MEAN
12-
#include <windows.h>
13-
#include <stdio.h>
14-
#elif defined(_NBL_POSIX_API_)
15-
#include <dlfcn.h>
16-
#endif
8+
#include "nbl/system/FuncPtrLoader.h"
179

1810

1911
namespace nbl::system
2012
{
2113

2214
class DefaultFuncPtrLoader final : FuncPtrLoader
2315
{
24-
protected:
25-
#if defined(_NBL_WINDOWS_API_)
26-
HMODULE lib;
27-
#elif defined(_NBL_POSIX_API_)
28-
void* lib;
29-
#endif
30-
public:
31-
DefaultFuncPtrLoader() : lib(NULL) {}
32-
DefaultFuncPtrLoader(const char* name) : DefaultFuncPtrLoader()
33-
{
34-
if (!name)
35-
return;
16+
void* lib;
3617

37-
// TODO: redo with either LoadLibraryExA or SetDllDirectoryA and linux equivalents to allow loading shared libraries
38-
// with other shared library dependencies from regular directories without changing CWD (which is not thread safe)
39-
#if defined(_NBL_WINDOWS_API_)
40-
std::string libname(name);
41-
libname += ".dll";
42-
lib = LoadLibraryA(libname.c_str());
43-
if (!lib)
44-
lib = LoadLibraryA(name);
45-
#elif defined(_NBL_POSIX_API_)
46-
std::string libname("lib");
47-
libname += name;
48-
libname += ".so";
49-
lib = dlopen(libname.c_str(),RTLD_LAZY);
50-
if (!lib)
51-
lib = dlopen(name,RTLD_LAZY);
52-
#endif
53-
}
54-
DefaultFuncPtrLoader(DefaultFuncPtrLoader&& other) : DefaultFuncPtrLoader()
18+
public:
19+
inline DefaultFuncPtrLoader() : lib(nullptr) {}
20+
NBL_API2 DefaultFuncPtrLoader(const char* name);
21+
inline DefaultFuncPtrLoader(DefaultFuncPtrLoader&& other) : DefaultFuncPtrLoader()
5522
{
5623
operator=(std::move(other));
5724
}
58-
~DefaultFuncPtrLoader()
59-
{
60-
if (lib != NULL)
61-
#if defined(_NBL_WINDOWS_API_)
62-
FreeLibrary(lib);
63-
#elif defined(_NBL_POSIX_API_)
64-
dlclose(lib);
65-
#endif
66-
}
25+
NBL_API2 ~DefaultFuncPtrLoader();
6726

6827
inline DefaultFuncPtrLoader& operator=(DefaultFuncPtrLoader&& other)
6928
{
70-
std::swap(lib, other.lib);
29+
std::swap(lib,other.lib);
7130
return *this;
7231
}
7332

7433
inline bool isLibraryLoaded() override final
7534
{
76-
return lib!=NULL;
35+
return lib!=nullptr;
7736
}
7837

79-
inline void* loadFuncPtr(const char* funcname) override final
80-
{
81-
if (isLibraryLoaded())
82-
{
83-
#if defined(_NBL_WINDOWS_API_)
84-
return GetProcAddress(lib,funcname);
85-
#elif defined(_NBL_POSIX_API_)
86-
return dlsym(lib,funcname);
87-
#endif
88-
}
89-
return nullptr;
90-
}
38+
void* loadFuncPtr(const char* funcname) override final;
9139
};
9240

9341
}

include/nbl/system/DynamicLibraryFunctionPointer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Copyright (C) 2018-2022 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2018-2023 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4-
#ifndef __NBL_SYSTEM_DYNAMIC_LIBRARY_FUNCTION_POINTER_H_INCLUDED__
5-
#define __NBL_SYSTEM_DYNAMIC_LIBRARY_FUNCTION_POINTER_H_INCLUDED__
4+
#ifndef _NBL_SYSTEM_DYNAMIC_LIBRARY_FUNCTION_POINTER_H_INCLUDED_
5+
#define _NBL_SYSTEM_DYNAMIC_LIBRARY_FUNCTION_POINTER_H_INCLUDED_
66

77

88
#include "nbl/core/declarations.h"

include/nbl/system/FuncPtrLoader.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2018-2023 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4-
5-
#ifndef __NBL_SYSTEM_FUNC_PTR_LOADER_H_INCLUDED__
6-
#define __NBL_SYSTEM_FUNC_PTR_LOADER_H_INCLUDED__
4+
#ifndef _NBL_SYSTEM_FUNC_PTR_LOADER_H_INCLUDED_
5+
#define _NBL_SYSTEM_FUNC_PTR_LOADER_H_INCLUDED_
76

87
#include "nbl/system/DynamicLibraryFunctionPointer.h"
98

src/nbl/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,15 @@ set(NBL_CORE_SOURCES
166166
${NBL_ROOT_PATH}/src/nbl/core/IReferenceCounted.cpp
167167
)
168168
set(NBL_SYSTEM_SOURCES
169+
${NBL_ROOT_PATH}/src/nbl/system/DefaultFuncPtrLoader.cpp
169170
${NBL_ROOT_PATH}/src/nbl/system/IFileBase.cpp
170171
${NBL_ROOT_PATH}/src/nbl/system/ILogger.cpp
171172
${NBL_ROOT_PATH}/src/nbl/system/CArchiveLoaderZip.cpp
172173
${NBL_ROOT_PATH}/src/nbl/system/CArchiveLoaderTar.cpp
173174
${NBL_ROOT_PATH}/src/nbl/system/CAPKResourcesArchive.cpp
174175
${NBL_ROOT_PATH}/src/nbl/system/ISystem.cpp
175176
${NBL_ROOT_PATH}/src/nbl/system/IFileArchive.cpp
177+
${NBL_ROOT_PATH}/src/nbl/system/CColoredStdoutLoggerWin32.cpp
176178
${NBL_ROOT_PATH}/src/nbl/system/CStdoutLoggerAndroid.cpp
177179
${NBL_ROOT_PATH}/src/nbl/system/CFileViewVirtualAllocatorWin32.cpp
178180
${NBL_ROOT_PATH}/src/nbl/system/CFileViewVirtualAllocatorPOSIX.cpp
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "nbl/system/CColoredStdoutLoggerWin32.h"
2+
3+
using namespace nbl;
4+
using namespace nbl::system;
5+
6+
#ifdef _NBL_PLATFORM_WINDOWS_
7+
#define WIN32_LEAN_AND_MEAN
8+
#include <windows.h>
9+
10+
CColoredStdoutLoggerWin32::CColoredStdoutLoggerWin32(core::bitflag<E_LOG_LEVEL> logLevelMask) : IThreadsafeLogger(logLevelMask)
11+
{
12+
m_native_console = GetStdHandle(STD_OUTPUT_HANDLE);
13+
}
14+
15+
void CColoredStdoutLoggerWin32::threadsafeLog_impl(const std::string_view& fmt, E_LOG_LEVEL logLevel, va_list args)
16+
{
17+
SetConsoleTextAttribute(m_native_console, getConsoleColor(logLevel));
18+
printf(constructLogString(fmt, logLevel, args).data());
19+
fflush(stdout);
20+
SetConsoleTextAttribute(m_native_console, 15); // restore to white
21+
}
22+
#endif

src/nbl/system/CSystemWin32.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ using namespace nbl;
55
using namespace nbl::system;
66

77
#ifdef _NBL_PLATFORM_WINDOWS_
8-
#include <windows.h>
98
#include <powerbase.h>
109

1110
//LOL the struct definition wasn't added to winapi headers do they ask to declare them yourself
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "nbl/system/DefaultFuncPtrLoader.h"
2+
3+
#if defined(_NBL_WINDOWS_API_)
4+
#define WIN32_LEAN_AND_MEAN
5+
#include <windows.h>
6+
#include <stdio.h>
7+
#elif defined(_NBL_POSIX_API_)
8+
#include <dlfcn.h>
9+
#endif
10+
11+
12+
using namespace nbl;
13+
using namespace nbl::system;
14+
15+
16+
#if defined(_NBL_WINDOWS_API_)
17+
#define LIB reinterpret_cast<HMODULE&>(lib)
18+
#elif defined(_NBL_POSIX_API_)
19+
#define lib
20+
#endif
21+
22+
DefaultFuncPtrLoader::DefaultFuncPtrLoader(const char* name) : DefaultFuncPtrLoader()
23+
{
24+
if (!name)
25+
return;
26+
27+
// TODO: redo with either LoadLibraryExA or SetDllDirectoryA and linux equivalents to allow loading shared libraries
28+
// with other shared library dependencies from regular directories without changing CWD (which is not thread safe)
29+
#if defined(_NBL_WINDOWS_API_)
30+
std::string libname(name);
31+
libname += ".dll";
32+
LIB = LoadLibraryA(libname.c_str());
33+
if (!lib)
34+
LIB = LoadLibraryA(name);
35+
#elif defined(_NBL_POSIX_API_)
36+
std::string libname("lib");
37+
libname += name;
38+
libname += ".so";
39+
LIB = dlopen(libname.c_str(),RTLD_LAZY);
40+
if (!lib)
41+
LIB = dlopen(name,RTLD_LAZY);
42+
#endif
43+
}
44+
45+
DefaultFuncPtrLoader::~DefaultFuncPtrLoader()
46+
{
47+
if (lib!=nullptr)
48+
{
49+
#if defined(_NBL_WINDOWS_API_)
50+
FreeLibrary(LIB);
51+
#elif defined(_NBL_POSIX_API_)
52+
dlclose(LIB);
53+
#endif
54+
}
55+
}
56+
57+
void* DefaultFuncPtrLoader::loadFuncPtr(const char* funcname)
58+
{
59+
if (isLibraryLoaded())
60+
{
61+
#if defined(_NBL_WINDOWS_API_)
62+
return GetProcAddress(LIB,funcname);
63+
#elif defined(_NBL_POSIX_API_)
64+
return dlsym(lib,funcname);
65+
#endif
66+
}
67+
return nullptr;
68+
}

0 commit comments

Comments
 (0)