diff options
author | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2018-06-25 15:06:41 -0700 |
---|---|---|
committer | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2018-06-25 16:00:04 -0700 |
commit | b0303c7493e6a7255a38d1786d8f790ddf4b6ec2 (patch) | |
tree | dd1e92cc9a7f4c19d5811b2337ec6fb2ede5b1b6 | |
parent | 279dd93f7cbd57adce60a97bdb760905e30f4580 (diff) | |
download | qtlocation-mapboxgl-upstream/unique_any_no_copy.tar.gz |
Remove any_cast<>(unique_any) overloads that facilitate copying. This contradicts the unique_any contractupstream/unique_any_no_copy
-rw-r--r-- | include/mbgl/util/unique_any.hpp | 26 | ||||
-rw-r--r-- | platform/darwin/src/MGLStyle.mm | 3 | ||||
-rw-r--r-- | test/util/unique_any.test.cpp | 11 |
3 files changed, 8 insertions, 32 deletions
diff --git a/include/mbgl/util/unique_any.hpp b/include/mbgl/util/unique_any.hpp index d488930a03..b84069c2e4 100644 --- a/include/mbgl/util/unique_any.hpp +++ b/include/mbgl/util/unique_any.hpp @@ -222,35 +222,11 @@ template<typename ValueType> inline ValueType* any_cast(unique_any* any) { if(any == nullptr || any->type() != typeid(ValueType)) - return nullptr; + throw bad_any_cast(); else return any->cast<ValueType>(); } -template<typename ValueType, typename _Vt = std::decay_t<ValueType> > -inline ValueType any_cast(const unique_any& any) -{ - static_assert(std::is_constructible<ValueType, const _Vt&>::value, - "any_cast type can't construct copy of contained object"); - auto temp = any_cast<_Vt>(&any); - if (temp == nullptr) { - throw bad_any_cast(); - } - return static_cast<ValueType>(*temp); -} - -template<typename ValueType, typename _Vt = std::decay_t<ValueType> > -inline ValueType any_cast(unique_any& any) -{ - static_assert(std::is_constructible<ValueType, const _Vt&>::value, - "any_cast type can't construct copy of contained object"); - auto temp = any_cast<_Vt>(&any); - if (temp == nullptr) { - throw bad_any_cast(); - } - return static_cast<ValueType>(*temp); -} - template<typename ValueType, typename _Vt = std::remove_cv_t<ValueType> > inline ValueType any_cast(unique_any&& any) { diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 3f9bfbf8ca..ad47cb0155 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -177,7 +177,8 @@ static_assert(6 == mbgl::util::default_styles::numOrderedStyles, } - (MGLSource *)sourceFromMBGLSource:(mbgl::style::Source *)rawSource { - if (MGLSource *source = rawSource->peer.has_value() ? mbgl::util::any_cast<SourceWrapper>(rawSource->peer).source : nil) { + if (rawSource->peer.has_value()) { + MGLSource* source = mbgl::util::any_cast<SourceWrapper>(&(rawSource->peer))->source; return source; } diff --git a/test/util/unique_any.test.cpp b/test/util/unique_any.test.cpp index 9357b9c0ec..19763eb90d 100644 --- a/test/util/unique_any.test.cpp +++ b/test/util/unique_any.test.cpp @@ -42,7 +42,7 @@ TEST(UniqueAny, BasicTypes) { EXPECT_TRUE(i.type() == typeid(int)); EXPECT_TRUE(IsStackAllocated(i, any_cast<int>(&i))); - auto iValue = any_cast<int>(i); + auto iValue = *any_cast<int>(&i); EXPECT_TRUE(iValue == 3); EXPECT_TRUE(unique_any(4).has_value()); @@ -53,7 +53,7 @@ TEST(UniqueAny, BasicTypes) { EXPECT_TRUE(f.type() == typeid(float)); EXPECT_TRUE(IsStackAllocated(f, any_cast<float>(&f))); - const float fValue = any_cast<const float>(f); + const float fValue = *any_cast<const float>(&f); EXPECT_TRUE(fValue == 6.2f); EXPECT_TRUE(unique_any(1.0f).has_value()); @@ -64,7 +64,7 @@ TEST(UniqueAny, BasicTypes) { EXPECT_TRUE(c.type() == typeid(char)); EXPECT_TRUE(IsStackAllocated(c, any_cast<char>(&c))); - EXPECT_THROW(any_cast<float>(c), bad_any_cast); + EXPECT_THROW(any_cast<float>(&c), bad_any_cast); EXPECT_TRUE(unique_any('4').has_value()); EXPECT_TRUE(unique_any('4').type() == typeid(char)); @@ -166,14 +166,13 @@ TEST(UniqueAny, SharedPtr) { std::weak_ptr<int> weak = shared; unique_any u1 = 0; - EXPECT_THROW(any_cast<float>(u1), bad_any_cast); + EXPECT_THROW(any_cast<float>(&u1), bad_any_cast); EXPECT_EQ(weak.use_count(), 1); unique_any u2 = shared; EXPECT_EQ(weak.use_count(), 2); - EXPECT_EQ(any_cast<std::unique_ptr<int>>(&u1), nullptr); - EXPECT_FALSE(IsStackAllocated(u1, any_cast<std::shared_ptr<TestType>>(&u1))); + EXPECT_THROW(any_cast<std::unique_ptr<int>>(&u1), bad_any_cast); u1 = std::move(u2); EXPECT_EQ(weak.use_count(), 2); |