proxygen
EnableSharedFromThisTest.cpp File Reference

Go to the source code of this file.

Functions

template<typename C >
static void test_enable_shared_from_this (std::shared_ptr< C > sp)
 
 TEST (enable_shared_from_this, compatible_with_std_enable_shared_from_this)
 

Function Documentation

TEST ( enable_shared_from_this  ,
compatible_with_std_enable_shared_from_this   
)

Definition at line 48 of file EnableSharedFromThisTest.cpp.

References folly::pushmi::__adl::noexcept(), and test_enable_shared_from_this().

48  {
49  // Compile-time compatibility.
50  class C_std : public std::enable_shared_from_this<C_std> {};
51  class C_folly : public folly::enable_shared_from_this<C_folly> {};
52  static_assert(
53  noexcept(std::declval<C_std>().shared_from_this()) ==
54  noexcept(std::declval<C_folly>().shared_from_this()),
55  "");
56  static_assert(
57  noexcept(std::declval<C_std const>().shared_from_this()) ==
58  noexcept(std::declval<C_folly const>().shared_from_this()),
59  "");
60  static_assert(noexcept(std::declval<C_folly>().weak_from_this()), "");
61  static_assert(noexcept(std::declval<C_folly const>().weak_from_this()), "");
62 
63  // Runtime compatibility.
64  test_enable_shared_from_this(std::make_shared<C_folly>());
65  test_enable_shared_from_this(std::make_shared<C_folly const>());
66 }
requires E e noexcept(noexcept(s.error(std::move(e))))
static void test_enable_shared_from_this(std::shared_ptr< C > sp)
template<typename C >
static void test_enable_shared_from_this ( std::shared_ptr< C sp)
static

Definition at line 23 of file EnableSharedFromThisTest.cpp.

References ASSERT_EQ, ASSERT_THROW, ASSERT_TRUE, and C.

Referenced by TEST().

23  {
24  ASSERT_EQ(1l, sp.use_count());
25 
26  // Test shared_from_this().
27  std::shared_ptr<C> sp2 = sp->shared_from_this();
28  ASSERT_EQ(sp, sp2);
29 
30  // Test weak_from_this().
31  std::weak_ptr<C> wp = sp->weak_from_this();
32  ASSERT_EQ(sp, wp.lock());
33  sp.reset();
34  sp2.reset();
35  ASSERT_EQ(nullptr, wp.lock());
36 
37  // Test shared_from_this() and weak_from_this() on object not owned by a
38  // shared_ptr. Undefined in C++14 but well-defined in C++17. Also known to
39  // work with libstdc++ >= 20150123. Feel free to add other standard library
40  // versions where the behavior is known.
41 #if __cplusplus >= 201700L || __GLIBCXX__ >= 20150123L
42  C stack_resident;
43  ASSERT_THROW(stack_resident.shared_from_this(), std::bad_weak_ptr);
44  ASSERT_TRUE(stack_resident.weak_from_this().expired());
45 #endif
46 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define C(name, bit)
Definition: CpuId.h:204
#define ASSERT_THROW(statement, expected_exception)
Definition: gtest.h:1849
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865