diff options
author | Jacques Lucke <jacques@blender.org> | 2020-07-08 23:27:25 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-07-08 23:27:25 +0300 |
commit | 403384998a6bb5f428e15ced5503206b45032b25 (patch) | |
tree | 23e09b183faa196603aafb1572ed0f13b3964333 /source/blender/blenlib/BLI_memory_utils.hh | |
parent | 4b85ed819dc0746f6fc46eab1019aab4c4fcf9c9 (diff) |
BLI: improve constructors and conversions to span
This allows us to avoid many calls to `as_span()` methods. I will
remove those in the next commit. Furthermore, constructors
of Vector and Array can convert from one type to another now.
I tested these changes on Linux with gcc and on Windows.
Diffstat (limited to 'source/blender/blenlib/BLI_memory_utils.hh')
-rw-r--r-- | source/blender/blenlib/BLI_memory_utils.hh | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_memory_utils.hh b/source/blender/blenlib/BLI_memory_utils.hh index 16d6227a551..133615f0f31 100644 --- a/source/blender/blenlib/BLI_memory_utils.hh +++ b/source/blender/blenlib/BLI_memory_utils.hh @@ -26,6 +26,7 @@ #include <memory> #include <new> +#include <type_traits> #include "BLI_utildefines.h" @@ -135,6 +136,32 @@ template<typename T> void uninitialized_copy_n(const T *src, uint n, T *dst) } /** + * Convert n values from type `From` to type `To`. + * + * Exception Safety: Strong. + * + * Before: + * src: initialized + * dst: uninitialized + * After: + * src: initialized + * dst: initialized + */ +template<typename From, typename To> void uninitialized_convert_n(const From *src, uint n, To *dst) +{ + uint current = 0; + try { + for (; current < n; current++) { + new ((void *)(dst + current)) To((To)src[current]); + } + } + catch (...) { + destruct_n(dst, current); + throw; + } +} + +/** * Move n values from src to dst. * * Exception Safety: Basic. @@ -364,6 +391,15 @@ template<typename T, size_t Size = 1> class TypedBuffer { class NoInitialization { }; +/** + * Helper variable that checks if a pointer type can be converted into another pointer type without + * issues. Possible issues are casting away const and casting a pointer to a child class. + * Adding const or casting to a parent class is fine. + */ +template<typename From, typename To> +inline constexpr bool is_convertible_pointer_v = + std::is_convertible_v<From, To> &&std::is_pointer_v<From> &&std::is_pointer_v<To>; + } // namespace blender #endif /* __BLI_MEMORY_UTILS_HH__ */ |