Skip to content

Commit 9cdef8e

Browse files
committed
Added return reference from stack::emplace
1 parent 2a77222 commit 9cdef8e

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

include/etl/stack.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,27 +283,31 @@ namespace etl
283283
///\param value The value to push to the stack.
284284
//*************************************************************************
285285
template <typename ... Args>
286-
void emplace(Args && ... args)
286+
reference emplace(Args && ... args)
287287
{
288288
#if defined(ETL_CHECK_PUSH_POP)
289289
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
290290
#endif
291291
base_t::add_in();
292292
::new (&p_buffer[top_index]) T(etl::forward<Args>(args)...);
293+
294+
return p_buffer[top_index];
293295
}
294296
#else
295297
//*************************************************************************
296298
/// Constructs a value in the stack place'.
297299
/// If asserts or exceptions are enabled, throws an etl::stack_full if the stack is already full.
298300
///\param value The value to push to the stack.
299301
//*************************************************************************
300-
void emplace()
302+
reference emplace()
301303
{
302304
#if defined(ETL_CHECK_PUSH_POP)
303305
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
304306
#endif
305307
base_t::add_in();
306308
::new (&p_buffer[top_index]) T();
309+
310+
return p_buffer[top_index];
307311
}
308312

309313
//*************************************************************************
@@ -312,13 +316,15 @@ namespace etl
312316
///\param value The value to push to the stack.
313317
//*************************************************************************
314318
template <typename T1>
315-
void emplace(const T1& value1)
319+
reference emplace(const T1& value1)
316320
{
317321
#if defined(ETL_CHECK_PUSH_POP)
318322
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
319323
#endif
320324
base_t::add_in();
321325
::new (&p_buffer[top_index]) T(value1);
326+
327+
return p_buffer[top_index];
322328
}
323329

324330
//*************************************************************************
@@ -327,13 +333,15 @@ namespace etl
327333
///\param value The value to push to the stack.
328334
//*************************************************************************
329335
template <typename T1, typename T2>
330-
void emplace(const T1& value1, const T2& value2)
336+
reference emplace(const T1& value1, const T2& value2)
331337
{
332338
#if defined(ETL_CHECK_PUSH_POP)
333339
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
334340
#endif
335341
base_t::add_in();
336342
::new (&p_buffer[top_index]) T(value1, value2);
343+
344+
return p_buffer[top_index];
337345
}
338346

339347
//*************************************************************************
@@ -342,13 +350,15 @@ namespace etl
342350
///\param value The value to push to the stack.
343351
//*************************************************************************
344352
template <typename T1, typename T2, typename T3>
345-
void emplace(const T1& value1, const T2& value2, const T3& value3)
353+
reference emplace(const T1& value1, const T2& value2, const T3& value3)
346354
{
347355
#if defined(ETL_CHECK_PUSH_POP)
348356
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
349357
#endif
350358
base_t::add_in();
351359
::new (&p_buffer[top_index]) T(value1, value2, value3);
360+
361+
return p_buffer[top_index];
352362
}
353363

354364
//*************************************************************************
@@ -357,13 +367,15 @@ namespace etl
357367
///\param value The value to push to the stack.
358368
//*************************************************************************
359369
template <typename T1, typename T2, typename T3, typename T4>
360-
void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
370+
reference emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
361371
{
362372
#if defined(ETL_CHECK_PUSH_POP)
363373
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
364374
#endif
365375
base_t::add_in();
366376
::new (&p_buffer[top_index]) T(value1, value2, value3, value4);
377+
378+
return p_buffer[top_index];
367379
}
368380
#endif
369381

test/test_stack.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,28 +231,38 @@ namespace
231231
{
232232
etl::stack<Item, 5> stack;
233233

234-
stack.emplace();
234+
Item& item1 = stack.emplace();
235235
CHECK_EQUAL(1U, stack.size());
236236

237-
stack.emplace('b', 2, 2.3);
237+
Item& item2 = stack.emplace('b', 2, 2.3);
238238
CHECK_EQUAL(2U, stack.size());
239239

240-
stack.emplace('c', 3, 3.4);
240+
Item& item3 = stack.emplace('c', 3, 3.4);
241241
CHECK_EQUAL(3U, stack.size());
242242

243-
stack.emplace('d', 4, 4.5);
243+
Item& item4 = stack.emplace('d', 4, 4.5);
244244
CHECK_EQUAL(4U, stack.size());
245245

246-
stack.emplace('e', 5, 5.6);
246+
Item& item5 = stack.emplace('e', 5, 5.6);
247247
CHECK_EQUAL(5U, stack.size());
248248

249+
CHECK(item1 == Item('a', 1, 1.2));
250+
CHECK(item2 == Item('b', 2, 2.3));
251+
CHECK(item3 == Item('c', 3, 3.4));
252+
CHECK(item4 == Item('d', 4, 4.5));
253+
CHECK(item5 == Item('e', 5, 5.6));
254+
249255
CHECK(stack.top() == Item('e', 5, 5.6));
256+
250257
stack.pop();
251258
CHECK(stack.top() == Item('d', 4, 4.5));
259+
252260
stack.pop();
253261
CHECK(stack.top() == Item('c', 3, 3.4));
262+
254263
stack.pop();
255264
CHECK(stack.top() == Item('b', 2, 2.3));
265+
256266
stack.pop();
257267
CHECK(stack.top() == Item('a', 1, 1.2));
258268
}

0 commit comments

Comments
 (0)