Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
7ef14a28
Commit
7ef14a28
authored
Feb 08, 2015
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add macro observer
parent
d9aa7362
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
19 deletions
+121
-19
LocalLock.h
LocalLock.h
+18
-1
Log.h
Log.h
+28
-18
observer_macro.h
observer_macro.h
+75
-0
No files found.
LocalLock.h
View file @
7ef14a28
...
...
@@ -28,13 +28,27 @@ private:
class
CLock
{
public
:
CLock
(
)
/*CLock(PCRITICAL_SECTION cs) : m_cs(cs), bNullInit(FALSE
)
{
if (m_cs == NULL) {
bNullInit = TRUE;
m_cs = new CRITICAL_SECTION();
}
InitializeCriticalSection(m_cs);
}*/
CLock
()
/* : m_cs(NULL), bNullInit(FALSE)*/
{
/*if (m_cs == NULL) {
m_cs = new CRITICAL_SECTION();
}*/
InitializeCriticalSection
(
&
m_cs
);
}
~
CLock
()
{
DeleteCriticalSection
(
&
m_cs
);
/*if (bNullInit) {
delete m_cs;
}*/
}
void
Lock
()
{
...
...
@@ -44,8 +58,11 @@ public:
{
LeaveCriticalSection
(
&
m_cs
);
}
const
LPCRITICAL_SECTION
GetObject
()
{
return
&
m_cs
;
}
private
:
CRITICAL_SECTION
m_cs
;
//BOOL bNullInit;
};
...
...
Log.h
View file @
7ef14a28
...
...
@@ -22,6 +22,8 @@
#pragma once
#endif // _MSC_VER > 1000
#include <assert.h>
//#include "dbghlpapi.h"
#ifdef TRACE
...
...
@@ -33,7 +35,8 @@
CRITICAL_SECTION CLog::m_cs;\
CLock CLog::m_lockForSingleton;\
char CLog::m_ansiBuf[MAX_OUTPUT_LEN];\
wchar_t CLog::m_utf16Buf[MAX_OUTPUT_LEN];
wchar_t CLog::m_utf16Buf[MAX_OUTPUT_LEN];\
CLog* CLog::m_pInstance = NULL;
/*
#define TRACEFUNCNAME \
...
...
@@ -66,12 +69,13 @@ private:
static
CLock
m_lockForSingleton
;
static
char
m_ansiBuf
[
MAX_OUTPUT_LEN
];
static
wchar_t
m_utf16Buf
[
MAX_OUTPUT_LEN
];
static
CLog
*
m_pInstance
;
public
:
static
CString
GetPrivateLogPath
(){
static
CString
strPrivateMapFolder
=
_T
(
""
)
;
static
const
wchar_t
*
GetPrivateLogPath
(){
static
wchar_t
strPrivateMapFolder
[
1024
]
=
{
0
}
;
static
BOOL
b
=
TRUE
;
if
(
b
){
strPrivateMapFolder
.
Format
(
_T
(
"%s
\\
Log"
),
GetModuleFilePath
());
wsprintf
(
strPrivateMapFolder
,
_T
(
"%s
\\
Log"
),
GetModuleFilePath
());
b
=
FALSE
;
}
return
strPrivateMapFolder
;
...
...
@@ -79,9 +83,12 @@ public:
static
CLog
*
GetInstance
(){
m_lockForSingleton
.
Lock
();
static
CLog
log
;
if
(
CLog
::
m_pInstance
==
NULL
)
{
static
CLog
log
;
CLog
::
m_pInstance
=
&
log
;
}
m_lockForSingleton
.
UnLock
();
return
&
log
;
return
CLog
::
m_pInstance
;
}
~
CLog
(){
...
...
@@ -148,8 +155,7 @@ public:
}
}
catch
(...){
AfxMessageBox
(
_T
(
"CLog::WriteLog"
));
ASSERT
(
0
);
assert
(
0
);
}
}
...
...
@@ -190,8 +196,7 @@ public:
}
}
catch
(...){
AfxMessageBox
(
_T
(
"CLog::WriteLog"
));
ASSERT
(
0
);
assert
(
0
);
}
}
...
...
@@ -235,8 +240,7 @@ public:
}
}
catch
(...){
AfxMessageBox
(
_T
(
"CLog::WriteLog"
));
ASSERT
(
0
);
assert
(
0
);
}
}
...
...
@@ -361,14 +365,16 @@ protected:
if
(
pfile
==
NULL
){
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"wb"
));
if
(
pfile
==
NULL
){
AfxMessageBox
(
_T
(
"Create log file failed."
));
MessageBox
(
NULL
,
_T
(
"Create log file failed."
),
NULL
,
0
);
ASSERT
(
0
);
return
NULL
;
}
}
else
{
fclose
(
pfile
);
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"ab"
));
if
(
pfile
==
NULL
){
AfxMessageBox
(
_T
(
"Open log file failed."
));
MessageBox
(
NULL
,
_T
(
"Open log file failed."
),
NULL
,
0
);
ASSERT
(
0
);
return
NULL
;
}
}
...
...
@@ -380,11 +386,15 @@ protected:
void
CreateFileName
(){
SYSTEMTIME
st
;
GetLocalTime
(
&
st
);
CString
path
=
GetPrivateLogPath
();
const
wchar_t
*
path
=
GetPrivateLogPath
();
CreateDirectory
(
path
,
NULL
);
wsprintf
(
g_szFileName
,
_T
(
"%s
\\
%04d-%02d-%02d-w%d-%02d-%02d-%02d"
),
path
,
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
(
st
.
wDayOfWeek
!=
0
)
?
st
.
wDayOfWeek
:
7
,
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
);
wchar_t
exe
[
1024
]
=
{
0
};
GetModuleFileName
(
NULL
,
exe
,
1023
);
wsprintf
(
g_szFileName
,
_T
(
"%s
\\
%s-%04d-%02d-%02d-w%d-%02d-%02d-%02d"
),
path
,
CFileOper
::
GetFileNameFromPathName
(
exe
),
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
(
st
.
wDayOfWeek
!=
0
)
?
st
.
wDayOfWeek
:
7
,
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
);
lstrcat
(
g_szFileName
,
_T
(
".log"
));
}
...
...
observer_macro.h
0 → 100644
View file @
7ef14a28
// place this macro in your class's header file, in your class's definition
#define DECLARE_OBSERVER(callback, param_type) \
protected: \
typedef struct callback##Info { \
DECLARE_UNCOPYABLE(callback##Info) \
public: \
callback##Info() : _udata(NULL), _on_result(NULL) {} \
callback##Info(void* udata, callback on_result) : _udata(udata), _on_result(on_result) {} \
void* _udata; \
callback _on_result; \
}callback##Info; \
typedef callback##Info _callbackInfo; \
typedef callback _callback; \
typedef param_type _param_type; \
std::list<_callbackInfo *> _observerList; \
public: \
void RegisterObserver(void* udata, callback cb); \
void UnRegisterObserver(void* udata); \
void NotifyObservers(_param_type param);
// place this macro in your class's cpp file
#define IMPLEMENT_OBSERVER(class_name) \
void class_name::RegisterObserver(void* udata, _callback cb) \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo* observer = *iter; \
if (observer->_udata == udata) { \
return; \
} \
iter++; \
} \
_callbackInfo *observer = new _callbackInfo(udata, cb); \
_observerList.insert(iter, observer); \
} \
void class_name::UnRegisterObserver(void* udata) \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo* observer = *iter; \
if (observer->_udata == udata) { \
delete observer; \
_observerList.erase(iter); \
break; \
} \
iter++; \
} \
} \
void class_name::NotifyObservers(_param_type param) \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo * observer = *iter++; \
observer->_on_result(observer->_udata, param); \
} \
}
// place this macro in your class's destruct function.
#define DESTROY_OBSERVER \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo * observer = *iter++; \
delete observer; \
} \
_observerList.clear(); \
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment