summaryrefslogtreecommitdiff
path: root/include/mbgl/util
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2018-06-18 16:24:58 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2018-06-25 13:14:52 -0700
commitd176e9bb46c4ab539e6cc5f6c7d17bc68d95e205 (patch)
treed060da460dec129fef73b8ee410d2b9bd6afaa30 /include/mbgl/util
parentf3341dd589c6330c0cfd6e8e381398d08493b48a (diff)
downloadqtlocation-mapboxgl-d176e9bb46c4ab539e6cc5f6c7d17bc68d95e205.tar.gz
[core] Fix issues in unique_any
* Eliminate unnecessary temporary in VTableStack::move, which also fixes calling the destructor on the incorrect instance * Make move consistent: it destructs the src, not the dest, which is always empty * delete doesn't need a null guard * Conversions to void* don't need a cast
Diffstat (limited to 'include/mbgl/util')
-rw-r--r--include/mbgl/util/unique_any.hpp16
1 files changed, 6 insertions, 10 deletions
diff --git a/include/mbgl/util/unique_any.hpp b/include/mbgl/util/unique_any.hpp
index d488930a03..c7dc8b38ea 100644
--- a/include/mbgl/util/unique_any.hpp
+++ b/include/mbgl/util/unique_any.hpp
@@ -135,15 +135,12 @@ private:
template <typename ValueType>
struct VTableHeap : public VTable {
void move(Storage&& src, Storage& dest) override {
- destroy(dest);
dest.dynamic = src.dynamic;
+ src.dynamic = nullptr;
}
void destroy(Storage& s) override {
- if (s.dynamic) {
- delete reinterpret_cast<ValueType*>(s.dynamic);
- }
- s.dynamic = nullptr;
+ delete reinterpret_cast<ValueType*>(s.dynamic);
}
const std::type_info& type() override {
@@ -154,9 +151,8 @@ private:
template <typename ValueType>
struct VTableStack : public VTable {
void move(Storage&& src, Storage& dest) override {
- auto srcValue = reinterpret_cast<ValueType&&>(src.stack);
- new (static_cast<void*>(&dest.stack)) ValueType(std::move(srcValue));
- srcValue.~ValueType();
+ new (&dest.stack) ValueType(std::move(reinterpret_cast<ValueType&>(src.stack)));
+ destroy(src);
}
void destroy(Storage& s) override {
@@ -178,13 +174,13 @@ private:
template <typename ValueType, typename _Vt>
std::enable_if_t<AllocateOnStack<_Vt>::value>
createStorage(ValueType&& value) {
- new (static_cast<void*>(&storage.stack)) _Vt(std::forward<ValueType>(value));
+ new (&storage.stack) _Vt(std::forward<ValueType>(value));
}
template <typename ValueType, typename _Vt>
std::enable_if_t<!AllocateOnStack<_Vt>::value>
createStorage(ValueType&& value) {
- storage.dynamic = static_cast<void*>(new _Vt(std::forward<ValueType>(value)));
+ storage.dynamic = new _Vt(std::forward<ValueType>(value));
}
template <typename ValueType>