From 292f5d9176a1875f384e28805116494c61e2810e Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 4 Mar 2022 20:54:38 +0000 Subject: [PATCH] Modified logic for enabling the use of initializer lists --- include/etl/array.h | 1 + include/etl/initializer_list.h | 15 ++- include/etl/placement_new.h | 28 +++-- include/etl/platform.h | 15 ++- .../etl_initializer_list.sln | 31 ++++++ test/etl_initializer_list/etl_profile.h | 0 .../test_initializer_list.cpp | 100 ++++++++++++++++++ 7 files changed, 169 insertions(+), 21 deletions(-) create mode 100644 test/etl_initializer_list/etl_initializer_list.sln create mode 100644 test/etl_initializer_list/etl_profile.h create mode 100644 test/etl_initializer_list/test_initializer_list.cpp diff --git a/include/etl/array.h b/include/etl/array.h index 289e1d46..9b6404da 100644 --- a/include/etl/array.h +++ b/include/etl/array.h @@ -44,6 +44,7 @@ SOFTWARE. #include "static_assert.h" #include "error_handler.h" #include "nth_type.h" +#include "initializer_list.h" ///\defgroup array array /// A replacement for std::array if you haven't got C++0x11. diff --git a/include/etl/initializer_list.h b/include/etl/initializer_list.h index 96092ec3..1073917b 100644 --- a/include/etl/initializer_list.h +++ b/include/etl/initializer_list.h @@ -33,12 +33,16 @@ SOFTWARE. #include "platform.h" +#if ETL_NO_INITIALIZER_LIST + + #define ETL_USING_INITIALIZER_LIST 0 + +#else + #if ETL_CPP11_SUPPORTED #include - - #if ((ETL_USING_STL && ETL_NOT_USING_STLPORT) || defined(ETL_IN_UNIT_TEST)) && !defined(ETL_IN_UNIT_TEST_INITIALIZER_LIST) #define ETL_USING_INITIALIZER_LIST 1 @@ -223,11 +227,14 @@ namespace std #endif // Compiler tests } -#endif // ETL_USING_STL + +#endif // ((ETL_USING_STL && ETL_NOT_USING_STLPORT) || defined(ETL_IN_UNIT_TEST)) && !defined(ETL_IN_UNIT_TEST_INITIALIZER_LIST) #else #define ETL_USING_INITIALIZER_LIST 0 #endif // ETL_CPP11_SUPPORTED -#endif // ETL_INITIALIZER_LIST_INCLUDED \ No newline at end of file +#endif // ETL_NO_INITIALIZER_LIST + +#endif // ETL_INITIALIZER_LIST_INCLUDED diff --git a/include/etl/placement_new.h b/include/etl/placement_new.h index 15f5533f..4f448a28 100644 --- a/include/etl/placement_new.h +++ b/include/etl/placement_new.h @@ -28,21 +28,33 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef ETL_NEW_INCLUDED -#define ETL_NEW_INCLUDED -///\ingroup private +#ifndef ETL_PLACEMENT_NEW_INCLUDED +#define ETL_PLACEMENT_NEW_INCLUDED #include "platform.h" +//************************************* +// Figure out if we can use the standard library header, if haven't already done so in etl_profile.h +#if !defined(ETL_USING_STD_NEW) + #if defined(__has_include) + #define ETL_USING_STD_NEW __has_include() + #elif (defined(ARDUINO) && defined(__AVR__)) + #define ETL_USING_STD_NEW 0 + #else + #define ETL_USING_STD_NEW 1 + #endif +#endif + #if ETL_USING_STD_NEW #include #else -// Define placement new if no new header is available -inline void* operator new(size_t, void* p) { return p; } -inline void* operator new[](size_t, void* p) { return p; } + // Define placement new if no new header is available + inline void* operator new(size_t, void* p) { return p; } + inline void* operator new[](size_t, void* p) { return p; } + + inline void operator delete(void*, void*) ETL_NOEXCEPT {} + inline void operator delete[](void*, void*) ETL_NOEXCEPT{} -inline void operator delete(void*, void*) ETL_NOEXCEPT {} -inline void operator delete[](void*, void*) ETL_NOEXCEPT {} #endif #endif diff --git a/include/etl/platform.h b/include/etl/platform.h index fdb4320f..3e14b59c 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -113,15 +113,12 @@ SOFTWARE. #include "profiles/determine_development_os.h" //************************************* -// Figure out if we can use the standard library header, if haven't already done so in etl_profile.h -#if !defined(ETL_USING_STD_NEW) - #if defined(__has_include) - #define ETL_USING_STD_NEW __has_include() - #elif ETL_NOT_USING_STL || (defined(ARDUINO) && defined(__AVR__)) - #define ETL_USING_STD_NEW 0 - #else - #define ETL_USING_STD_NEW 1 - #endif +// Initializer lists are enabled by default. +#if defined(ETL_NO_INITIALIZER_LIST) + #undef ETL_NO_INITIALIZER_LIST + #define ETL_NO_INITIALIZER_LIST 1 +#else + #define ETL_NO_INITIALIZER_LIST 0 #endif //************************************* diff --git a/test/etl_initializer_list/etl_initializer_list.sln b/test/etl_initializer_list/etl_initializer_list.sln new file mode 100644 index 00000000..b0103604 --- /dev/null +++ b/test/etl_initializer_list/etl_initializer_list.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32126.315 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "etl_initializer_list", "etl_initializer_list.vcxproj", "{E22E8381-71AD-4EE0-9238-7277E343FC8B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Debug|x64.ActiveCfg = Debug|x64 + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Debug|x64.Build.0 = Debug|x64 + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Debug|x86.ActiveCfg = Debug|Win32 + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Debug|x86.Build.0 = Debug|Win32 + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Release|x64.ActiveCfg = Release|x64 + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Release|x64.Build.0 = Release|x64 + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Release|x86.ActiveCfg = Release|Win32 + {E22E8381-71AD-4EE0-9238-7277E343FC8B}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F34563DE-EC62-4A92-B873-FBD0AD24C7AD} + EndGlobalSection +EndGlobal diff --git a/test/etl_initializer_list/etl_profile.h b/test/etl_initializer_list/etl_profile.h new file mode 100644 index 00000000..e69de29b diff --git a/test/etl_initializer_list/test_initializer_list.cpp b/test/etl_initializer_list/test_initializer_list.cpp new file mode 100644 index 00000000..71d1d666 --- /dev/null +++ b/test/etl_initializer_list/test_initializer_list.cpp @@ -0,0 +1,100 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/std +https://www.etlcpp.com + +Copyright(c) 2022 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "etl/initializer_list.h" + +#include "etl/string_view.h" // Check for an include that causes a clash with the compilers version. + +#include + +template +class Container +{ +public: + + constexpr Container(std::initializer_list init) + : buffer() + , length(init.size()) + { + typename std::initializer_list::const_iterator itr = std::begin(init); + T* p = buffer; + + while (itr != std::end(init)) + { + *p++ = *itr++; + } + } + + const T& operator [](int i) const + { + return buffer[i]; + } + + const size_t length; + +private: + + T buffer[10]; +}; + +int main() +{ + int result = 0; + + Container c = { 1, 2, 3, 4, 5 }; + + if (c[0] != 1) result = 1; + if (c[1] != 2) result = 2; + if (c[2] != 3) result = 3; + if (c[3] != 4) result = 4; + if (c[4] != 5) result = 5; + + if (c.length != 5) result = 6; + + constexpr Container cc = { 1, 2, 3, 4, 5 }; + + if (cc[0] != 1) result = 7; + if (cc[1] != 2) result = 8; + if (cc[2] != 3) result = 9; + if (cc[3] != 4) result = 10; + if (cc[4] != 5) result = 11; + + if (cc.length != 5) result = 12; + + if (result == 0) + { + printf("**** All tests passed ****\n"); + } + else + { + printf(">>>> Tests failed at #%d <<<<\n", result); + } + + return result; +} +