RDKit
Open-source cheminformatics and machine learning.
Invariant.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Greg Landrum, Randal M. Henne and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 
11 #ifndef __RD_INVARIANT_H__
12 #define __RD_INVARIANT_H__
13 
14 #include <assert.h>
15 #include <string>
16 #include <iostream>
17 #include <stdexcept>
18 
19 #include <RDGeneral/RDLog.h>
20 //
21 // What if no invariant method is defined?
22 //
23 #if !defined INVARIANT_EXCEPTION_METHOD && \
24  !defined INVARIANT_ASSERT_METHOD && \
25  !defined INVARIANT_SILENT_METHOD
26 #define INVARIANT_EXCEPTION_METHOD 1
27 #endif
28 
29 //
30 // What if an invariant method is defined, but none are true?
31 //
32 #if !INVARIANT_EXCEPTION_METHOD && \
33  !INVARIANT_ASSERT_METHOD && \
34  !INVARIANT_SILENT_METHOD
35 #undef INVARIANT_EXCEPTION_METHOD
36 #define INVARIANT_EXCEPTION_METHOD 1
37 #endif
38 
39 
40 namespace Invar {
41 
42  class Invariant : public std::runtime_error {
43 
44  public:
45 
46  Invariant( const char * prefix, const char * mess, const char * expr, const char * const file, int line )
47  : std::runtime_error( prefix ),
48  mess_d( mess ),
49  expr_d( expr ),
50  prefix_d( prefix ),
51  file_dp( file ),
52  line_d( line )
53  {
54  }
55  Invariant( const char * prefix, std::string mess, const char * expr, const char * const file, int line )
56  : std::runtime_error( prefix ),
57  mess_d( mess.c_str() ),
58  expr_d( expr ),
59  prefix_d( prefix ),
60  file_dp( file ),
61  line_d( line )
62  {
63  }
64  ~Invariant() throw () {};
65 
66  std::string
67  getMessage() const
68  { return mess_d; }
69 
70  const char *
71  getFile() const
72  { return file_dp; }
73 
74  std::string
75  getExpression() const
76  { return expr_d; }
77 
78  int
79  getLine() const
80  { return line_d; }
81 
82  std::string
83  toString() const;
84 
85 
86  private:
87 
88  std::string
89  mess_d,
90  expr_d,prefix_d;
91 
92 
93 
94  const char
95  * const file_dp;
96 
97  int
98  line_d;
99  };
100  std::ostream& operator<<( std::ostream & s, const Invariant & inv );
101 } // end of namespace Invar
102 
103 
104 
105 #define ASSERT_INVARIANT( expr, mess ) \
106  assert( expr )
107 
108 //
109 // Set desired reporting method
110 //
111 
112 #if INVARIANT_EXCEPTION_METHOD
113 
114 #define CHECK_INVARIANT( expr, mess ) if ( !(expr) ) {\
115  Invar::Invariant inv( "Invariant Violation", mess, \
116  #expr, __FILE__, __LINE__ ); \
117  BOOST_LOG(rdErrorLog) << "\n\n****\n" << inv << "****\n\n"; throw inv; }
118 
119 #define PRECONDITION( expr, mess ) if ( !(expr) ) {\
120  Invar::Invariant inv( "Pre-condition Violation", mess, \
121  #expr, __FILE__, __LINE__ ); \
122  BOOST_LOG(rdErrorLog) << "\n\n****\n" << inv << "****\n\n"; throw inv; }
123 
124 #define POSTCONDITION( expr, mess ) if ( !(expr) ) { \
125  Invar::Invariant inv( "Post-condition Violation", mess, \
126  #expr, __FILE__, __LINE__ );\
127  BOOST_LOG(rdErrorLog) << "\n\n****\n" << inv << "****\n\n"; throw inv; }
128 
129 #define UNDER_CONSTRUCTION( fn ) Invar::Invariant inv( "Incomplete Code", \
130  "This routine is still under development", fn, __FILE__, __LINE__ ); \
131  BOOST_LOG(rdErrorLog) << "\n\n****\n" << inv << "****\n\n"; throw inv;
132 
133 #define RANGE_CHECK( lo, x, hi ) if ( (lo)>(hi) || (x)<(lo) || (x)>(hi) ) {\
134  std::stringstream errstr;\
135  errstr << lo << " <= " << x <<" <= "<<hi;\
136  Invar::Invariant inv( "Range Error", #x, errstr.str().c_str(), __FILE__, __LINE__ );\
137  BOOST_LOG(rdErrorLog) << "\n\n****\n" << inv << "****\n\n"; throw inv; }
138 
139 #define TEST_ASSERT( expr ) if ( !(expr) ) {\
140  Invar::Invariant inv( "Test Assert", "Expression Failed: ", \
141  #expr, __FILE__, __LINE__ ); \
142  BOOST_LOG(rdErrorLog) << "\n\n****\n" << inv << "****\n\n"; throw inv; }
143 
144 
145 #elif INVARIANT_ASSERT_METHOD
146 
147 #define CHECK_INVARIANT( expr, mess ) assert( expr );
148 #define PRECONDITION( expr, mess ) assert( expr );
149 #define POSTCONDITION( expr, mess ) assert( expr );
150 #define UNDER_CONSTRUCTION(fn ) assert(0);
151 #define RANGE_CHECK( lo, x, hi ) assert( (lo)<=(hi) && (x)>=(lo) && (x)<=(hi) );
152 #define TEST_ASSERT( expr ) assert(expr);
153 
154 #elif INVARIANT_SILENT_METHOD
155 
156 #define CHECK_INVARIANT( expr, mess )
157 #define PRECONDITION( expr, mess )
158 #define POSTCONDITION( expr, mess )
159 #define UNDER_CONSTRUCTION( fn )
160 #define RANGE_CHECK( lo, x, hi )
161 #define TEST_ASSERT( expr )
162 
163 #endif
164 
165 #endif
166 
int getLine() const
Definition: Invariant.h:79
STL namespace.
std::string toString() const
Invariant(const char *prefix, const char *mess, const char *expr, const char *const file, int line)
Definition: Invariant.h:46
Invariant(const char *prefix, std::string mess, const char *expr, const char *const file, int line)
Definition: Invariant.h:55
const char * getFile() const
Definition: Invariant.h:71
std::string getMessage() const
Definition: Invariant.h:67
std::ostream & operator<<(std::ostream &s, const Invariant &inv)
std::string getExpression() const
Definition: Invariant.h:75