From 4f85f37b01c5813e87803d28f2bd0a828f783035 Mon Sep 17 00:00:00 2001 From: mizipzor Date: Sun, 28 Feb 2010 01:13:34 +0100 Subject: [PATCH] added some exceptions to handle errors --- examples/attachtest.cpp | 24 +++++++++++++----- library/CMakeLists.txt | 1 + library/DFError.h | 55 +++++++++++++++++++++++++++++++++++++++++ library/DFHackAPI.cpp | 11 ++++++--- 4 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 library/DFError.h diff --git a/examples/attachtest.cpp b/examples/attachtest.cpp index f02f7b019..018d32ef4 100644 --- a/examples/attachtest.cpp +++ b/examples/attachtest.cpp @@ -9,23 +9,35 @@ using namespace std; #include #include +#include int main (void) { time_t start, end; double time_diff; DFHack::API DF("Memory.xml"); - if(!DF.Attach()) + //if(!DF.Attach()) + //{ + // cerr << "DF not found" << endl; + // return 1; + //} + //if(!DF.Detach()) + //{ + // cerr << "Can't detach from DF" << endl; + // return 1; + //} + + try { - cerr << "DF not found" << endl; - return 1; + DF.Attach(); + DF.Detach(); } - if(!DF.Detach()) + catch (DFHack::Error::NoProcess& e) { - cerr << "Can't detach from DF" << endl; + cerr << e.what() << endl; return 1; } - + // attach/detach test cout << "Testing attach/detach" << endl; time(&start); diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 0f172f0dd..3b76f4247 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -2,6 +2,7 @@ SET(PROJECT_HDRS DFCommonInternal.h +DFError.h DFHackAPI.h DFMemInfo.h DFMemInfoManager.h diff --git a/library/DFError.h b/library/DFError.h new file mode 100644 index 000000000..c6255aa04 --- /dev/null +++ b/library/DFError.h @@ -0,0 +1,55 @@ +/* +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 ERROR_H_INCLUDED +#define ERROR_H_INCLUDED + +#include +#include + +namespace DFHack +{ + namespace Error + { + class NoProcess : public exception + { + public: + virtual const char* what() const throw() + { + return "couldn't find a suitable process"; + } + }; + + class CantAttach : public exception + { + public: + virtual const char* what() const throw() + { + return "couldn't attach to process"; + } + }; + } +} + +#endif // ERROR_H_INCLUDED diff --git a/library/DFHackAPI.cpp b/library/DFHackAPI.cpp index 479243d0f..83c9bd76f 100644 --- a/library/DFHackAPI.cpp +++ b/library/DFHackAPI.cpp @@ -23,6 +23,7 @@ distribution. */ #include "DFCommonInternal.h" +#include "DFError.h" using namespace DFHack; class API::Private @@ -1369,14 +1370,16 @@ bool API::Attach() // find a process (ProcessManager can find multiple when used properly) if (!d->pm->findProcessess()) { - cerr << "couldn't find a suitable process" << endl; - return false; + throw Error::NoProcess(); + //cerr << "couldn't find a suitable process" << endl; + //return false; } d->p = (*d->pm) [0]; if (!d->p->attach()) { - cerr << "couldn't attach to process" << endl; - return false; // couldn't attach to process, no go + throw Error::CantAttach(); + //cerr << "couldn't attach to process" << endl; + //return false; // couldn't attach to process, no go } d->offset_descriptor = d->p->getDescriptor(); // process is attached, everything went just fine... hopefully