From 9cdef8e39943c0a3e645417ecb0c87d22d369675 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 12 Dec 2024 19:42:54 +0000 Subject: [PATCH] Added return reference from stack::emplace --- include/etl/stack.h | 24 ++++++++++++++++++------ test/test_stack.cpp | 20 +++++++++++++++----- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/include/etl/stack.h b/include/etl/stack.h index a292218a3..1c3759060 100644 --- a/include/etl/stack.h +++ b/include/etl/stack.h @@ -283,13 +283,15 @@ namespace etl ///\param value The value to push to the stack. //************************************************************************* template - void emplace(Args && ... args) + reference emplace(Args && ... args) { #if defined(ETL_CHECK_PUSH_POP) ETL_ASSERT(!full(), ETL_ERROR(stack_full)); #endif base_t::add_in(); ::new (&p_buffer[top_index]) T(etl::forward(args)...); + + return p_buffer[top_index]; } #else //************************************************************************* @@ -297,13 +299,15 @@ namespace etl /// If asserts or exceptions are enabled, throws an etl::stack_full if the stack is already full. ///\param value The value to push to the stack. //************************************************************************* - void emplace() + reference emplace() { #if defined(ETL_CHECK_PUSH_POP) ETL_ASSERT(!full(), ETL_ERROR(stack_full)); #endif base_t::add_in(); ::new (&p_buffer[top_index]) T(); + + return p_buffer[top_index]; } //************************************************************************* @@ -312,13 +316,15 @@ namespace etl ///\param value The value to push to the stack. //************************************************************************* template - void emplace(const T1& value1) + reference emplace(const T1& value1) { #if defined(ETL_CHECK_PUSH_POP) ETL_ASSERT(!full(), ETL_ERROR(stack_full)); #endif base_t::add_in(); ::new (&p_buffer[top_index]) T(value1); + + return p_buffer[top_index]; } //************************************************************************* @@ -327,13 +333,15 @@ namespace etl ///\param value The value to push to the stack. //************************************************************************* template - void emplace(const T1& value1, const T2& value2) + reference emplace(const T1& value1, const T2& value2) { #if defined(ETL_CHECK_PUSH_POP) ETL_ASSERT(!full(), ETL_ERROR(stack_full)); #endif base_t::add_in(); ::new (&p_buffer[top_index]) T(value1, value2); + + return p_buffer[top_index]; } //************************************************************************* @@ -342,13 +350,15 @@ namespace etl ///\param value The value to push to the stack. //************************************************************************* template - void emplace(const T1& value1, const T2& value2, const T3& value3) + reference emplace(const T1& value1, const T2& value2, const T3& value3) { #if defined(ETL_CHECK_PUSH_POP) ETL_ASSERT(!full(), ETL_ERROR(stack_full)); #endif base_t::add_in(); ::new (&p_buffer[top_index]) T(value1, value2, value3); + + return p_buffer[top_index]; } //************************************************************************* @@ -357,13 +367,15 @@ namespace etl ///\param value The value to push to the stack. //************************************************************************* template - void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4) + reference emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4) { #if defined(ETL_CHECK_PUSH_POP) ETL_ASSERT(!full(), ETL_ERROR(stack_full)); #endif base_t::add_in(); ::new (&p_buffer[top_index]) T(value1, value2, value3, value4); + + return p_buffer[top_index]; } #endif diff --git a/test/test_stack.cpp b/test/test_stack.cpp index b27f7e115..81d35b467 100644 --- a/test/test_stack.cpp +++ b/test/test_stack.cpp @@ -231,28 +231,38 @@ namespace { etl::stack stack; - stack.emplace(); + Item& item1 = stack.emplace(); CHECK_EQUAL(1U, stack.size()); - stack.emplace('b', 2, 2.3); + Item& item2 = stack.emplace('b', 2, 2.3); CHECK_EQUAL(2U, stack.size()); - stack.emplace('c', 3, 3.4); + Item& item3 = stack.emplace('c', 3, 3.4); CHECK_EQUAL(3U, stack.size()); - stack.emplace('d', 4, 4.5); + Item& item4 = stack.emplace('d', 4, 4.5); CHECK_EQUAL(4U, stack.size()); - stack.emplace('e', 5, 5.6); + Item& item5 = stack.emplace('e', 5, 5.6); CHECK_EQUAL(5U, stack.size()); + CHECK(item1 == Item('a', 1, 1.2)); + CHECK(item2 == Item('b', 2, 2.3)); + CHECK(item3 == Item('c', 3, 3.4)); + CHECK(item4 == Item('d', 4, 4.5)); + CHECK(item5 == Item('e', 5, 5.6)); + CHECK(stack.top() == Item('e', 5, 5.6)); + stack.pop(); CHECK(stack.top() == Item('d', 4, 4.5)); + stack.pop(); CHECK(stack.top() == Item('c', 3, 3.4)); + stack.pop(); CHECK(stack.top() == Item('b', 2, 2.3)); + stack.pop(); CHECK(stack.top() == Item('a', 1, 1.2)); }