pw_allocator: Fix #if PW_HAVE_FEATURE(__cpp_rtti)
Since __cpp_rtti is a standard feature test macro, it should not use
__has_feature.
Additionally, this CL takes the opportunity to reduce the number of preprocessor directives added by CL 210571.
Bug: 341975367
Change-Id: I77dcec55bde62a66c1328f1852ec9f346ede8ff6
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/210816
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Reviewed-by: Taylor Cramer <cramertj@google.com>
Commit-Queue: Aaron Green <aarongreen@google.com>
diff --git a/pw_allocator/as_pmr_allocator.cc b/pw_allocator/as_pmr_allocator.cc
index b11045d..fa7accb 100644
--- a/pw_allocator/as_pmr_allocator.cc
+++ b/pw_allocator/as_pmr_allocator.cc
@@ -50,11 +50,11 @@
// requires runtime type identification. Without RTTI, two `MemoryResource`s
// with the same allocator will be treated as unequal, and moving objects
// between them may lead to an extra allocation, copy, and deallocation.
-#if PW_HAVE_FEATURE(__cpp_rtti)
+#if defined(__cpp_rtti) && __cpp_rtti
if (typeid(*this) == typeid(other)) {
return allocator_ == static_cast<const MemoryResource&>(other).allocator_;
}
-#endif // PW_HAVE_FEATURE(__cpp_rtti)
+#endif // defined(__cpp_rtti) && __cpp_rtti
return false;
}
diff --git a/pw_allocator/public/pw_allocator/as_pmr_allocator.h b/pw_allocator/public/pw_allocator/as_pmr_allocator.h
index 56c326b..1be7587 100644
--- a/pw_allocator/public/pw_allocator/as_pmr_allocator.h
+++ b/pw_allocator/public/pw_allocator/as_pmr_allocator.h
@@ -15,8 +15,12 @@
#if __has_include(<memory_resource>)
#include <memory_resource>
+#define pmr_namespace ::std::pmr
+
#elif __has_include(<experimental/memory_resource>)
#include <experimental/memory_resource>
+#define pmr_namespace ::std::experimental::pmr
+
#else
#error "<memory_resource> is required to use this header!"
#endif // __has_include(<memory_resource>)
@@ -25,19 +29,12 @@
namespace pw {
+// Forward declaration
class Allocator;
namespace allocator {
-#if __has_include(<memory_resource>)
-using StdMemoryResource = ::std::pmr::memory_resource;
-using StdPolymorphicAllocator = ::std::pmr::polymorphic_allocator<std::byte>;
-#elif __has_include(<experimental/memory_resource>)
-using StdMemoryResource = ::std::experimental::pmr::memory_resource;
-using StdPolymorphicAllocator =
- ::std::experimental::pmr::polymorphic_allocator<std::byte>;
-#endif // __has_include(<memory_resource>)
-
+// Forward declaration
class AsPmrAllocator;
namespace internal {
@@ -48,7 +45,7 @@
/// NOTE! This class aborts if allocation fails.
///
/// See also https://en.cppreference.com/w/cpp/memory/memory_resource.
-class MemoryResource final : public StdMemoryResource {
+class MemoryResource final : public pmr_namespace::memory_resource {
public:
constexpr MemoryResource() = default;
@@ -60,7 +57,8 @@
void* do_allocate(size_t bytes, size_t alignment) override;
void do_deallocate(void* p, size_t bytes, size_t alignment) override;
- bool do_is_equal(const StdMemoryResource& other) const noexcept override;
+ bool do_is_equal(
+ const pmr_namespace::memory_resource& other) const noexcept override;
Allocator* allocator_ = nullptr;
};
@@ -74,9 +72,10 @@
/// can be used in `std::pmr` containers, such as `std::pmr::vector`.
///
/// See also https://en.cppreference.com/w/cpp/memory/polymorphic_allocator.
-class AsPmrAllocator final : public StdPolymorphicAllocator {
+class AsPmrAllocator final
+ : public pmr_namespace::polymorphic_allocator<std::byte> {
public:
- using Base = StdPolymorphicAllocator;
+ using Base = pmr_namespace::polymorphic_allocator<std::byte>;
AsPmrAllocator(Allocator& allocator) : Base(&memory_resource_) {
memory_resource_.set_allocator(allocator);
@@ -90,3 +89,5 @@
} // namespace allocator
} // namespace pw
+
+#undef pmr_namespace