RxDock 0.1.0
A fast, versatile, and open-source program for docking ligands to proteins and nucleic acids
Loading...
Searching...
No Matches
Error.h
1/***********************************************************************
2 * The rDock program was developed from 1998 - 2006 by the software team
3 * at RiboTargets (subsequently Vernalis (R&D) Ltd).
4 * In 2006, the software was licensed to the University of York for
5 * maintenance and distribution.
6 * In 2012, Vernalis and the University of York agreed to release the
7 * program as Open Source software.
8 * This version is licensed under GNU-LGPL version 3.0 with support from
9 * the University of Barcelona.
10 * http://rdock.sourceforge.net/
11 ***********************************************************************/
12
13// rxdock exception base class
14// Errors can be thrown for unspecified errors, or derived classes can be
15// used for particular exception types.
16// Has the following methods:
17//
18// Name - exception name (set by constructor)
19// File - source file where error occurred
20// Line - source line no.
21// Message - description of the error
22// isOK - if true, status is OK, not an error
23// AddMessage- append new message to existing message, e.g. when rethrowing
24// Insertion operator defined for dumping exception details to a stream
25
26#ifndef _RBTERROR_H_
27#define _RBTERROR_H_
28
29#include <exception>
30#include <sstream>
31
32// Useful macro for reporting errors
33//__FILE__ and __LINE__ are standard macros for source code file name
34// and line number respectively
35#define _WHERE_ __FILE__, __LINE__
36
37namespace rxdock {
38
39// General errors
40const std::string IDS_ERROR = "RBT_ERROR";
41const std::string IDS_OK = "RBT_OK";
42const std::string IDS_INVALID_REQUEST = "RBT_INVALID_REQUEST";
43const std::string IDS_BAD_ARGUMENT = "RBT_BAD_ARGUMENT";
44const std::string IDS_ASSERT = "RBT_ASSERT";
45const std::string IDS_BAD_RECEPTOR_FILE = "BAD_RECEPTOR_FILE";
46
47// Template for throwing exceptions from assert failures
48//(The C++ Programming Language, 3rd Ed, p750)
49template <class X, class A> inline void Assert(A assertion) {
50 if (!assertion) {
51 throw X();
52 }
53}
55// BASE ERROR CLASS
56// Note: This is a concrete class so can be instantiated
58
59class Error : public std::exception {
61 // Public methods
62public:
63 // Default constructor
64 // Use to create an "error" with status=OK, no line, no file, no message
65 // All other constructors set the OK flag to false
66 Error()
67 : m_strName(IDS_OK), m_strFile(""), m_strMessage(""), m_nLine(0),
68 m_bOK(true) {}
69
70 // Parameterised constructor
71 // Use to create unspecified rxdock Errors
72 Error(const std::string &strFile, int nLine,
73 const std::string &strMessage = "")
74 : Error(IDS_ERROR, strFile, nLine, strMessage) {}
75
76 // Default destructor
77 virtual ~Error() {}
78
79 virtual const char *what() const noexcept { return m_strWhat.c_str(); }
80
81 // Attribute methods
82 std::string File() const { return m_strFile; } // Get filename
83 int Line() const { return m_nLine; } // Get line number
84 std::string Message() const { return m_strMessage; } // Get message
85 std::string Name() const { return m_strName; } // Get error name
86 bool isOK() const { return m_bOK; } // If true, status is OK (not an error)
87
88 // Append new message to existing message
89 void AddMessage(const std::string &strMessage) {
90 m_strMessage += strMessage;
91 m_strWhat += "\n" + strMessage;
92 }
93
95 // Protected methods
96protected:
97 // Protected constructor to allow derived error classes to set error name
98 Error(const std::string &strName, const std::string &strFile, int nLine,
99 const std::string &strMessage = "")
100 : m_strName(strName), m_strFile(strFile), m_strMessage(strMessage),
101 m_nLine(nLine), m_bOK(false) {
102 std::ostringstream oss;
103 oss << Name();
104 if (!File().empty())
105 oss << " at " << File() << ", line " << Line();
106 if (!Message().empty())
107 oss << std::endl << Message();
108 oss.flush();
109 m_strWhat = oss.str();
110 }
111
113 // Private data
114private:
115 std::string m_strName;
116 std::string m_strFile;
117 std::string m_strMessage;
118 std::string m_strWhat;
119 int m_nLine;
120 bool m_bOK;
121};
122
124// DERIVED ERROR CLASSES - General
126
127// Invalid request - object does not supported the request
128class InvalidRequest : public Error {
129public:
130 InvalidRequest(const std::string &strFile, int nLine,
131 const std::string &strMessage = "")
132 : Error(IDS_INVALID_REQUEST, strFile, nLine, strMessage) {}
133};
134// Bad argument - e.g. empty atom list
135class BadArgument : public Error {
136public:
137 BadArgument(const std::string &strFile, int nLine,
138 const std::string &strMessage = "")
139 : Error(IDS_BAD_ARGUMENT, strFile, nLine, strMessage) {}
140};
141// Assertion failure
142// Assert template doesn't pass params to exception class so can't pass file and
143// line number
144class Assertion : public Error {
145public:
146 Assertion() : Error(IDS_ASSERT, "unspecified file", 0, "Assertion failed") {}
147};
148
149// bad receptor type (ie not PSF/CRD either mol2
150class BadReceptorFile : public Error {
151public:
152 BadReceptorFile(const std::string &strFile, int nLine,
153 const std::string &strMessage = "")
154 : Error(IDS_BAD_RECEPTOR_FILE, strFile, nLine, strMessage) {}
155};
156
157} // namespace rxdock
158
159#endif //_RBTERROR_H_
Definition Error.h:144
Definition Error.h:135
Definition Error.h:150
Definition Error.h:59
Definition Error.h:128