Skip to content

Commit

Permalink
Clean up optional throw
Browse files Browse the repository at this point in the history
  • Loading branch information
emilydolson committed Feb 29, 2024
1 parent 1e542a3 commit 2d7b807
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions include/emp/base/_optional_throw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,40 @@
namespace emp {

/// Base case for assert_print...
inline void assert_print(std::stringstream &) { ; }
inline void assert_print_opt(std::stringstream &) { ; }

/// Print out information about the next variable and recurse...
template <typename T, typename... EXTRA>
void assert_print(std::stringstream & ss, std::string name, T && val, EXTRA &&... extra) {
void assert_print_opt(std::stringstream & ss, std::string name, T && val, EXTRA &&... extra) {
if constexpr ( emp::is_streamable<std::stringstream, T>::value ) {
ss << name << ": [" << val << "]" << std::endl;
} else ss << name << ": (non-streamable type)" << std::endl;
assert_print(ss, std::forward<EXTRA>(extra)...);
assert_print_opt(ss, std::forward<EXTRA>(extra)...);
}

template <typename T, typename... EXTRA>
void assert_print_second(std::stringstream & ss, std::string name, T && val, EXTRA &&... extra) {
assert_print(ss, std::forward<EXTRA>(extra)...);
void assert_print_second_opt(std::stringstream & ss, std::string name, T && val, EXTRA &&... extra) {
assert_print_opt(ss, std::forward<EXTRA>(extra)...);
}

template <typename T>
void assert_print_second(std::stringstream & ss, std::string name, T && val) {;}
void assert_print_second_opt(std::stringstream & ss, std::string name, T && val) {;}

template <typename T, typename... EXTRA>
void assert_print_first(std::stringstream & ss, std::string name, T && val, EXTRA &&... extra) {
void assert_print_first_opt(std::stringstream & ss, std::string name, T && val, EXTRA &&... extra) {
if constexpr ( emp::is_streamable<std::stringstream, T>::value ) {
ss << name << ": [" << val << "]" << std::endl;
} else ss << name << ": (non-streamable type)" << std::endl;
assert_print_second(ss, std::forward<EXTRA>(extra)...);
assert_print_second_opt(ss, std::forward<EXTRA>(extra)...);
}

void assert_print_first(std::stringstream & ss, int placeholder) {;}
void assert_print_first_opt(std::stringstream & ss, int placeholder) {;}

template <typename... EXTRA>
void assert_throw(std::string filename, size_t line, std::string expr, std::string message, EXTRA &&... extra) {
void assert_throw_opt(std::string filename, size_t line, std::string expr, std::string message, EXTRA &&... extra) {
std::stringstream ss;
ss << "Internal Error (in " << filename << " line " << line << "): " << expr << ".\n\n Message: " << message << "\n\n";
assert_print_first(ss, std::forward<EXTRA>(extra)...);
assert_print_first_opt(ss, std::forward<EXTRA>(extra)...);
throw(std::runtime_error(ss.str()));
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/emp/base/always_assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

#define emp_always_assert_impl(TEST) emp_always_assert_msvc_impl(TEST)

#elif defined(IN_PYTHON)
#elif defined(EMP_OPTIONAL_THROW_ON)

#define emp_always_assert_impl(...) \
do { \
Expand Down
4 changes: 2 additions & 2 deletions include/emp/base/optional_throw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#endif


#if defined( IN_PYTHON )
#if defined( EMP_OPTIONAL_THROW_ON )

// #if defined (_MSC_VER )

#define emp_optional_throw(TEST, MESSAGE) \
do { \
if (!(TEST)) { \
emp::assert_throw(__FILE__, __LINE__, #TEST, MESSAGE, 0); \
emp::assert_throw_opt(__FILE__, __LINE__, #TEST, MESSAGE, 0); \
} \
} while(0)

Expand Down
2 changes: 1 addition & 1 deletion tests/base/optional_throw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST_CASE("Optional throw" "[asserts]") {
emp_optional_throw(false);
REQUIRE(emp::assert_last_fail);

#define IN_PYTHON 1
#define EMP_OPTIONAL_THROW_ON 1
try {
emp_optional_throw(false);
}
Expand Down

0 comments on commit 2d7b807

Please sign in to comment.