From 63ab8672d3da6e8ab92ee8266847caaec2c09d07 Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Wed, 23 Feb 2011 06:08:30 -0500 Subject: [PATCH] Add create*Process functions to create process instances. This allows us to remove friendship of DFProcessEnumerator for Process subclasses. --- library/CMakeLists.txt | 1 + library/DFProcess-SHM.cpp | 6 +++++ library/DFProcess-linux-wine.cpp | 26 ++++++++++++++++++++++ library/DFProcess-linux.cpp | 22 ++++++++++++++++++ library/DFProcess-windows.cpp | 6 +++++ library/DFProcessEnumerator.cpp | 10 ++++----- library/private/LinuxProcess.h | 33 --------------------------- library/private/ProcessFactory.h | 38 ++++++++++++++++++++++++++++++++ library/private/SHMProcess.h | 3 +-- library/private/WindowsProcess.h | 3 +-- 10 files changed, 105 insertions(+), 43 deletions(-) create mode 100644 library/private/ProcessFactory.h diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index f22c53fd4..d54fc79d5 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -32,6 +32,7 @@ SET(PROJECT_HDRS_INTERNAL private/SHMProcess.h private/LinuxProcess.h private/WindowsProcess.h + private/ProcessFactory.h ) SET(PROJECT_HDRS diff --git a/library/DFProcess-SHM.cpp b/library/DFProcess-SHM.cpp index ad7ebc8a5..3246e567e 100644 --- a/library/DFProcess-SHM.cpp +++ b/library/DFProcess-SHM.cpp @@ -23,6 +23,7 @@ distribution. */ #include "Internal.h" #include "SHMProcess.h" +#include "ProcessFactory.h" #include "dfhack/VersionInfo.h" #include "dfhack/DFError.h" #include "shms.h" @@ -30,6 +31,11 @@ distribution. using namespace DFHack; +Process* DFHack::createSHMProcess(uint32_t pid, vector & known_versions) +{ + return new SHMProcess(pid, known_versions); +} + SHMProcess::SHMProcess(uint32_t PID, vector & known_versions) : d(new Private(this)) { diff --git a/library/DFProcess-linux-wine.cpp b/library/DFProcess-linux-wine.cpp index 419f633b9..a75b8077d 100644 --- a/library/DFProcess-linux-wine.cpp +++ b/library/DFProcess-linux-wine.cpp @@ -23,6 +23,7 @@ distribution. */ #include "Internal.h" #include "LinuxProcess.h" +#include "ProcessFactory.h" #include "dfhack/VersionInfo.h" #include "dfhack/DFError.h" #include @@ -30,6 +31,31 @@ distribution. #include using namespace DFHack; +namespace { + class WineProcess : public LinuxProcessBase + { + private: + uint32_t STLSTR_buf_off; + uint32_t STLSTR_size_off; + uint32_t STLSTR_cap_off; + public: + WineProcess(uint32_t pid, std::vector & known_versions); + + const std::string readSTLString (uint32_t offset); + size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity); + void writeSTLString(const uint32_t address, const std::string writeString){}; + // get class name of an object with rtti/type info + std::string readClassName(uint32_t vptr); + private: + bool validate(char * exe_file,uint32_t pid, char * memFile, vector & known_versions); + }; +} + +Process* DFHack::createWineProcess(uint32_t pid, vector & known_versions) +{ + return new WineProcess(pid, known_versions); +} + WineProcess::WineProcess(uint32_t pid, vector & known_versions) : LinuxProcessBase(pid) { char dir_name [256]; diff --git a/library/DFProcess-linux.cpp b/library/DFProcess-linux.cpp index 7ce1d2152..44ba5afea 100644 --- a/library/DFProcess-linux.cpp +++ b/library/DFProcess-linux.cpp @@ -23,12 +23,34 @@ distribution. */ #include "Internal.h" #include "LinuxProcess.h" +#include "ProcessFactory.h" #include "dfhack/VersionInfo.h" #include "dfhack/DFError.h" #include #include using namespace DFHack; +namespace { + class NormalProcess : public LinuxProcessBase + { + public: + NormalProcess(uint32_t pid, std::vector & known_versions); + + const std::string readSTLString (uint32_t offset); + size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity); + void writeSTLString(const uint32_t address, const std::string writeString){}; + // get class name of an object with rtti/type info + std::string readClassName(uint32_t vptr); + private: + bool validate(char * exe_file,uint32_t pid, char * memFile, vector & known_versions); + }; +} + +Process* DFHack::createNormalProcess(uint32_t pid, vector & known_versions) +{ + return new NormalProcess(pid, known_versions); +} + NormalProcess::NormalProcess(uint32_t pid, vector & known_versions) : LinuxProcessBase(pid) { char dir_name [256]; diff --git a/library/DFProcess-windows.cpp b/library/DFProcess-windows.cpp index 4c4fae337..5a9967ffb 100644 --- a/library/DFProcess-windows.cpp +++ b/library/DFProcess-windows.cpp @@ -23,11 +23,17 @@ distribution. */ #include "Internal.h" #include "WindowsProcess.h" +#include "ProcessFactory.h" #include "dfhack/VersionInfo.h" #include "dfhack/DFError.h" #include using namespace DFHack; +Process* DFHack::createNormalProcess(uint32_t pid, vector & known_versions) +{ + return new NormalProcess(pid, known_versions); +} + class NormalProcess::Private { public: diff --git a/library/DFProcessEnumerator.cpp b/library/DFProcessEnumerator.cpp index 2dbfd806b..be33d41fd 100644 --- a/library/DFProcessEnumerator.cpp +++ b/library/DFProcessEnumerator.cpp @@ -23,9 +23,7 @@ distribution. */ #include "Internal.h" -#include "SHMProcess.h" -#include "LinuxProcess.h" -#include "WindowsProcess.h" +#include "ProcessFactory.h" #include "dfhack/VersionInfoFactory.h" #include "dfhack/DFProcessEnumerator.h" @@ -122,19 +120,19 @@ Process * BadProcesses::operator[](uint32_t index) Process *ProcessEnumerator::Private::GetProcessObject(ProcessID ID) { - Process *p1 = new SHMProcess(ID.pid,meminfo->versions); + Process *p1 = createSHMProcess(ID.pid,meminfo->versions); if(p1->isIdentified()) return p1; else delete p1; - Process *p2 = new NormalProcess(ID.pid,meminfo->versions); + Process *p2 = createNormalProcess(ID.pid,meminfo->versions); if(p2->isIdentified()) return p2; else delete p2; #ifdef LINUX_BUILD - Process *p3 = new WineProcess(ID.pid,meminfo->versions); + Process *p3 = createWineProcess(ID.pid,meminfo->versions); if(p3->isIdentified()) return p3; else diff --git a/library/private/LinuxProcess.h b/library/private/LinuxProcess.h index a9e3c094e..cc9df1a05 100644 --- a/library/private/LinuxProcess.h +++ b/library/private/LinuxProcess.h @@ -90,39 +90,6 @@ namespace DFHack bool SetAndWait (uint32_t state){return false;}; }; - class DFHACK_EXPORT NormalProcess : public LinuxProcessBase - { - friend class ProcessEnumerator; - public: - NormalProcess(uint32_t pid, std::vector & known_versions); - - const std::string readSTLString (uint32_t offset); - size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity); - void writeSTLString(const uint32_t address, const std::string writeString){}; - // get class name of an object with rtti/type info - std::string readClassName(uint32_t vptr); - private: - bool validate(char * exe_file,uint32_t pid, char * memFile, vector & known_versions); - }; - - class DFHACK_EXPORT WineProcess : public LinuxProcessBase - { - friend class ProcessEnumerator; - private: - uint32_t STLSTR_buf_off; - uint32_t STLSTR_size_off; - uint32_t STLSTR_cap_off; - public: - WineProcess(uint32_t pid, std::vector & known_versions); - - const std::string readSTLString (uint32_t offset); - size_t readSTLString (uint32_t offset, char * buffer, size_t bufcapacity); - void writeSTLString(const uint32_t address, const std::string writeString){}; - // get class name of an object with rtti/type info - std::string readClassName(uint32_t vptr); - private: - bool validate(char * exe_file,uint32_t pid, char * memFile, vector & known_versions); - }; } #endif diff --git a/library/private/ProcessFactory.h b/library/private/ProcessFactory.h new file mode 100644 index 000000000..217dbe839 --- /dev/null +++ b/library/private/ProcessFactory.h @@ -0,0 +1,38 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef PROCESS_FACTORY_H_INCLUDED +#define PROCESS_FACTORY_H_INCLUDED + +#include "dfhack/DFProcess.h" + +namespace DFHack +{ + Process* createNormalProcess(uint32_t pid, std::vector & known_versions); + Process* createSHMProcess(uint32_t pid, std::vector & known_versions); +#ifdef LINUX_BUILD + Process* createWineProcess(uint32_t pid, std::vector & known_versions); +#endif +} +#endif diff --git a/library/private/SHMProcess.h b/library/private/SHMProcess.h index a4fe6e55f..5fb704b07 100644 --- a/library/private/SHMProcess.h +++ b/library/private/SHMProcess.h @@ -31,9 +31,8 @@ namespace DFHack { class DFHACK_EXPORT SHMProcess : public Process { - friend class ProcessEnumerator; - class Private; private: + class Private; Private * const d; public: diff --git a/library/private/WindowsProcess.h b/library/private/WindowsProcess.h index 96ed504a1..97565f47c 100644 --- a/library/private/WindowsProcess.h +++ b/library/private/WindowsProcess.h @@ -30,9 +30,8 @@ distribution. namespace DFHack { - class DFHACK_EXPORT NormalProcess : public Process + class NormalProcess : public Process { - friend class ProcessEnumerator; class Private; private: Private * const d;