mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-26 20:38:45 +08:00
Modified logic for enabling the use of initializer lists
This commit is contained in:
parent
2c6ffdc36f
commit
292f5d9176
@ -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.
|
||||
|
||||
@ -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 <stddef.h>
|
||||
|
||||
|
||||
|
||||
#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
|
||||
#endif // ETL_NO_INITIALIZER_LIST
|
||||
|
||||
#endif // ETL_INITIALIZER_LIST_INCLUDED
|
||||
|
||||
@ -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 <new> 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(<new>)
|
||||
#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 <new>
|
||||
#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
|
||||
|
||||
@ -113,15 +113,12 @@ SOFTWARE.
|
||||
#include "profiles/determine_development_os.h"
|
||||
|
||||
//*************************************
|
||||
// Figure out if we can use the standard library <new> 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(<new>)
|
||||
#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
|
||||
|
||||
//*************************************
|
||||
|
||||
31
test/etl_initializer_list/etl_initializer_list.sln
Normal file
31
test/etl_initializer_list/etl_initializer_list.sln
Normal file
@ -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
|
||||
0
test/etl_initializer_list/etl_profile.h
Normal file
0
test/etl_initializer_list/etl_profile.h
Normal file
100
test/etl_initializer_list/test_initializer_list.cpp
Normal file
100
test/etl_initializer_list/test_initializer_list.cpp
Normal file
@ -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 <stdio.h>
|
||||
|
||||
template <typename T>
|
||||
class Container
|
||||
{
|
||||
public:
|
||||
|
||||
constexpr Container(std::initializer_list<T> init)
|
||||
: buffer()
|
||||
, length(init.size())
|
||||
{
|
||||
typename std::initializer_list<T>::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<int> 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<int> 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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user