From 3be32926b95040ed55958ec7eb45e7861ca0b836 Mon Sep 17 00:00:00 2001 From: Kearwood Kip Gilbert Date: Sun, 14 Jul 2019 15:18:48 -0700 Subject: [PATCH] Implement pImpl pattern for KRContext/Context, fix crash in KRDataBlock --- kraken/Context.cpp | 48 +++++++++++++++++++++++++++++ kraken/KRDataBlock.cpp | 8 ++--- kraken/public/context.h | 6 ++-- tests/smoke/hello_cube/main_win.cpp | 2 ++ 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 kraken/Context.cpp diff --git a/kraken/Context.cpp b/kraken/Context.cpp new file mode 100644 index 0000000..772d174 --- /dev/null +++ b/kraken/Context.cpp @@ -0,0 +1,48 @@ +#include "public/context.h" + +#include "KRContext.h" + +namespace kraken { + +Context* sContext = nullptr; + +class Context::impl +{ +public: + impl(); + ~impl(); + +private: + KRContext mContext; +}; + +/* static */ +Context* Context::Get() +{ + if (!sContext) { + sContext = new Context(); + } + return sContext; +} + +Context::Context() +{ + mImpl = new impl(); +} + +Context::~Context() +{ + delete mImpl; +} + +Context::impl::impl() +{ + +} + +Context::impl::~impl() +{ + +} + +}; // namespace kraken diff --git a/kraken/KRDataBlock.cpp b/kraken/KRDataBlock.cpp index ae03868..0b66845 100755 --- a/kraken/KRDataBlock.cpp +++ b/kraken/KRDataBlock.cpp @@ -181,7 +181,7 @@ KRDataBlock *KRDataBlock::getSubBlock(int start, int length) new_block->m_data_size = length; #if defined(_WIN32) || defined(_WIN64) - if(m_hPackFile) { + if(m_hPackFile != INVALID_HANDLE_VALUE) { new_block->m_hPackFile = m_hPackFile; #elif defined(__APPLE__) || defined(ANDROID) if (m_fdPackFile) { @@ -220,7 +220,7 @@ size_t KRDataBlock::getSize() const { void KRDataBlock::expand(size_t size) { #if defined(_WIN32) || defined(_WIN64) - if(m_data == NULL && m_hPackFile == 0) { + if(m_data == NULL && m_hPackFile == INVALID_HANDLE_VALUE) { #elif defined(__APPLE__) || defined(ANDROID) if (m_data == NULL && m_fdPackFile == 0) { #else @@ -275,7 +275,7 @@ void KRDataBlock::copy(void *dest) { // Copy a range of data to the destination pointer void KRDataBlock::copy(void *dest, int start, int count) { #if defined(_WIN32) || defined(_WIN64) - if (m_lockCount == 0 && m_hPackFile != 0) { + if (m_lockCount == 0 && m_hPackFile != INVALID_HANDLE_VALUE) { // Optimization: If we haven't mmap'ed or malloced the data already, pread() it directly from the file into the buffer LARGE_INTEGER distance; @@ -499,7 +499,7 @@ void KRDataBlock::unlock() // Memory mapped file; ensure data is unmapped from ram #if defined(_WIN32) || defined(_WIN64) - if (m_hPackFile) { + if (m_hPackFile != INVALID_HANDLE_VALUE) { #elif defined(__APPLE__) || defined(ANDROID) if(m_fdPackFile) { #else diff --git a/kraken/public/context.h b/kraken/public/context.h index 800ed49..3aff16a 100644 --- a/kraken/public/context.h +++ b/kraken/public/context.h @@ -35,13 +35,15 @@ namespace kraken { class Context { +class impl; +public: + static Context* Get(); private: Context(); ~Context(); + impl* mImpl; }; -Context* GetContext(); - } // namepsace kraken #endif // KRAKEN_CONTEXT_H diff --git a/tests/smoke/hello_cube/main_win.cpp b/tests/smoke/hello_cube/main_win.cpp index 815c6ea..185be3d 100644 --- a/tests/smoke/hello_cube/main_win.cpp +++ b/tests/smoke/hello_cube/main_win.cpp @@ -7,6 +7,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { + Context* context = Context::Get(); + MSG msg = { 0 }; WNDCLASS wc = { 0 }; wc.lpfnWndProc = WndProc;