Implement pImpl pattern for KRContext/Context, fix crash in KRDataBlock
This commit is contained in:
48
kraken/Context.cpp
Normal file
48
kraken/Context.cpp
Normal file
@@ -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
|
||||||
@@ -181,7 +181,7 @@ KRDataBlock *KRDataBlock::getSubBlock(int start, int length)
|
|||||||
|
|
||||||
new_block->m_data_size = length;
|
new_block->m_data_size = length;
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
if(m_hPackFile) {
|
if(m_hPackFile != INVALID_HANDLE_VALUE) {
|
||||||
new_block->m_hPackFile = m_hPackFile;
|
new_block->m_hPackFile = m_hPackFile;
|
||||||
#elif defined(__APPLE__) || defined(ANDROID)
|
#elif defined(__APPLE__) || defined(ANDROID)
|
||||||
if (m_fdPackFile) {
|
if (m_fdPackFile) {
|
||||||
@@ -220,7 +220,7 @@ size_t KRDataBlock::getSize() const {
|
|||||||
void KRDataBlock::expand(size_t size)
|
void KRDataBlock::expand(size_t size)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#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)
|
#elif defined(__APPLE__) || defined(ANDROID)
|
||||||
if (m_data == NULL && m_fdPackFile == 0) {
|
if (m_data == NULL && m_fdPackFile == 0) {
|
||||||
#else
|
#else
|
||||||
@@ -275,7 +275,7 @@ void KRDataBlock::copy(void *dest) {
|
|||||||
// Copy a range of data to the destination pointer
|
// Copy a range of data to the destination pointer
|
||||||
void KRDataBlock::copy(void *dest, int start, int count) {
|
void KRDataBlock::copy(void *dest, int start, int count) {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#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
|
// Optimization: If we haven't mmap'ed or malloced the data already, pread() it directly from the file into the buffer
|
||||||
|
|
||||||
LARGE_INTEGER distance;
|
LARGE_INTEGER distance;
|
||||||
@@ -499,7 +499,7 @@ void KRDataBlock::unlock()
|
|||||||
|
|
||||||
// Memory mapped file; ensure data is unmapped from ram
|
// Memory mapped file; ensure data is unmapped from ram
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
if (m_hPackFile) {
|
if (m_hPackFile != INVALID_HANDLE_VALUE) {
|
||||||
#elif defined(__APPLE__) || defined(ANDROID)
|
#elif defined(__APPLE__) || defined(ANDROID)
|
||||||
if(m_fdPackFile) {
|
if(m_fdPackFile) {
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -35,13 +35,15 @@ namespace kraken {
|
|||||||
|
|
||||||
class Context
|
class Context
|
||||||
{
|
{
|
||||||
|
class impl;
|
||||||
|
public:
|
||||||
|
static Context* Get();
|
||||||
private:
|
private:
|
||||||
Context();
|
Context();
|
||||||
~Context();
|
~Context();
|
||||||
|
impl* mImpl;
|
||||||
};
|
};
|
||||||
|
|
||||||
Context* GetContext();
|
|
||||||
|
|
||||||
} // namepsace kraken
|
} // namepsace kraken
|
||||||
|
|
||||||
#endif // KRAKEN_CONTEXT_H
|
#endif // KRAKEN_CONTEXT_H
|
||||||
|
|||||||
@@ -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)
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
||||||
{
|
{
|
||||||
|
Context* context = Context::Get();
|
||||||
|
|
||||||
MSG msg = { 0 };
|
MSG msg = { 0 };
|
||||||
WNDCLASS wc = { 0 };
|
WNDCLASS wc = { 0 };
|
||||||
wc.lpfnWndProc = WndProc;
|
wc.lpfnWndProc = WndProc;
|
||||||
|
|||||||
Reference in New Issue
Block a user