Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return reference from emplace() in etl::queue #992

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions include/etl/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,90 +337,107 @@ namespace etl
//*************************************************************************
/// Constructs a value in the queue 'in place'.
/// If asserts or exceptions are enabled, throws an etl::queue_full if the queue if already full.
///\param value The value to use to construct the item to push to the queue.
///\param args The arguments to the constructor for the new item to push to the queue.
//*************************************************************************
template <typename ... Args>
void emplace(Args && ... args)
reference emplace(Args && ... args)
{
#if defined(ETL_CHECK_PUSH_POP)
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
#endif
::new (&p_buffer[in]) T(etl::forward<Args>(args)...);
reference value = p_buffer[in];
::new (&value) T(etl::forward<Args>(args)...);
add_in();
return value;
}
#else
//*************************************************************************
/// Constructs a value in the queue 'in place'.
/// Constructs a default constructed value in the queue 'in place'.
/// If asserts or exceptions are enabled, throws an etl::queue_full if the queue if already full.
///\param value The value to use to construct the item to push to the queue.
//*************************************************************************
void emplace()
reference emplace()
{
#if defined(ETL_CHECK_PUSH_POP)
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
#endif
::new (&p_buffer[in]) T();
reference value = p_buffer[in];
::new (&value) T();
add_in();
return value;
}

//*************************************************************************
/// Constructs a value in the queue 'in place'.
/// If asserts or exceptions are enabled, throws an etl::queue_full if the queue if already full.
///\param value The value to use to construct the item to push to the queue.
///\param value1 The argument to use to construct the item to push to the queue.
//*************************************************************************
template <typename T1>
void emplace(const T1& value1)
reference emplace(const T1& value1)
{
#if defined(ETL_CHECK_PUSH_POP)
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
#endif
::new (&p_buffer[in]) T(value1);
reference value = p_buffer[in];
::new (&value) T(value1);
add_in();
return value;
}

//*************************************************************************
/// Constructs a value in the queue 'in place'.
/// If asserts or exceptions are enabled, throws an etl::queue_full if the queue if already full.
///\param value The value to use to construct the item to push to the queue.
///\param value1 The first argument to use to construct the item to push to the queue.
///\param value2 The second argument to use to construct the item to push to the queue.
//*************************************************************************
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(queue_full));
#endif
::new (&p_buffer[in]) T(value1, value2);
reference value = p_buffer[in];
::new (&value) T(value1, value2);
add_in();
return value;
}

//*************************************************************************
/// Constructs a value in the queue 'in place'.
/// If asserts or exceptions are enabled, throws an etl::queue_full if the queue if already full.
///\param value The value to use to construct the item to push to the queue.
///\param value1 The first argument to use to construct the item to push to the queue.
///\param value2 The second argument to use to construct the item to push to the queue.
///\param value3 The third argument to use to construct the item to push to the queue.
//*************************************************************************
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(queue_full));
#endif
::new (&p_buffer[in]) T(value1, value2, value3);
reference value = p_buffer[in];
::new (&value) T(value1, value2, value3);
add_in();
return value;
}

//*************************************************************************
/// Constructs a value in the queue 'in place'.
/// If asserts or exceptions are enabled, throws an etl::queue_full if the queue if already full.
///\param value The value to use to construct the item to push to the queue.
///\param value1 The first argument to use to construct the item to push to the queue.
///\param value2 The second argument to use to construct the item to push to the queue.
///\param value3 The third argument to use to construct the item to push to the queue.
///\param value4 The fourth argument to use to construct the item to push to the queue.
//*************************************************************************
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(queue_full));
#endif
::new (&p_buffer[in]) T(value1, value2, value3, value4);
reference value = p_buffer[in];
::new (&value) T(value1, value2, value3, value4);
add_in();
return value;
}
#endif

Expand Down
16 changes: 11 additions & 5 deletions test/test_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,17 @@ namespace
{
etl::queue<Item, 5> queue;

queue.emplace();
queue.emplace('b', 2, 2.3);
queue.emplace('c', 3, 3.4);
queue.emplace('d', 4, 4.5);
queue.emplace('e', 5, 5.6);
Item& item_a = queue.emplace();
Item& item_b = queue.emplace('b', 2, 2.3);
Item& item_c = queue.emplace('c', 3, 3.4);
Item& item_d = queue.emplace('d', 4, 4.5);
Item& item_e = queue.emplace('e', 5, 5.6);

CHECK(item_a == Item('a', 1, 1.2));
CHECK(item_b == Item('b', 2, 2.3));
CHECK(item_c == Item('c', 3, 3.4));
CHECK(item_d == Item('d', 4, 4.5));
CHECK(item_e == Item('e', 5, 5.6));

CHECK(queue.front() == Item('a', 1, 1.2));
queue.pop();
Expand Down
Loading