Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
exit.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file utility/exit.hh
11 /// @brief Program exit functions and macros
12 /// @author David Kim (dekim@u.washington.edu)
13 /// @author Sergey Lyskov (Sergey.Lyskov@jhu.edu)
14 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
15 ///
16 /// @note The point of these is:
17 /// @li Show the file and line number where the exit originates
18 /// @li Optionally show a message about why the exit occurred
19 /// @li Provide a core dump on Linux/UNIX so a post-mortem backtrace
20 /// can be performed
21 /// @li Provide macro functions to add the file and line for you
22 /// @note Break on utility::exit when debugging to allow you to
23 /// get a backtrace from the point of exit
24 
25 
26 #ifndef INCLUDED_utility_exit_hh
27 #define INCLUDED_utility_exit_hh
28 
29 
30 // C++ headers
31 #include <string>
32 
33 
34 /// @brief Macro function wrappers for utility::exit
35 ///
36 /// @note Convenience macros that fills in the file and line
37 /// @note These have to be macros to get the file and line from the point of call
38 /// @note Don't use variadic macros to reduce the "overloads": They aren't standard C++
39 
40 /// @brief Exit with file + line
41 #define utility_exit() utility::exit( __FILE__, __LINE__ )
42 
43 
44 /// @brief Exit with file + line + message
45 ///
46 /// @note The m argument is a message string
47 #define utility_exit_with_message(m) utility::exit( __FILE__, __LINE__, m )
48 
49 
50 /// @brief Exit with file + line + status
51 ///
52 /// @note The s argument is a status value
53 #define utility_exit_with_status(s) utility::exit( __FILE__, __LINE__, s )
54 
55 
56 /// @brief Exit with file + line + message + status
57 ///
58 /// @note The m argument is a message string
59 /// @note The s argument is a status value
60 #define utility_exit_with_message_status(m,s) utility::exit( __FILE__, __LINE__, m, s )
61 
62 /// @brief Assert that the condition holds. Evaluated for both debug and release builds
63 #define runtime_assert(_Expression) if ( !(_Expression) ) utility::exit(__FILE__, __LINE__, #_Expression)
64 
65 /// @brief Assert that the condition holds. Evaluated for both debug and release builds
66 #define runtime_assert_msg(_Expression, msg) \
67  if ( !(_Expression) ) utility::exit(__FILE__, __LINE__, #_Expression " MSG:" msg )
68 
69 /// @brief Assert that the condition holds. Evaluated for both debug and release builds
70 // Does the same thing as runtime_assert_msg but allows C++ strings to be used for the message.
71 #define runtime_assert_string_msg(_Expression, msg) \
72  if ( !(_Expression) ) utility::exit(__FILE__, __LINE__, msg )
73 
74 namespace utility {
75 
76 
77 #ifdef __GNUC__
78 # define NORETURN __attribute__ ((noreturn))
79 #elif __clang__
80 # define NORETURN __attribute__ ((noreturn))
81 #else
82 # define NORETURN
83 #endif
84 
85 /// @brief Exit with file + line + message + optional status
86 void
87 exit(
88  std::string const & file,
89  int const line,
90  std::string const & message,
91  int const status = 1
92 ) NORETURN;
93 
94 /// @brief Conditional Exit with file + line + message + optional status. WIll exit if the condition is not met!
95 int
96 cond_exit(
97  bool condition,
98  std::string const & file,
99  int const line,
100  std::string const & message,
101  int const status = 1
102 );
103 
104 
105 /// @brief Exit with file + line + optional status
106 inline
107 void
108 exit(
109  std::string const & file,
110  int const line,
111  int const status = 1
112 ) NORETURN;
113 
114 
115 /// @brief Exit with file + line + optional status
116 inline
117 void
119  std::string const & file,
120  int const line,
121  int const status
122 )
123 {
124  utility::exit( file, line, std::string(), status );
125 }
126 
127 
128 /// @brief Exit with file + line + status
129 ///
130 /// @note Deprecated: For backwards compatibility with earlier version
131 inline
132 void
134  int const status,
135  std::string const & file,
136  int const line
137 )
138 {
139  utility::exit( file, line, std::string(), status );
140 }
141 
142 typedef void (* UtilityExitCallBack)(void);
143 
144 /// @brief Set call back funtion that will be called on utility::exit.
145 /// Use this function to overload default behavior of sys.exit to more appropriate to your application
146 /// Defaut value for callback function is 0, whicth mean no sys exit is called.
148 
149 /// @brief Add additional callback function that will be called *before* standard exit(…) is executed.
150 /// [Note: do not confuse this function with 'set_main_exit_callback' which is replacing the end behavior of exit(…)]
152 
153 /// @brief Remove additional callback function that was previously added by using add_exit_callback.
155 
156 } // namespace utility
157 
158 
159 #endif // INCLUDED_utility_exit_HH
#define inline
Definition: mt19937.hh:117
int cond_exit(bool condition, std::string const &file, int const line, std::string const &message, int const status)
Conditional Exit with file + line + message + optional status.
Definition: exit.cc:184
void exit(std::string const &file, int const line, std::string const &message, int const status)
Exit with file + line + message + optional status.
Definition: exit.cc:108
def status
static char * line
Definition: Svm.cc:2683
void set_main_exit_callback(UtilityExitCallBack my_callback)
Set call back funtion that will be called on utility::exit. Use this function to overload default beh...
Definition: exit.cc:54
void(* UtilityExitCallBack)(void)
Definition: exit.hh:142
#define NORETURN
Definition: exit.hh:82
void remove_exit_callback(UtilityExitCallBack cb)
Remove additional callback function that was previously added by using add_exit_callback.
Definition: exit.cc:71
void add_exit_callback(UtilityExitCallBack cb)
Add additional callback function that will be called before standard exit(…) is executed. [Note: do not confuse this function with 'set_main_exit_callback' which is replacing the end behavior of exit(…)].
Definition: exit.cc:66