mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Return reference from emplace() in etl::queue (#992)
This commit is contained in:
parent
6b88bef8ab
commit
00b6c9fcb4
@ -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
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user