Skip to content

Commit

Permalink
Added return reference from stack::emplace
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed Dec 12, 2024
1 parent 2a77222 commit 9cdef8e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
24 changes: 18 additions & 6 deletions include/etl/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,31 @@ namespace etl
///\param value The value to push to the stack.
//*************************************************************************
template <typename ... Args>
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>(args)...);

return p_buffer[top_index];
}
#else
//*************************************************************************
/// Constructs a value in the stack place'.
/// 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];
}

//*************************************************************************
Expand All @@ -312,13 +316,15 @@ namespace etl
///\param value The value to push to the stack.
//*************************************************************************
template <typename T1>
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];
}

//*************************************************************************
Expand All @@ -327,13 +333,15 @@ namespace etl
///\param value The value to push to the stack.
//*************************************************************************
template <typename T1, typename T2>
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];
}

//*************************************************************************
Expand All @@ -342,13 +350,15 @@ namespace etl
///\param value The value to push to the stack.
//*************************************************************************
template <typename T1, typename T2, typename T3>
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];
}

//*************************************************************************
Expand All @@ -357,13 +367,15 @@ namespace etl
///\param value The value to push to the stack.
//*************************************************************************
template <typename T1, typename T2, typename T3, typename T4>
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

Expand Down
20 changes: 15 additions & 5 deletions test/test_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,28 +231,38 @@ namespace
{
etl::stack<Item, 5> 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));
}
Expand Down

0 comments on commit 9cdef8e

Please sign in to comment.