ref_counted_unittest.nc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/memory/ref_counted.h"
  5. #include "base/memory/ref_counted_delete_on_sequence.h"
  6. namespace base {
  7. class InitialRefCountIsZero : public base::RefCounted<InitialRefCountIsZero> {
  8. public:
  9. InitialRefCountIsZero() {}
  10. private:
  11. friend class base::RefCounted<InitialRefCountIsZero>;
  12. ~InitialRefCountIsZero() {}
  13. };
  14. #if defined(NCTEST_ADOPT_REF_TO_ZERO_START) // [r"fatal error: static assertion failed due to requirement 'std::is_same<base::subtle::StartRefCountFromOneTag, base::subtle::StartRefCountFromZeroTag>::value': Use AdoptRef only if the reference count starts from one\."]
  15. void WontCompile() {
  16. AdoptRef(new InitialRefCountIsZero());
  17. }
  18. #endif
  19. #if defined(NCTEST_WRONG_REFCOUNT_BASE_CLASS) // [r"fatal error: static assertion failed due to requirement 'std::is_base_of_v<base::Foo, base::Bar>': T implements RefCounted<U>, but U is not a base of T\."]
  20. class Foo : public base::RefCounted<Foo> {
  21. private:
  22. friend class base::RefCounted<Foo>;
  23. ~Foo() {}
  24. };
  25. class Bar : public base::RefCounted<Foo> {
  26. private:
  27. friend class base::RefCounted<Bar>;
  28. ~Bar() {}
  29. };
  30. void WontCompile() {
  31. scoped_refptr<Bar> ptr;
  32. }
  33. #endif
  34. #if defined(NCTEST_WRONG_REFCOUNT_THREADSAFE_BASE_CLASS) // [r"fatal error: static assertion failed due to requirement 'std::is_base_of_v<base::Foo, base::Bar>': T implements RefCountedThreadSafe<U>, but U is not a base of T\."]
  35. class Foo : public base::RefCountedThreadSafe<Foo> {
  36. private:
  37. friend class base::RefCountedThreadSafe<Foo>;
  38. ~Foo() {}
  39. };
  40. class Bar : public base::RefCountedThreadSafe<Foo> {
  41. private:
  42. friend class base::RefCountedThreadSafe<Bar>;
  43. ~Bar() {}
  44. };
  45. void WontCompile() {
  46. scoped_refptr<Bar> ptr;
  47. }
  48. #endif
  49. #if defined(NCTEST_WRONG_REFCOUNT_ON_SEQUENCE_BASE_CLASS) // [r"fatal error: static assertion failed due to requirement 'std::is_base_of_v<base::Foo, base::Bar>': T implements RefCountedDeleteOnSequence<U>, but U is not a base of T\."]
  50. class Foo : public base::RefCountedDeleteOnSequence<Foo> {
  51. private:
  52. friend class base::RefCountedDeleteOnSequence<Foo>;
  53. friend class base::DeleteHelper<Foo>;
  54. ~Foo() {}
  55. };
  56. class Bar : public base::RefCountedDeleteOnSequence<Foo> {
  57. private:
  58. friend class base::RefCountedDeleteOnSequence<Bar>;
  59. friend class base::DeleteHelper<Bar>;
  60. ~Bar() {}
  61. };
  62. void WontCompile() {
  63. scoped_refptr<Bar> ptr;
  64. }
  65. #endif
  66. #if defined(NCTEST_SUBCLASS_OVERRIDES_REFCOUNT_PREFERENCE) // [r"fatal error: static assertion failed due to requirement .*: It's unsafe to override the ref count preference\. Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE from subclasses\."]
  67. class Base : public base::RefCounted<Base> {
  68. protected:
  69. friend class base::RefCounted<Base>;
  70. ~Base() {}
  71. };
  72. class Derived : public Base {
  73. public:
  74. REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  75. };
  76. void WontCompile() {
  77. scoped_refptr<Derived> ptr;
  78. }
  79. #endif
  80. #if defined(NCTEST_SUBCLASS_OVERRIDES_REFCOUNT_PREFERENCE_THREADSAFE) // [r"fatal error: static assertion failed due to requirement .*: It's unsafe to override the ref count preference\. Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE from subclasses\."]
  81. class Base : public base::RefCountedThreadSafe<Base> {
  82. protected:
  83. friend class base::RefCountedThreadSafe<Base>;
  84. ~Base() {}
  85. };
  86. class Derived : public Base {
  87. public:
  88. REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  89. };
  90. void WontCompile() {
  91. scoped_refptr<Derived> ptr;
  92. }
  93. #endif
  94. #if defined(NCTEST_SUBCLASS_OVERRIDES_REFCOUNT_PREFERENCE_SEQUENCE) // [r"fatal error: static assertion failed due to requirement .*: It's unsafe to override the ref count preference\. Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE from subclasses\."]
  95. class Base : public base::RefCountedDeleteOnSequence<Base> {
  96. protected:
  97. friend class base::RefCountedDeleteOnSequence<Base>;
  98. friend class base::DeleteHelper<Base>;
  99. ~Base() {}
  100. };
  101. class Derived : public Base {
  102. public:
  103. REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  104. };
  105. void WontCompile() {
  106. scoped_refptr<Derived> ptr;
  107. }
  108. #endif
  109. } // namespace base