diff options
author | Jacques Lucke <jacques@blender.org> | 2020-08-19 17:44:53 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-08-19 17:44:59 +0300 |
commit | 2aff45146f1464ba8899368ad004522cb6a1a98c (patch) | |
tree | 9841430147c351566bebeb2e0460efb534f2ebe3 /source/blender/blenlib/tests/BLI_exception_safety_test_utils.hh | |
parent | aeaf2b0dd437f1a03ed30142678cbf44d0414ea1 (diff) |
BLI: improve exception safety of Vector, Array and Stack
Using C++ exceptions in Blender is difficult, due to the large
number of C functions in the call stack. However, C++ data
structures in blenlib should at least try to be exception safe,
so that they can be used if someone wants to use exceptions
in some isolated area.
This patch improves the exception safety of the Vector, Array
and Stack data structure. This is mainly achieved by reordering
some lines and doing some explicit exception handling.
I don't expect performance of common operations to be affected
by this change.
The three containers are supposed to provide at least the
basic exception guarantee for most methods (except for e.g.
`*_unchecked` methods). So, resources should not leak when
the contained type throws an exception.
I also added new unit tests that test the exception handling
in various cases.
Diffstat (limited to 'source/blender/blenlib/tests/BLI_exception_safety_test_utils.hh')
-rw-r--r-- | source/blender/blenlib/tests/BLI_exception_safety_test_utils.hh | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/source/blender/blenlib/tests/BLI_exception_safety_test_utils.hh b/source/blender/blenlib/tests/BLI_exception_safety_test_utils.hh new file mode 100644 index 00000000000..1e85dd4bab2 --- /dev/null +++ b/source/blender/blenlib/tests/BLI_exception_safety_test_utils.hh @@ -0,0 +1,60 @@ +#include "BLI_utildefines.h" +#include "testing/testing.h" + +namespace blender::tests { + +struct ExceptionThrower { + static constexpr uint32_t is_alive_state = 0x21254634; + static constexpr uint32_t is_destructed_state = 0xFA4BC327; + uint32_t state; + bool throw_during_copy; + bool throw_during_move; + + ExceptionThrower() : state(is_alive_state), throw_during_copy(false), throw_during_move(false) + { + } + + ExceptionThrower(const ExceptionThrower &other) + : state(is_alive_state), throw_during_copy(false), throw_during_move(false) + { + EXPECT_EQ(other.state, is_alive_state); + if (other.throw_during_copy) { + throw std::runtime_error("throwing during copy, as requested"); + } + } + + ExceptionThrower(ExceptionThrower &&other) + : state(is_alive_state), throw_during_copy(false), throw_during_move(false) + { + EXPECT_EQ(other.state, is_alive_state); + if (other.throw_during_move) { + throw std::runtime_error("throwing during move, as requested"); + } + } + + ExceptionThrower &operator=(const ExceptionThrower &other) + { + EXPECT_EQ(other.state, is_alive_state); + if (throw_during_copy || other.throw_during_copy) { + throw std::runtime_error("throwing during copy, as requested"); + } + return *this; + } + + ExceptionThrower &operator=(ExceptionThrower &&other) + { + EXPECT_EQ(other.state, is_alive_state); + if (throw_during_move || other.throw_during_move) { + throw std::runtime_error("throwing during move, as requested"); + } + return *this; + } + + ~ExceptionThrower() + { + EXPECT_EQ(state, is_alive_state); + state = is_destructed_state; + } +}; + +} // namespace blender::tests |