• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版4075bf405c5cd8fe54ab531af249dd2854af86a6 (tree)
时间2017-01-04 07:47:59
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Some clean-up and simplification for JobObject class.

更改概述

差异

--- a/include/MUtils/JobObject.h
+++ b/include/MUtils/JobObject.h
@@ -27,18 +27,18 @@ class QProcess;
2727
2828 namespace MUtils
2929 {
30- class MUTILS_API JobObject_Private;
31-
3230 class MUTILS_API JobObject
3331 {
3432 public:
3533 JobObject(void);
3634 ~JobObject(void);
3735
38- bool addProcessToJob(const QProcess *proc);
39- bool terminateJob(unsigned int exitCode);
36+ bool isObjectCreated(void);
37+ bool addProcessToJob(const QProcess *const process);
38+ bool terminateJob(const quint32 &exitCode);
4039
4140 private:
42- JobObject_Private *const p;
41+ uintptr_t m_jobPtr;
42+ MUTILS_NO_COPY(JobObject)
4343 };
4444 }
--- a/src/JobObject_Win32.cpp
+++ b/src/JobObject_Win32.cpp
@@ -30,44 +30,28 @@
3030 #define NOMINMAX
3131 #define WIN32_LEAN_AND_MEAN 1
3232 #include <Windows.h>
33-#include <MMSystem.h>
34-#include <ShellAPI.h>
35-#include <WinInet.h>
3633
37-namespace MUtils
38-{
39- class JobObject_Private
40- {
41- friend class JobObject;
42-
43- protected:
44- JobObject_Private(void)
45- {
46- m_hJobObject = NULL;
47- }
48-
49- HANDLE m_hJobObject;
50- };
51-}
34+//Utilities
35+#define PTR2HANDLE(X) reinterpret_cast<HANDLE>((X))
36+#define HANDLE2PTR(X) reinterpret_cast<uintptr_t>((X))
5237
5338 MUtils::JobObject::JobObject(void)
5439 :
55- p(new JobObject_Private())
40+ m_jobPtr(NULL)
5641 {
5742 const HANDLE jobObject = CreateJobObject(NULL, NULL);
5843 if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
5944 {
6045 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
6146 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
62- memset(&jobExtendedLimitInfo.BasicLimitInformation, 0, sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION));
6347 jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
6448 if(SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)))
6549 {
66- p->m_hJobObject = jobObject;
50+ m_jobPtr = HANDLE2PTR(jobObject);
6751 }
6852 else
6953 {
70- qWarning("Failed to set job object information!");
54+ qWarning("Failed to set up job object limit information!");
7155 CloseHandle(jobObject);
7256 }
7357 }
@@ -79,24 +63,27 @@ MUtils::JobObject::JobObject(void)
7963
8064 MUtils::JobObject::~JobObject(void)
8165 {
82- if(p->m_hJobObject)
66+ if(m_jobPtr)
8367 {
84- CloseHandle(p->m_hJobObject);
85- p->m_hJobObject = NULL;
68+ CloseHandle(PTR2HANDLE(m_jobPtr));
69+ m_jobPtr = NULL;
8670 }
71+}
8772
88- delete p;
73+bool MUtils::JobObject::isObjectCreated(void)
74+{
75+ return (bool) m_jobPtr;
8976 }
9077
91-bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
78+bool MUtils::JobObject::addProcessToJob(const QProcess *const process)
9279 {
93- if(!p->m_hJobObject)
80+ if(!m_jobPtr)
9481 {
9582 qWarning("Cannot assign process to job: No job bject available!");
9683 return false;
9784 }
9885
99- if(Q_PID pid = proc->pid())
86+ if(const Q_PID pid = process->pid())
10087 {
10188 DWORD exitCode;
10289 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
@@ -104,19 +91,16 @@ bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
10491 qWarning("Cannot assign process to job: Failed to query process status!");
10592 return false;
10693 }
107-
10894 if(exitCode != STILL_ACTIVE)
10995 {
11096 qWarning("Cannot assign process to job: Process is not running anymore!");
11197 return false;
11298 }
113-
114- if(!AssignProcessToJobObject(p->m_hJobObject, pid->hProcess))
99+ if(!AssignProcessToJobObject(PTR2HANDLE(m_jobPtr), pid->hProcess))
115100 {
116101 qWarning("Failed to assign process to job object!");
117102 return false;
118103 }
119-
120104 return true;
121105 }
122106 else
@@ -126,11 +110,11 @@ bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
126110 }
127111 }
128112
129-bool MUtils::JobObject::terminateJob(unsigned int exitCode)
113+bool MUtils::JobObject::terminateJob(const quint32 &exitCode)
130114 {
131- if(p->m_hJobObject)
115+ if(m_jobPtr)
132116 {
133- if(TerminateJobObject(p->m_hJobObject, exitCode))
117+ if(TerminateJobObject(PTR2HANDLE(m_jobPtr), exitCode))
134118 {
135119 return true;
136120 }
Show on old repository browser