Merge remote-tracking branch 'origin/feature/emplace_for_optional' into development

This commit is contained in:
John Wellbelove 2019-04-18 19:25:45 +01:00
commit 9c817107ea
5 changed files with 113 additions and 2 deletions

View File

@ -325,6 +325,93 @@ namespace etl
}
}
#if ETL_CPP11_SUPPORTED && !defined(ETL_STLPORT) && !defined(ETL_OPTIONAL_FORCE_CPP03)
//*************************************************************************
/// Emplaces a value.
///\param args The arguments to construct with.
//*************************************************************************
template <typename ... Args>
void emplace(Args && ... args)
{
if (valid)
{
// Destroy the old one.
storage.template get_reference<T>().~T();
}
::new (storage.template get_address<T>()) T(std::forward<Args>(args)...);
valid = true;
}
#else
//*************************************************************************
/// Emplaces a value.
/// 1 parameter.
//*************************************************************************
template <typename T1>
void emplace(const T1& value1)
{
if (valid)
{
// Destroy the old one.
storage.template get_reference<T>().~T();
}
::new (storage.template get_address<T>()) T(value1);
valid = true;
}
//*************************************************************************
/// Emplaces a value.
/// 2 parameters.
//*************************************************************************
template <typename T1, typename T2>
void emplace(const T1& value1, const T2& value2)
{
if (valid)
{
// Destroy the old one.
storage.template get_reference<T>().~T();
}
::new (storage.template get_address<T>()) T(value1, value2);
valid = true;
}
//*************************************************************************
/// Emplaces a value.
/// 3 parameters.
//*************************************************************************
template <typename T1, typename T2, typename T3>
void emplace(const T1& value1, const T2& value2, const T3& value3)
{
if (valid)
{
// Destroy the old one.
storage.template get_reference<T>().~T();
}
::new (storage.template get_address<T>()) T(value1, value2, value3);
valid = true;
}
//*************************************************************************
/// Emplaces a value.
/// 4 parameters.
//*************************************************************************
template <typename T1, typename T2, typename T3, typename T4>
void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
{
if (valid)
{
// Destroy the old one.
storage.template get_reference<T>().~T();
}
::new (storage.template get_address<T>()) T(value1, value2, value3, value4);
valid = true;
}
#endif
private:
typename etl::aligned_storage_as<sizeof(T), T>::type storage;

View File

@ -38,8 +38,8 @@ SOFTWARE.
///\ingroup utilities
#define ETL_VERSION_MAJOR 14
#define ETL_VERSION_MINOR 18
#define ETL_VERSION_PATCH 5
#define ETL_VERSION_MINOR 19
#define ETL_VERSION_PATCH 0
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH))

View File

@ -1,3 +1,8 @@
===============================================================================
14.19.0
Added 'emplace' to etl::optional.
C++03 & C++11 variants supplied depending on compiler support.
===============================================================================
14.18.4
Fixed bug in fractional part for floating point with leading zeros after the decimal point.

View File

@ -85,6 +85,7 @@ SOFTWARE.
//#define ETL_QUEUE_MPMC_MUTEX_FORCE_CPP03
//#define ETL_QUEUE_ISR_FORCE_CPP03
//#define ETL_QUEUE_LOCKED_FORCE_CPP03
//#define ETL_OPTIONAL_FORCE_CPP03
#ifdef _MSC_VER
#include "etl/profiles/msvc_x86.h"

View File

@ -36,6 +36,7 @@ SOFTWARE.
#include "data.h"
typedef TestDataNDC<std::string> Data;
typedef TestDataM<uint32_t> DataM;
std::ostream& operator << (std::ostream& os, const Data& data)
{
@ -85,6 +86,23 @@ namespace
CHECK(!bool(data4));
}
//*************************************************************************
TEST(test_emplace)
{
etl::optional<DataM> data;
data.emplace(1U);
CHECK_EQUAL(1U, data.value().value);
data.emplace(2U);
CHECK_EQUAL(2U, data.value().value);
data.emplace(3U);
CHECK_EQUAL(3U, data.value().value);
CHECK_EQUAL(1, DataM::get_instance_count());
}
//*************************************************************************
TEST(test_nullopt)
{