Commit 7f8fb838 authored by captainwong's avatar captainwong

add win32.h

parent 37046691
...@@ -26,6 +26,7 @@ or else it might cause multiple constructions. ...@@ -26,6 +26,7 @@ or else it might cause multiple constructions.
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include "utf8.h" #include "utf8.h"
#include "chrono_wrapper.h" #include "chrono_wrapper.h"
#include "singleton.h"
namespace jlib namespace jlib
{ {
...@@ -36,9 +37,9 @@ namespace jlib ...@@ -36,9 +37,9 @@ namespace jlib
#define JLOGB(b, l) jlib::log::dump_hex(b, l) #define JLOGB(b, l) jlib::log::dump_hex(b, l)
#define JLOGASC(b, l) jlib::log::dump_ascii(b, l) #define JLOGASC(b, l) jlib::log::dump_ascii(b, l)
#define IMPLEMENT_CLASS_LOG_STATIC_MEMBER jlib::log* jlib::log::instance_ = nullptr; //#define IMPLEMENT_CLASS_LOG_STATIC_MEMBER jlib::log* jlib::log::instance_ = nullptr;
class log : private boost::noncopyable class log : public dp::singleton<log>
{ {
enum { max_output_size = 1024 * 64, max_single_log_file_size = 1024 * 1024 * 10 }; enum { max_output_size = 1024 * 64, max_single_log_file_size = 1024 * 1024 * 10 };
...@@ -52,7 +53,7 @@ private: ...@@ -52,7 +53,7 @@ private:
std::string log_file_path_ = ""; std::string log_file_path_ = "";
std::string line_prefix_ = ""; std::string line_prefix_ = "";
std::mutex lock_; std::mutex lock_;
static log* instance_; //static log* instance_;
public: public:
// initializers, they should be called right after get_instance // initializers, they should be called right after get_instance
...@@ -70,20 +71,20 @@ public: ...@@ -70,20 +71,20 @@ public:
public: public:
static log* get_instance() //static log* get_instance()
{ //{
/* // /*
Warnning: // Warnning:
The singleton pattern that log implemented is not thread-safe, // The singleton pattern that log implemented is not thread-safe,
you should call log::get_instance() once in your main thread, // you should call log::get_instance() once in your main thread,
or else it might cause multiple constructions. // or else it might cause multiple constructions.
*/ // */
if (log::instance_ == nullptr) { // if (log::instance_ == nullptr) {
static log log_; // static log log_;
log::instance_ = &log_; // log::instance_ = &log_;
} // }
return log::instance_; // return log::instance_;
} //}
~log() ~log()
{ {
...@@ -105,7 +106,7 @@ public: ...@@ -105,7 +106,7 @@ public:
static void dump_hex(const char* buff, size_t buff_len) static void dump_hex(const char* buff, size_t buff_len)
{ {
try { try {
log* instance = log::get_instance(); auto instance = log::get_instance();
if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) { if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) {
size_t output_len = buff_len * 6 + 64; size_t output_len = buff_len * 6 + 64;
...@@ -132,7 +133,7 @@ public: ...@@ -132,7 +133,7 @@ public:
static void dump_ascii(const char* buff, size_t buff_len) static void dump_ascii(const char* buff, size_t buff_len)
{ {
try { try {
log* instance = log::get_instance(); auto instance = log::get_instance();
if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) { if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) {
size_t output_len = buff_len * 6 + 64; size_t output_len = buff_len * 6 + 64;
...@@ -159,7 +160,7 @@ public: ...@@ -159,7 +160,7 @@ public:
static void log_utf16(const wchar_t* format, ...) static void log_utf16(const wchar_t* format, ...)
{ {
try { try {
log* instance = log::get_instance(); auto instance = log::get_instance();
if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) { if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) {
wchar_t buf[max_output_size], *p; wchar_t buf[max_output_size], *p;
...@@ -201,7 +202,7 @@ public: ...@@ -201,7 +202,7 @@ public:
static void log_utf8(const char* format, ...) static void log_utf8(const char* format, ...)
{ {
try { try {
log* instance = log::get_instance(); auto instance = log::get_instance();
if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) { if (instance->log_to_file_ || instance->log_to_dbg_view_ || instance->log_to_console_) {
char buf[max_output_size], *p; char buf[max_output_size], *p;
...@@ -216,7 +217,7 @@ public: ...@@ -216,7 +217,7 @@ public:
*p++ = '\n'; *p++ = '\n';
*p = '\0'; *p = '\0';
instance_->output(instance->format_msg(buf)); instance->output(instance->format_msg(buf));
} }
} catch (...) { } catch (...) {
assert(0); assert(0);
......
...@@ -12,45 +12,7 @@ ...@@ -12,45 +12,7 @@
#include "micro_getter_setter.h" #include "micro_getter_setter.h"
//#include "micro_singleton.h" //#include "micro_singleton.h"
#include "singleton.h" #include "singleton.h"
#include "win32.h"
namespace jlib {
inline std::wstring get_exe_path()
{
wchar_t path[1024] = { 0 };
GetModuleFileName(nullptr, path, 1024);
std::wstring::size_type pos = std::wstring(path).find_last_of(L"\\/");
return std::wstring(path).substr(0, pos);
}
inline std::string get_exe_path_a()
{
char path[1024] = { 0 };
GetModuleFileNameA(nullptr, path, 1024);
std::string::size_type pos = std::string(path).find_last_of("\\/");
return std::string(path).substr(0, pos);
}
class auto_timer : public boost::noncopyable
{
private:
int m_timer_id;
DWORD m_time_out;
HWND m_hWnd;
public:
auto_timer(HWND hWnd, int timerId, DWORD timeout) : m_hWnd(hWnd), m_timer_id(timerId), m_time_out(timeout)
{
KillTimer(hWnd, m_timer_id);
}
~auto_timer()
{
SetTimer(m_hWnd, m_timer_id, m_time_out, nullptr);
}
};
};
#pragma once
#include <windows.h>
#include <string>
namespace jlib {
inline std::wstring get_exe_path()
{
wchar_t path[1024] = { 0 };
GetModuleFileName(nullptr, path, 1024);
std::wstring::size_type pos = std::wstring(path).find_last_of(L"\\/");
return std::wstring(path).substr(0, pos);
}
inline std::string get_exe_path_a()
{
char path[1024] = { 0 };
GetModuleFileNameA(nullptr, path, 1024);
std::string::size_type pos = std::string(path).find_last_of("\\/");
return std::string(path).substr(0, pos);
}
inline BOOL Win32CenterWindow(HWND hwndWindow)
{
HWND hwndParent;
RECT rectWindow, rectParent;
if ((hwndParent = GetParent(hwndWindow)) == NULL) {
hwndParent = GetDesktopWindow();
}
// make the window relative to its parent
if (hwndParent != NULL) {
GetWindowRect(hwndWindow, &rectWindow);
GetWindowRect(hwndParent, &rectParent);
int nWidth = rectWindow.right - rectWindow.left;
int nHeight = rectWindow.bottom - rectWindow.top;
int nX = ((rectParent.right - rectParent.left) - nWidth) / 2 + rectParent.left;
int nY = ((rectParent.bottom - rectParent.top) - nHeight) / 2 + rectParent.top;
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
// make sure that the dialog box never moves outside of the screen
if (nX < 0) nX = 0;
if (nY < 0) nY = 0;
if (nX + nWidth > nScreenWidth) nX = nScreenWidth - nWidth;
if (nY + nHeight > nScreenHeight) nY = nScreenHeight - nHeight;
MoveWindow(hwndWindow, nX, nY, nWidth, nHeight, FALSE);
return TRUE;
}
return FALSE;
}
class auto_timer : public boost::noncopyable
{
private:
int m_timer_id;
DWORD m_time_out;
HWND m_hWnd;
public:
auto_timer(HWND hWnd, int timerId, DWORD timeout) : m_hWnd(hWnd), m_timer_id(timerId), m_time_out(timeout)
{
KillTimer(hWnd, m_timer_id);
}
~auto_timer()
{
SetTimer(m_hWnd, m_timer_id, m_time_out, nullptr);
}
};
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment