diff --git a/include/etl/optional.h b/include/etl/optional.h index 47f84004..36c03e0f 100644 --- a/include/etl/optional.h +++ b/include/etl/optional.h @@ -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 + void emplace(Args && ... args) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(std::forward(args)...); + valid = true; + } +#else + //************************************************************************* + /// Emplaces a value. + /// 1 parameter. + //************************************************************************* + template + void emplace(const T1& value1) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(value1); + valid = true; + } + + //************************************************************************* + /// Emplaces a value. + /// 2 parameters. + //************************************************************************* + template + void emplace(const T1& value1, const T2& value2) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(value1, value2); + valid = true; + } + + //************************************************************************* + /// Emplaces a value. + /// 3 parameters. + //************************************************************************* + template + void emplace(const T1& value1, const T2& value2, const T3& value3) + { + if (valid) + { + // Destroy the old one. + storage.template get_reference().~T(); + } + + ::new (storage.template get_address()) T(value1, value2, value3); + valid = true; + } + + //************************************************************************* + /// Emplaces a value. + /// 4 parameters. + //************************************************************************* + template + 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(); + } + + ::new (storage.template get_address()) T(value1, value2, value3, value4); + valid = true; + } +#endif + private: typename etl::aligned_storage_as::type storage; diff --git a/include/etl/version.h b/include/etl/version.h index 5eae462c..2ab16a18 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -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)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 0b2373fc..a8823399 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -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. diff --git a/test/etl_profile.h b/test/etl_profile.h index 95880884..ffcbee75 100644 --- a/test/etl_profile.h +++ b/test/etl_profile.h @@ -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" diff --git a/test/test_optional.cpp b/test/test_optional.cpp index 35884641..c48039b7 100644 --- a/test/test_optional.cpp +++ b/test/test_optional.cpp @@ -36,6 +36,7 @@ SOFTWARE. #include "data.h" typedef TestDataNDC Data; +typedef TestDataM DataM; std::ostream& operator << (std::ostream& os, const Data& data) { @@ -85,6 +86,23 @@ namespace CHECK(!bool(data4)); } + //************************************************************************* + TEST(test_emplace) + { + etl::optional 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) {