Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/include/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,47 @@

#include <cassert>
#include <exception>
#include <iostream>
#include <sstream>
#include <stdexcept>

namespace bustub {

#define BUSTUB_ASSERT(expr, message) assert((expr) && (message))

namespace internal {

// An internal stream, used to take C++ stream-style parameters for display, and exit the program with `std::abort`.
class LogFatalStream {
public:
LogFatalStream(const char *file, int line) : file_(file), line_(line) {}

~LogFatalStream() {
std::cerr << file_ << ":" << line_ << ": " << log_stream_.str() << std::endl;
std::abort();
}

template <typename T>
LogFatalStream &operator<<(const T &val) {
log_stream_ << val;
return *this;
}

private:
const char *file_;
int line_;
std::ostringstream log_stream_;
};

} // namespace internal

// A macro which checks `expr` value and performs assert.
// Different from `BUSTUB_ASSERT`, it takes stream-style parameters.
#define BUSTUB_ASSERT_AND_LOG(expr) \
if (bool val = (expr); !val) internal::LogFatalStream { \
__FILE__, __LINE__ \
}

#define UNIMPLEMENTED(message) throw std::logic_error(message)

#define BUSTUB_ENSURE(expr, message) \
Expand Down
Loading