-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolException.h
More file actions
39 lines (31 loc) · 1.13 KB
/
Copy pathBoolException.h
File metadata and controls
39 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#include "Logger.h"
#include <stdexcept>
namespace Utility
{
// BoolException.
// Decorate a boolean expression with an exception on mismatch
//
template<bool ONTRUE, typename Exception = std::runtime_error>
class BoolException
{
public:
BoolException(Location const& location, bool condition, char const* ctxt ="Unspecified context")
: condition_(condition)
{
if ( ONTRUE == condition )
{
ErrorLog0 << location << "Throwing exception: " << ctxt;
throw Exception(ctxt);
}
DebugLog0 << location << "Status OK: " << ctxt;
}
operator bool () const { return condition_; }
private:
bool condition_;
};
} // namespace Utility
#define THROW_UNLESS(...) THROW_ON_FALSE(std::runtime_error,__VA_ARGS__)
#define THROW_IF(...) THROW_ON_TRUE(std::runtime_error,__VA_ARGS__)
#define THROW_ON_FALSE(X,...) Utility::BoolException<false, X>(LOCATION(),__VA_ARGS__)
#define THROW_ON_TRUE(X,...) Utility::BoolException<true, X>(LOCATION(),__VA_ARGS__)