From 671e5cf56245b1e7295563063f83b4909cad8ccd Mon Sep 17 00:00:00 2001 From: owent Date: Mon, 14 Oct 2024 16:24:00 +0800 Subject: [PATCH] Add nostd::unique_ptr APIs for `CircularBuffer` --- .../opentelemetry/sdk/common/circular_buffer.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/common/circular_buffer.h b/sdk/include/opentelemetry/sdk/common/circular_buffer.h index a35498ecea..9f498fd2a1 100644 --- a/sdk/include/opentelemetry/sdk/common/circular_buffer.h +++ b/sdk/include/opentelemetry/sdk/common/circular_buffer.h @@ -11,6 +11,7 @@ #include #include "opentelemetry/nostd/span.h" +#include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/sdk/common/atomic_unique_ptr.h" #include "opentelemetry/sdk/common/circular_buffer_range.h" #include "opentelemetry/version.h" @@ -83,7 +84,7 @@ class CircularBuffer * @param ptr a pointer to the element to add * @return true if the element was successfully added; false, otherwise. */ - bool Add(std::unique_ptr &ptr) noexcept + bool Add(nostd::unique_ptr &ptr) noexcept { while (true) { @@ -118,7 +119,7 @@ class CircularBuffer } } - bool Add(std::unique_ptr &&ptr) noexcept + bool Add(nostd::unique_ptr &&ptr) noexcept { // rvalue to lvalue reference bool result = Add(std::ref(ptr)); @@ -126,6 +127,19 @@ class CircularBuffer return result; } +#if !defined(OPENTELEMETRY_HAVE_STD_UNIQUE_PTR) + bool Add(std::unique_ptr &ptr) noexcept + { + nostd::unique_ptr convert_ptr{std::move(ptr)}; + + bool result = Add(convert_ptr); + ptr.reset(convert_ptr.release()); + return result; + } + + bool Add(std::unique_ptr &&ptr) noexcept { return Add(nostd::unique_ptr{std::move(ptr)}); } +#endif + /** * Clear the circular buffer. *