GCC Code Coverage Report


Directory: ./
File: openvdb/openvdb/Exceptions.h
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 17 18 94.4%
Functions: 3 3 100.0%
Branches: 5 10 50.0%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #ifndef OPENVDB_EXCEPTIONS_HAS_BEEN_INCLUDED
5 #define OPENVDB_EXCEPTIONS_HAS_BEEN_INCLUDED
6
7 #include <openvdb/version.h>
8 #include <exception>
9 #include <sstream>
10 #include <string>
11
12
13 namespace openvdb {
14 OPENVDB_USE_VERSION_NAMESPACE
15 namespace OPENVDB_VERSION_NAME {
16
17 class OPENVDB_API Exception: public std::exception
18 {
19 public:
20 Exception(const Exception&) = default;
21 Exception(Exception&&) = default;
22 Exception& operator=(const Exception&) = default;
23 Exception& operator=(Exception&&) = default;
24 598 ~Exception() override = default;
25
26 12 const char* what() const noexcept override
27 {
28 try { return mMessage.c_str(); } catch (...) {}
29 return nullptr;
30 }
31
32 protected:
33 Exception() noexcept {}
34 299 explicit Exception(const char* eType, const std::string* const msg = nullptr) noexcept
35
1/2
✓ Branch 0 taken 299 times.
✗ Branch 1 not taken.
299 {
36 try {
37
2/4
✓ Branch 0 taken 299 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 299 times.
✗ Branch 4 not taken.
299 if (eType) mMessage = eType;
38
2/4
✓ Branch 0 taken 299 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 299 times.
✗ Branch 4 not taken.
598 if (msg) mMessage += ": " + (*msg);
39 } catch (...) {}
40 299 }
41
42 private:
43 std::string mMessage;
44 };
45
46
47 #define OPENVDB_EXCEPTION(_classname) \
48 class OPENVDB_API _classname: public Exception \
49 { \
50 public: \
51 _classname() noexcept: Exception( #_classname ) {} \
52 explicit _classname(const std::string& msg) noexcept: Exception( #_classname , &msg) {} \
53 }
54
55
56 86 OPENVDB_EXCEPTION(ArithmeticError);
57 13 OPENVDB_EXCEPTION(IndexError);
58 32 OPENVDB_EXCEPTION(IoError);
59 21 OPENVDB_EXCEPTION(KeyError);
60 21 OPENVDB_EXCEPTION(LookupError);
61 4 OPENVDB_EXCEPTION(NotImplementedError);
62 2 OPENVDB_EXCEPTION(ReferenceError);
63 31 OPENVDB_EXCEPTION(RuntimeError);
64 22 OPENVDB_EXCEPTION(TypeError);
65 30 OPENVDB_EXCEPTION(ValueError);
66
67 #undef OPENVDB_EXCEPTION
68
69
70 } // namespace OPENVDB_VERSION_NAME
71 } // namespace openvdb
72
73
74 #define OPENVDB_THROW(exception, message) \
75 { \
76 std::string _openvdb_throw_msg; \
77 try { \
78 std::ostringstream _openvdb_throw_os; \
79 _openvdb_throw_os << message; \
80 _openvdb_throw_msg = _openvdb_throw_os.str(); \
81 } catch (...) {} \
82 throw exception(_openvdb_throw_msg); \
83 } // OPENVDB_THROW
84
85 #endif // OPENVDB_EXCEPTIONS_HAS_BEEN_INCLUDED
86