From f1777dce37cc2945e0697ae3ec3413ecd37b5d27 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 16 May 2019 23:23:13 +0100 Subject: [PATCH 1/5] Added variadic 'is_one_of' --- include/etl/type_traits.h | 42 ++++++++++++++++++++++++----- include/etl/type_traits_generator.h | 23 ++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index ee01e614..43be133e 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -314,6 +314,13 @@ namespace etl template struct is_trivially_copy_assignable : etl::is_pod {}; #endif +#if ETL_CPP11_SUPPORTED + /// is_rvalue_reference + ///\ingroup type_traits + template struct is_rvalue_reference : etl::false_type {}; + template struct is_rvalue_reference : etl::true_type {}; +#endif + /// conditional ///\ingroup type_traits template struct conditional { typedef T type; }; @@ -335,7 +342,7 @@ namespace etl struct conditional_integral_constant { ETL_STATIC_ASSERT(etl::is_integral::value, "Not an integral type"); - static const T value = FALSE_VALUE; + static const T value = FALSE_VALUE; }; /// make_signed @@ -480,19 +487,40 @@ namespace etl ///\ingroup type_traits template <> struct alignment_of : integral_constant {}; +#if ETL_CPP11_SUPPORTED + + //***************************************************************************" + /// Template to determine if a type is one of a specified list. + ///\ingroup types + //***************************************************************************" + template + struct is_one_of + { + static const bool value = etl::is_same::value || + etl::is_one_of::value; + }; + + template + struct is_one_of + { + static const bool value = etl::is_same::value; + }; + +#else + //*************************************************************************** /// Template to determine if a type is one of a specified list. ///\ingroup types //*************************************************************************** template struct is_one_of { - static const bool value = + static const bool value = etl::is_same::value || etl::is_same::value || etl::is_same::value || @@ -512,6 +540,8 @@ namespace etl etl::is_same::value; }; +#endif + //*************************************************************************** // A set of templates to allow related types to be derived. //*************************************************************************** diff --git a/include/etl/type_traits_generator.h b/include/etl/type_traits_generator.h index 83b982bd..5c237cac 100644 --- a/include/etl/type_traits_generator.h +++ b/include/etl/type_traits_generator.h @@ -499,6 +499,27 @@ namespace etl ///\ingroup type_traits template <> struct alignment_of : integral_constant {}; +#if ETL_CPP11_SUPPORTED + + //***************************************************************************" + /// Template to determine if a type is one of a specified list. + ///\ingroup types + //***************************************************************************" + template + struct is_one_of + { + static const bool value = etl::is_same::value || + etl::is_one_of::value; + }; + + template + struct is_one_of + { + static const bool value = etl::is_same::value; + }; + +#else + /*[[[cog import cog cog.outl("//***************************************************************************") @@ -524,6 +545,8 @@ namespace etl ]]]*/ /*[[[end]]]*/ +#endif + //*************************************************************************** // A set of templates to allow related types to be derived. //*************************************************************************** From 8ab44900e41a9142b3c67788e1134834454909a1 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 16 May 2019 23:40:43 +0100 Subject: [PATCH 2/5] Merge remote-tracking branch 'origin/development' into feature/C++11_variadic_templates --- .vs/.gitignore | 1 + .vs/ProjectSettings.json | 3 + .vs/VSWorkspaceState.json | 10 + CMakeSettings.json | 17 + include/etl/algorithm.h | 138 ++++++- include/etl/basic_format_spec.h | 263 +++++++++++++ include/etl/basic_string.h | 73 +++- include/etl/cstring.h | 3 + include/etl/cyclic_value.h | 8 - include/etl/format_spec.h | 45 +++ include/etl/memory.h | 117 +++++- include/etl/multi_array.h | 69 ++++ include/etl/negative.h | 60 +++ include/etl/optional.h | 87 +++++ include/etl/private/to_string_helper.h | 499 +++++++++++++++++++++++++ include/etl/profiles/etl_profile.h | 26 +- include/etl/to_string.h | 67 ++++ include/etl/to_u16string.h | 65 ++++ include/etl/to_u32string.h | 65 ++++ include/etl/to_wstring.h | 65 ++++ include/etl/type_traits.h | 15 + include/etl/type_traits_generator.h | 15 + include/etl/u16format_spec.h | 45 +++ include/etl/u16string.h | 3 + include/etl/u32format_spec.h | 45 +++ include/etl/u32string.h | 3 + include/etl/version.h | 4 +- include/etl/wformat_spec.h | 45 +++ include/etl/wstring.h | 3 + library.json | 27 ++ support/Release notes.txt | 63 ++++ test/codeblocks/ETL.cbp | 16 + test/etl_profile.h | 1 + test/test_algorithm.cpp | 36 ++ test/test_cyclic_value.cpp | 56 +-- test/test_memory.cpp | 146 ++++++-- test/test_multi_array.cpp | 495 ++++++++++++++++++++++++ test/test_optional.cpp | 18 + test/test_string_char.cpp | 99 +++++ test/test_string_u16.cpp | 85 +++++ test/test_string_u32.cpp | 85 +++++ test/test_string_wchar_t.cpp | 85 +++++ test/test_to_string.cpp | 399 ++++++++++++++++++++ test/test_to_u16string.cpp | 331 ++++++++++++++++ test/test_to_u32string.cpp | 334 +++++++++++++++++ test/test_to_wstring.cpp | 397 ++++++++++++++++++++ test/test_type_traits.cpp | 24 ++ test/vs2017/etl.vcxproj | 17 + test/vs2017/etl.vcxproj.filters | 51 +++ 49 files changed, 4526 insertions(+), 98 deletions(-) create mode 100644 .vs/.gitignore create mode 100644 .vs/ProjectSettings.json create mode 100644 .vs/VSWorkspaceState.json create mode 100644 CMakeSettings.json create mode 100644 include/etl/basic_format_spec.h create mode 100644 include/etl/format_spec.h create mode 100644 include/etl/multi_array.h create mode 100644 include/etl/negative.h create mode 100644 include/etl/private/to_string_helper.h create mode 100644 include/etl/to_string.h create mode 100644 include/etl/to_u16string.h create mode 100644 include/etl/to_u32string.h create mode 100644 include/etl/to_wstring.h create mode 100644 include/etl/u16format_spec.h create mode 100644 include/etl/u32format_spec.h create mode 100644 include/etl/wformat_spec.h create mode 100644 library.json create mode 100644 test/test_multi_array.cpp create mode 100644 test/test_to_string.cpp create mode 100644 test/test_to_u16string.cpp create mode 100644 test/test_to_u32string.cpp create mode 100644 test/test_to_wstring.cpp diff --git a/.vs/.gitignore b/.vs/.gitignore new file mode 100644 index 00000000..35226880 --- /dev/null +++ b/.vs/.gitignore @@ -0,0 +1 @@ +/slnx.sqlite diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 00000000..38c5f003 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "x86-Debug" +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 00000000..9e5a6885 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,10 @@ +{ + "ExpandedNodes": [ + "", + "\\include", + "\\include\\etl", + "\\test" + ], + "SelectedNode": "\\test\\test_to_string.cpp", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/CMakeSettings.json b/CMakeSettings.json new file mode 100644 index 00000000..9d0eaab8 --- /dev/null +++ b/CMakeSettings.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "x86-Debug", + "generator": "Ninja", + "configurationType": "Debug", + "inheritEnvironments": [ + "msvc_x86" + ], + "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", + "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "-v", + "ctestCommandArgs": "" + } + ] +} \ No newline at end of file diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 65a23687..626ca838 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -1131,21 +1131,21 @@ namespace etl void sort(TIterator first, TIterator last, TCompare compare) { typedef typename std::iterator_traits::difference_type difference_t; - + difference_t n = std::distance(first, last); - for (difference_t i = n / 2; i > 0; i /= 2) + for (difference_t i = n / 2; i > 0; i /= 2) { - for (difference_t j = i; j < n; ++j) + for (difference_t j = i; j < n; ++j) { for (difference_t k = j - i; k >= 0; k -= i) { - TIterator itr1 = first; + TIterator itr1 = first; TIterator itr2 = first; - + std::advance(itr1, k); std::advance(itr2, k + i); - + if (compare(*itr2, *itr1)) { std::iter_swap(itr1, itr2); @@ -1164,6 +1164,132 @@ namespace etl { etl::sort(first, last, std::less::value_type>()); } + +#if ETL_CPP11_SUPPORTED + //*************************************************************************** + /// Returns the maximum value. + //*************************************************************************** + template + constexpr const T& multimax(const T& a, const T& b) + { + return a < b ? b : a; + } + + template + constexpr const T& multimax(const T& t, const Tx&... tx) + { + return multimax(t, multimax(tx...)); + } + + //*************************************************************************** + /// Returns the maximum value. + /// User supplied compare function. + //*************************************************************************** + template + constexpr const T& multimax_compare(TCompare compare, const T& a, const T& b) + { + return compare(a, b) ? b : a; + } + + template + constexpr const T& multimax_compare(TCompare compare, const T& t, const Tx&... tx) + { + return multimax_compare(compare, t, multimax_compare(compare, tx...)); + } + + //*************************************************************************** + /// Returns the maximum value. + //*************************************************************************** + template + constexpr const T& multimin(const T& a, const T& b) + { + return a < b ? a : b; + } + + template + constexpr const T& multimin(const T& t, const Tx&... tx) + { + return multimin(t, multimin(tx...)); + } + + //*************************************************************************** + /// Returns the minimum value. + /// User supplied compare function. + //*************************************************************************** + template + constexpr const T& multimin_compare(TCompare compare, const T& a, const T& b) + { + return compare(a, b) ? a : b; + } + + template + constexpr const T& multimin_compare(TCompare compare, const T& t, const Tx&... tx) + { + return multimin_compare(compare, t, multimin_compare(compare, tx...)); + } + + //*************************************************************************** + /// Returns the iterator to the maximum value. + //*************************************************************************** + template + constexpr const TIterator& multimax_iter(const TIterator& a, const TIterator& b) + { + return *a < *b ? b : a; + } + + template + constexpr const TIterator& multimax_iter(const TIterator& t, const TIteratorx&... tx) + { + return multimax_iter(t, multimax_iter(tx...)); + } + + //*************************************************************************** + /// Returns the iterator to the maximum value. + /// User supplied compare function. + //*************************************************************************** + template + constexpr const TIterator& multimax_iter_compare(TCompare compare, const TIterator& a, const TIterator& b) + { + return compare(*a, *b) ? b : a; + } + + template + constexpr const TIterator& multimax_iter_compare(TCompare compare, const TIterator& t, const TIteratorx&... tx) + { + return multimax_iter_compare(compare, t, multimax_iter_compare(compare, tx...)); + } + + //*************************************************************************** + /// Returns the iterator to the minimum value. + //*************************************************************************** + template + constexpr const TIterator& multimin_iter(const TIterator& a, const TIterator& b) + { + return *a < *b ? a : b; + } + + template + constexpr const TIterator& multimin_iter(const TIterator& t, const Tx&... tx) + { + return multimin_iter(t, multimin_iter(tx...)); + } + + //*************************************************************************** + /// Returns the iterator to the minimum value. + /// User supplied compare function. + //*************************************************************************** + template + constexpr const TIterator& multimin_iter_compare(TCompare compare, const TIterator& a, const TIterator& b) + { + return compare(*a, *b) ? a : b; + } + + template + constexpr const TIterator& multimin_iter_compare(TCompare compare, const TIterator& t, const Tx&... tx) + { + return multimin_iter_compare(compare, t, multimin_iter_compare(compare, tx...)); + } +#endif } #endif diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h new file mode 100644 index 00000000..ca3da502 --- /dev/null +++ b/include/etl/basic_format_spec.h @@ -0,0 +1,263 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_BASIC_FORMAT_SPEC_INCLUDED +#define ETL_BASIC_FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "static_assert.h" + +namespace etl +{ + template + class basic_format_spec + { + public: + + //*************************************************************************** + /// Default constructor. + /// Sets:- + /// Base = 10 + /// Width = 0 + /// Upper case (for hex) = true + /// Left Justified = false + //*************************************************************************** + basic_format_spec() + : base_(10) + , width_(0) + , precision_(0) + , upper_case_(true) + , left_justified_(false) + , boolalpha_(false) + , fill_(typename TString::value_type(' ')) + { + + } + + //*************************************************************************** + /// Sets the base. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& base(const uint32_t b) + { + base_ = static_cast(b); + return *this; + } + + //*************************************************************************** + /// Sets the base to binary. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& binary() + { + base(2); + return *this; + } + + //*************************************************************************** + /// Sets the base to octal. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& octal() + { + base(8); + return *this; + } + + //*************************************************************************** + /// Sets the base to decimal. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& decimal() + { + base(10); + return *this; + } + + //*************************************************************************** + /// Sets the base to hex. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& hex() + { + base(16); + return *this; + } + + //*************************************************************************** + /// Gets the base. + //*************************************************************************** + uint32_t get_base() const + { + return base_; + } + + //*************************************************************************** + /// Sets the width. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& width(const uint32_t w) + { + width_ = static_cast(w); + return *this; + } + + //*************************************************************************** + /// Gets the width. + //*************************************************************************** + uint32_t get_width() const + { + return width_; + } + + //*************************************************************************** + /// Sets the precision. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& precision(const uint32_t w) + { + precision_ = static_cast(w); + return *this; + } + + //*************************************************************************** + /// Gets the precision. + //*************************************************************************** + uint32_t get_precision() const + { + return precision_; + } + + //*************************************************************************** + /// Sets the upper case flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& upper_case(const bool u) + { + upper_case_ = u; + return *this; + } + + //*************************************************************************** + /// Gets the upper case flag. + //*************************************************************************** + bool is_upper_case() const + { + return upper_case_; + } + + //*************************************************************************** + /// Sets the fill character. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& fill(const typename TString::value_type c) + { + fill_ = c; + return *this; + } + + //*************************************************************************** + /// Gets the fill character. + //*************************************************************************** + typename TString::value_type get_fill() const + { + return fill_; + } + + //*************************************************************************** + /// Sets the left justify flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& left() + { + left_justified_ = true; + return *this; + } + + //*************************************************************************** + /// Gets the left justify flag. + //*************************************************************************** + bool is_left() const + { + return left_justified_; + } + + //*************************************************************************** + /// Sets the right justify flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& right() + { + left_justified_ = false; + return *this; + } + + //*************************************************************************** + /// Gets the right justify flag. + //*************************************************************************** + bool is_right() const + { + return !left_justified_; + } + + //*************************************************************************** + /// Sets the bool alpha flag. + /// \return A reference to the basic_format_spec. + //*************************************************************************** + basic_format_spec& boolalpha(bool b) + { + boolalpha_ = b; + return *this; + } + + //*************************************************************************** + /// Gets the boolalpha flag. + //*************************************************************************** + bool is_boolalpha() const + { + return boolalpha_; + } + + private: + + uint_least8_t base_; + uint_least8_t width_; + uint_least8_t precision_; + bool upper_case_; + bool left_justified_; + bool boolalpha_; + typename TString::value_type fill_; + }; +} + +#endif diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index f5c13b5d..10c0afc8 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -50,6 +50,7 @@ SOFTWARE. #include "error_handler.h" #include "integral_limits.h" #include "exception.h" +#include "memory.h" #undef ETL_FILE #define ETL_FILE "27" @@ -229,15 +230,32 @@ namespace etl is_truncated = false; } + //************************************************************************* + /// Sets the 'secure' flag to the requested state. + //************************************************************************* + void set_secure() + { + clear_afer_use = true; + } + + //************************************************************************* + /// Gets the 'secure' state flag. +//************************************************************************* + bool is_secure() const + { + return clear_afer_use; + } + protected: //************************************************************************* /// Constructor. //************************************************************************* string_base(size_t max_size_) - : is_truncated(false), - current_size(0), - CAPACITY(max_size_) + : is_truncated(false) + , clear_afer_use(false) + , current_size(0) + , CAPACITY(max_size_) { } @@ -248,9 +266,10 @@ namespace etl { } - bool is_truncated; ///< Set to true if the operation truncated the string. - size_type current_size; ///< The current number of elements in the string. - const size_type CAPACITY; ///< The maximum number of elements in the string. + bool is_truncated; ///< Set to true if the operation truncated the string. + bool clear_afer_use; ///< Set to true if the string must be cleared after use. + size_type current_size; ///< The current number of elements in the string. + const size_type CAPACITY; ///< The maximum number of elements in the string. }; //*************************************************************************** @@ -263,6 +282,8 @@ namespace etl { public: + typedef ibasic_string interface_type; + typedef T value_type; typedef T& reference; typedef const T& const_reference; @@ -416,6 +437,7 @@ namespace etl current_size = new_size; p_buffer[new_size] = 0; + cleanup(); } //********************************************************************* @@ -529,6 +551,8 @@ namespace etl { is_truncated = true; } + + cleanup(); } //********************************************************************* @@ -1057,6 +1081,7 @@ namespace etl current_size -= n_delete; p_buffer[current_size] = 0; + cleanup(); return first; } @@ -1381,6 +1406,11 @@ namespace etl // Insert the new stuff. insert(position, str, subposition, sublength); + if (str.truncated()) + { + is_truncated = true; + } + return *this; } @@ -1981,7 +2011,8 @@ namespace etl void initialise() { current_size = 0; - p_buffer[0] = 0; + cleanup(); + p_buffer[0] = 0; is_truncated = false; } @@ -2035,9 +2066,25 @@ namespace etl } } - // Disable copy construction. + //************************************************************************* + /// Clear the unused trailing portion of the string. + //************************************************************************* + void cleanup() + { + if (is_secure()) + { + etl::memory_clear_range(&p_buffer[current_size], &p_buffer[CAPACITY]); + } + } + + //************************************************************************* + /// Disable copy construction. + //************************************************************************* ibasic_string(const ibasic_string&); + //************************************************************************* + /// Pointer to the string's buffer. + //************************************************************************* T* p_buffer; //************************************************************************* @@ -2045,15 +2092,17 @@ namespace etl //************************************************************************* #if defined(ETL_POLYMORPHIC_STRINGS) || defined(ETL_POLYMORPHIC_CONTAINERS) public: - virtual ~ibasic_string() - { - } + virtual #else protected: +#endif ~ibasic_string() { + if (is_secure()) + { + initialise(); + } } -#endif }; //*************************************************************************** diff --git a/include/etl/cstring.h b/include/etl/cstring.h index 8fdd1aaa..497aa40f 100644 --- a/include/etl/cstring.h +++ b/include/etl/cstring.h @@ -55,6 +55,9 @@ namespace etl { public: + typedef istring base_type; + typedef istring interface_type; + typedef istring::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/cyclic_value.h b/include/etl/cyclic_value.h index e1fdd12d..3bb3bbe7 100644 --- a/include/etl/cyclic_value.h +++ b/include/etl/cyclic_value.h @@ -117,10 +117,6 @@ namespace etl //************************************************************************* void advance(int n) { - T range = LAST - FIRST + T(1); - - n = n % range; - if (n > 0) { for (int i = 0; i < n; ++i) @@ -394,10 +390,6 @@ namespace etl //************************************************************************* void advance(int n) { - T range = last_value - first_value + T(1); - - n = n % range; - if (n > 0) { for (int i = 0; i < n; ++i) diff --git a/include/etl/format_spec.h b/include/etl/format_spec.h new file mode 100644 index 00000000..27cbcaa0 --- /dev/null +++ b/include/etl/format_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_FORMAT_SPEC_INCLUDED +#define ETL_FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "cstring.h" + +namespace etl +{ + typedef etl::basic_format_spec format_spec; +} + +#endif diff --git a/include/etl/memory.h b/include/etl/memory.h index 35c8cfb9..606e6a7a 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1106,10 +1106,122 @@ namespace etl pointer p; }; + //***************************************************************************** + /// A low level function that clears an object's memory to zero. + ///\param p Pointer to the memory. + ///\param n Size of the memory. + ///\ingroup memory + //***************************************************************************** + inline void memory_clear(volatile char* p, size_t n) + { + while (n--) + { + *p++ = 0; + } + } + + //***************************************************************************** + /// A low level function that clears an object's memory to zero. + ///\tparam T The type. + ///\param object The object to clear. + ///\ingroup memory + //***************************************************************************** + template + void memory_clear(volatile T &object) + { + memory_clear(reinterpret_cast(&object), sizeof(T)); + } + + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param n The number of objects. + ///\ingroup memory + //***************************************************************************** + template + void memory_clear_range(volatile T* begin, size_t n) + { + memory_clear(reinterpret_cast(begin), n * sizeof(T)); + } + + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param end One past the last object in the range. + ///\ingroup memory + //***************************************************************************** + template + void memory_clear_range(volatile T* begin, volatile T* end) + { + const size_t n = static_cast(std::distance(begin, end)); + + memory_clear_range(begin, n); + } + + //***************************************************************************** + /// A low level function that clears an object's memory to zero. + ///\param p Pointer to the memory. + ///\param n Size of the memory. + ///\param value The value to set. + ///\ingroup memory + //***************************************************************************** + inline void memory_set(volatile char* p, size_t n, char value) + { + while (n--) + { + *p++ = value; + } + } + + //***************************************************************************** + /// A low level function that sets an object's memory to a value. + ///\tparam T The type. + ///\param object The object to set. + ///\param value The value to set the object's memory to. + ///\ingroup memory + //***************************************************************************** + template + void memory_set(volatile T &object, const char value) + { + memory_set(reinterpret_cast(&object), sizeof(T), value); + } + + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param n The number of objects. + ///\param value The value to set the object's memory to. + ///\ingroup memory + //***************************************************************************** + template + void memory_set_range(volatile T* begin, size_t n, const char value) + { + memory_set(reinterpret_cast(begin), n * sizeof(T), value); + } + + //***************************************************************************** + /// A low level function that clears a range to zero. + ///\tparam T The type. + ///\param begin The first object in the range. + ///\param end One past the last object in the range. + ///\param value The value to set the object's memory to. + ///\ingroup memory + //***************************************************************************** + template + void memory_set_range(volatile T* begin, volatile T* end, const char value) + { + const size_t n = static_cast(std::distance(begin, end)); + + memory_set_range(begin, n, value); + } + //***************************************************************************** /// Base class for objects that require their memory to be wiped after use. /// Erases the object's memory to zero. - /// Note: This must be the last destructor called for the derived object. + /// Note: This may not work for multiply inherited objects. ///\tparam T The derived type. ///\ingroup memory //***************************************************************************** @@ -1118,8 +1230,7 @@ namespace etl { ~wipe_on_destruct() { - char* pobject = reinterpret_cast(static_cast(this)); - memset(pobject, 0, sizeof(T)); + memory_clear(static_cast(*this)); } }; } diff --git a/include/etl/multi_array.h b/include/etl/multi_array.h new file mode 100644 index 00000000..0c1f9f06 --- /dev/null +++ b/include/etl/multi_array.h @@ -0,0 +1,69 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_MULTI_ARRAY_INCLUDED +#define ETL_MULTI_ARRAY_INCLUDED + +#include "platform.h" +#include "array.h" + +///\defgroup multi_array multi_array +/// A multi dimentional array. +///\ingroup containers + +namespace etl +{ +#if ETL_CPP11_SUPPORTED + + namespace private_multi_array + { + template + struct multi_array_t + { + using type = etl::array::type, D1>; + static constexpr size_t SIZE = D1; + }; + + template + struct multi_array_t + { + using type = etl::array; + static constexpr size_t SIZE = D1; + }; + } + + template + using multi_array = typename private_multi_array::multi_array_t::type; + +#endif +} + +#endif + diff --git a/include/etl/negative.h b/include/etl/negative.h new file mode 100644 index 00000000..7b2a6b33 --- /dev/null +++ b/include/etl/negative.h @@ -0,0 +1,60 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2018 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. +******************************************************************************/ + +#ifndef ETL_NEGATIVE_INCLUDED +#define ETL_NEGATIVE_INCLUDED + +#include "type_traits.h" + +namespace etl +{ + //*************************************************************************** + // For signed types. + //*************************************************************************** + template + typename etl::enable_if::value, bool>::type + is_negative(const T value) + { + return (value < T(0)); + } + + //*************************************************************************** + // For unsigned types. + //*************************************************************************** + template + typename etl::enable_if::value, T>::type + is_negative(const T) + { + return false; + } +} + +#endif + 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/private/to_string_helper.h b/include/etl/private/to_string_helper.h new file mode 100644 index 00000000..0aa8013d --- /dev/null +++ b/include/etl/private/to_string_helper.h @@ -0,0 +1,499 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_TO_STRING_HELPER_INCLUDED +#define ETL_TO_STRING_HELPER_INCLUDED + +///\ingroup private + +#include + +#include "../platform.h" +#include "../absolute.h" +#include "../negative.h" +#include "../basic_format_spec.h" +#include "../type_traits.h" +#include "../container.h" + +#include "../stl/algorithm.h" +#include "../stl/iterator.h" +#include "../stl/limits.h" + +namespace etl +{ + namespace private_to_string + { + //*************************************************************************** + /// Helper function for left/right alignment. + //*************************************************************************** + template + void add_alignment(TIString& str, typename TIString::iterator position, const etl::basic_format_spec& format) + { + uint32_t length = static_cast(std::distance(position, str.end())); + + if (length < format.get_width()) + { + uint32_t fill_length = format.get_width() - length; + + if (format.is_left()) + { + // Insert fill characters on the right. + str.insert(str.end(), fill_length, format.get_fill()); + } + else + { + // Insert fill characters on the left. + str.insert(position, fill_length, format.get_fill()); + } + } + } + + //*************************************************************************** + /// Helper function for booleans. + //*************************************************************************** + template + void add_boolean(const bool value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + typedef typename TIString::value_type type; + typedef typename TIString::iterator iterator; + + static const type t[] = { 't', 'r', 'u', 'e' }; + static const type f[] = { 'f', 'a', 'l', 's', 'e' }; + + if (!append) + { + str.clear(); + } + + iterator start = str.end(); + + if (format.is_boolalpha()) + { + if (value) + { + str.insert(str.end(), etl::begin(t), etl::end(t)); + } + else + { + str.insert(str.end(), etl::begin(f), etl::end(f)); + } + } + else + { + if (value) + { + str.push_back(type('1')); + } + else + { + str.push_back(type('0')); + } + } + + etl::private_to_string::add_alignment(str, start, format); + } + + //*************************************************************************** + /// Helper function for integrals. + //*************************************************************************** + template + void add_integral(T value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + typedef typename TIString::value_type type; + typedef typename TIString::iterator iterator; + + const bool negative = etl::is_negative(value); + + if (!append) + { + str.clear(); + } + + iterator start = str.end(); + + if (value == 0) + { + str.push_back(type('0')); + } + else + { + // Extract the digits, in reverse order. + while (value != 0) + { + T remainder = etl::absolute(value % T(format.get_base())); + str.push_back((remainder > 9) ? (format.is_upper_case() ? type('A' + (remainder - 10)) : type('a' + (remainder - 10))) : type('0' + remainder)); + value = value / T(format.get_base()); + } + + // If number is negative, append '-' + if ((format.get_base() == 10) && negative) + { + str.push_back(type('-')); + } + + // Reverse the string we appended. + std::reverse(start, str.end()); + } + + etl::private_to_string::add_alignment(str, start, format); + } + + //*************************************************************************** + /// Helper function for floating point nan and inf. + //*************************************************************************** + template + void add_nan_inf(const bool not_a_number, + const bool infinity, + TIString& str) + { + typedef typename TIString::value_type type; + + static const type n[] = { 'n', 'a', 'n' }; + static const type i[] = { 'i', 'n', 'f' }; + + if (not_a_number) + { + str.insert(str.end(), etl::begin(n), etl::end(n)); + } + else if (infinity) + { + str.insert(str.end(), etl::begin(i), etl::end(i)); + } + } + + //*************************************************************************** + /// Helper function for floating point integral and fractional. + //*************************************************************************** + template + void add_integral_fractional(const int64_t integral, + const int64_t fractional, + TIString& str, + const etl::basic_format_spec& integral_format, + const etl::basic_format_spec& fractional_format) + { + typedef typename TIString::value_type type; + + etl::private_to_string::add_integral(integral, str, integral_format, true); + + if (fractional_format.get_precision() > 0) + { + str.push_back(type('.')); + etl::private_to_string::add_integral(fractional, str, fractional_format, true); + } + } + + //*************************************************************************** + /// Helper function for floating point. + //*************************************************************************** + template + void add_floating_point(T value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + typedef typename TIString::iterator iterator; + typedef typename TIString::value_type type; + + if (!append) + { + str.clear(); + } + + iterator start = str.end(); + + if (isnan(value) || isinf(value)) + { + etl::private_to_string::add_nan_inf(isnan(value), isinf(value), str); + } + else + { + // Make sure we format the two halves correctly. + uint32_t max_precision = std::numeric_limits::digits10; + + etl::basic_format_spec integral_format = format; + integral_format.decimal().width(0).precision(format.get_precision() > max_precision ? max_precision : format.get_precision()); + + etl::basic_format_spec fractional_format = integral_format; + fractional_format.width(integral_format.get_precision()).fill(type('0')).right(); + + int64_t multiplier = 1; + + for (uint32_t i = 0; i < fractional_format.get_precision(); ++i) + { + multiplier *= 10U; + } + + T f_integral = (value < T(0.0) ? ceil(value) : floor(value)); + int64_t integral = static_cast(f_integral); + int64_t fractional = static_cast(round((value - f_integral) * multiplier)); + + etl::private_to_string::add_integral_fractional(integral, fractional, str, integral_format, fractional_format); + } + + etl::private_to_string::add_alignment(str, start, format); + } + + //*************************************************************************** + /// Helper function for pointers. + //*************************************************************************** + template + void add_pointer(const volatile void* value, + TIString& str, + const etl::basic_format_spec& format, + const bool append) + { + uintptr_t p = reinterpret_cast(value); + + return etl::private_to_string::add_integral(p, str, format, append); + } + + //********************************************************************************************************* + + //*************************************************************************** + /// For booleans. Default format spec. + //***************************************************************************etl::basic_format_spec + template + const TIString& to_string(const bool value, + TIString& str, + const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_boolean(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For booleans. Supplied format spec. + //*************************************************************************** + template + const TIString& to_string(const bool value, + TIString& str, + const etl::basic_format_spec& format, + const bool append = false) + { + etl::private_to_string::add_boolean(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For pointers. Default format spec. + //*************************************************************************** + template + const TIString& to_string(const volatile void* value, + TIString& str, + const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_pointer(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For pointers. Supplied format spec. + //*************************************************************************** + template + const TIString& to_string(const volatile void* value, + TIString& str, + const etl::basic_format_spec& format, + const bool append = false) + { + etl::private_to_string::add_pointer(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value && + !etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(int32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value && + !etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) + { + etl::private_to_string::add_integral(int32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned integrals less then 64 bits. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value && + !etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(uint32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned integrals less than 64 bits. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value && + !etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) + { + etl::private_to_string::add_integral(uint32_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value && + etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(int64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For signed 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_signed::value && + !etl::is_same::value && + etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) + { + etl::private_to_string::add_integral(int64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value && + etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_integral(uint64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For unsigned 64 bit integrals. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value && + etl::is_unsigned::value && + !etl::is_same::value && + etl::is_same::value, const TIString&>::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) + { + etl::private_to_string::add_integral(uint64_t(value), str, format, append); + + return str; + } + + //*************************************************************************** + /// For floating point. Default format spec. + //*************************************************************************** + template + typename etl::enable_if::value, const TIString&>::type + to_string(const T value, TIString& str, const bool append = false) + { + etl::basic_format_spec format; + + etl::private_to_string::add_floating_point(value, str, format, append); + + return str; + } + + //*************************************************************************** + /// For floating point. Supplied format spec. + //*************************************************************************** + template + typename etl::enable_if::value, const TIString&>::type + to_string(const T value, TIString& str, const etl::basic_format_spec& format, const bool append = false) + { + etl::private_to_string::add_floating_point(value, str, format, append); + + return str; + } + } +} + +#endif diff --git a/include/etl/profiles/etl_profile.h b/include/etl/profiles/etl_profile.h index 187bcb5b..9242d71c 100644 --- a/include/etl/profiles/etl_profile.h +++ b/include/etl/profiles/etl_profile.h @@ -29,31 +29,31 @@ SOFTWARE. ******************************************************************************/ #ifndef __ETL_PROFILE_H__ #define __ETL_PROFILE_H__ -#ifdef PROFILE_MSVC +#if defined(PROFILE_MSVC) #include "msvc_x86.h" -#elif PROFILE_GCC_GENERIC +#elif defined(PROFILE_GCC_GENERIC) #include "gcc_generic.h" -#elif PROFILE_GCC_LINUX_X86 +#elif defined(PROFILE_GCC_LINUX_X86) #include "gcc_linux_x86.h" -#elif PROFILE_GCC_WINDOWS_X86 +#elif defined(PROFILE_GCC_WINDOWS_X86) #include "gcc_windows_x86.h" -#elif PROFILE_ARM_V5_GENERIC +#elif defined(PROFILE_ARM_V5_GENERIC) #include "armv5.h" -#elif PROFILE_ARM_V6_GENERIC +#elif defined(PROFILE_ARM_V6_GENERIC) #include "armv6.h" -#elif PROFILE_ARDUINO +#elif defined(PROFILE_ARDUINO) #include "arduino_arm.h" -#elif PROFILE_TICC +#elif defined(PROFILE_TICC) #include "ticc.h" -#elif PROFILE_CPP03_GENERIC +#elif defined(PROFILE_CPP03_GENERIC) #include "cpp03.h" -#elif PROFILE_CPP11_GENERIC +#elif defined(PROFILE_CPP11_GENERIC) #include "cpp11.h" -#elif PROFILE_CPP14_GENERIC +#elif defined(PROFILE_CPP14_GENERIC) #include "cpp14.h" -#elif PROFILE_CPP17_GENERIC +#elif defined(PROFILE_CPP17_GENERIC) #include "cpp17.h" -#elif PROFILE_CUSTOM +#elif defined(PROFILE_CUSTOM) #include "custom_profile.h" #else #error Must provide a profile header file when buiding ETL. See https://www.etlcpp.com/setup.html diff --git a/include/etl/to_string.h b/include/etl/to_string.h new file mode 100644 index 00000000..9bb0e6a1 --- /dev/null +++ b/include/etl/to_string.h @@ -0,0 +1,67 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_TO_STRING_INCLUDED +#define ETL_TO_STRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "cstring.h" +#include "format_spec.h" +#include "private/to_string_helper.h" + +#include + +namespace etl +{ + //*************************************************************************** + /// Default format spec. + //*************************************************************************** + template + const etl::istring& to_string(const T value, etl::string& str, const bool append = false) + { + etl::format_spec format; + + return private_to_string::to_string(value, static_cast(str), format, append); + } + + //*************************************************************************** + /// Supplied format spec.. + //*************************************************************************** + template + const etl::istring& to_string(const T value, etl::string& str, const etl::format_spec& format, const bool append = false) + { + return private_to_string::to_string(value, static_cast(str), format, append); + } +} + +#endif diff --git a/include/etl/to_u16string.h b/include/etl/to_u16string.h new file mode 100644 index 00000000..be9dcbd8 --- /dev/null +++ b/include/etl/to_u16string.h @@ -0,0 +1,65 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_TO_U16STRING_INCLUDED +#define ETL_TO_U16STRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "u16string.h" +#include "u16format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// Default format spec. + //*************************************************************************** + template + const etl::iu16string& to_string(const T value, etl::u16string& str, const bool append = false) + { + etl::u16format_spec format; + + return private_to_string::to_string(value, static_cast(str), format, append); + } + + //*************************************************************************** + /// Supplied format spec. + //*************************************************************************** + template + const etl::iu16string& to_string(const T value, etl::u16string& str, const etl::u16format_spec& format, const bool append = false) + { + return private_to_string::to_string(value, static_cast(str), format, append); + } +} + +#endif diff --git a/include/etl/to_u32string.h b/include/etl/to_u32string.h new file mode 100644 index 00000000..8b95a367 --- /dev/null +++ b/include/etl/to_u32string.h @@ -0,0 +1,65 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_TO_U32STRING_INCLUDED +#define ETL_TO_U32STRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "u32string.h" +#include "u32format_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// Default format spec. + //*************************************************************************** + template + const etl::iu32string& to_string(const T value, etl::u32string& str, const bool append = false) + { + etl::u32format_spec format; + + return private_to_string::to_string(value, static_cast(str), format, append); + } + + //*************************************************************************** + /// Supplied format spec. + //*************************************************************************** + template + const etl::iu32string& to_string(const T value, etl::u32string& str, const etl::u32format_spec& format, const bool append = false) + { + return private_to_string::to_string(value, static_cast(str), format, append); + } +} + +#endif diff --git a/include/etl/to_wstring.h b/include/etl/to_wstring.h new file mode 100644 index 00000000..ad7283f2 --- /dev/null +++ b/include/etl/to_wstring.h @@ -0,0 +1,65 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_TO_WSTRING_INCLUDED +#define ETL_TO_WSTRING_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "type_traits.h" +#include "wstring.h" +#include "wformat_spec.h" +#include "private/to_string_helper.h" + +namespace etl +{ + //*************************************************************************** + /// Default format spec. + //*************************************************************************** + template + const etl::iwstring& to_string(const T value, etl::wstring& str, const bool append = false) + { + etl::wformat_spec format; + + return private_to_string::to_string(value, static_cast(str), format, append); + } + + //*************************************************************************** + /// Supplied format spec. + //*************************************************************************** + template + const etl::iwstring& to_string(const T value, etl::wstring& str, const etl::wformat_spec& format, const bool append = false) + { + return private_to_string::to_string(value, static_cast(str), format, append); + } +} + +#endif diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index 43be133e..0265eb9a 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -616,6 +616,21 @@ namespace etl typedef const type_t* const_pointer; typedef const type_t* const const_pointer_const; }; + + //*************************************************************************** + // size_of + //*************************************************************************** + template + struct size_of + { + static const size_t size = sizeof(T); + }; + + template <> + struct size_of + { + static const size_t size = 1; + }; } #endif diff --git a/include/etl/type_traits_generator.h b/include/etl/type_traits_generator.h index 5c237cac..32db7c49 100644 --- a/include/etl/type_traits_generator.h +++ b/include/etl/type_traits_generator.h @@ -621,6 +621,21 @@ namespace etl typedef const type_t* const_pointer; typedef const type_t* const const_pointer_const; }; + + //*************************************************************************** + // size_of + //*************************************************************************** + template + struct size_of + { + static const size_t size = sizeof(T); + }; + + template <> + struct size_of + { + static const size_t size = 1; + }; } #endif diff --git a/include/etl/u16format_spec.h b/include/etl/u16format_spec.h new file mode 100644 index 00000000..39c523ca --- /dev/null +++ b/include/etl/u16format_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_U16FORMAT_SPEC_INCLUDED +#define ETL_U16FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "u16string.h" + +namespace etl +{ + typedef etl::basic_format_spec u16format_spec; +} + +#endif diff --git a/include/etl/u16string.h b/include/etl/u16string.h index f0d7a08f..659a4aac 100644 --- a/include/etl/u16string.h +++ b/include/etl/u16string.h @@ -55,6 +55,9 @@ namespace etl { public: + typedef iu16string base_type; + typedef iu16string interface_type; + typedef iu16string::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/u32format_spec.h b/include/etl/u32format_spec.h new file mode 100644 index 00000000..2a9d34b8 --- /dev/null +++ b/include/etl/u32format_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_U32FORMAT_SPEC_INCLUDED +#define ETL_U32FORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "u32string.h" + +namespace etl +{ + typedef etl::basic_format_spec u32format_spec; +} + +#endif diff --git a/include/etl/u32string.h b/include/etl/u32string.h index 27792fcb..70826841 100644 --- a/include/etl/u32string.h +++ b/include/etl/u32string.h @@ -55,6 +55,9 @@ namespace etl { public: + typedef iu32string base_type; + typedef iu32string interface_type; + typedef iu32string::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/include/etl/version.h b/include/etl/version.h index 85d56f97..f338b0c8 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 17 -#define ETL_VERSION_PATCH 0 +#define ETL_VERSION_MINOR 23 +#define ETL_VERSION_PATCH 1 #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/include/etl/wformat_spec.h b/include/etl/wformat_spec.h new file mode 100644 index 00000000..0ff2d57e --- /dev/null +++ b/include/etl/wformat_spec.h @@ -0,0 +1,45 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2019 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. +******************************************************************************/ + +#ifndef ETL_WFORMAT_SPEC_INCLUDED +#define ETL_WFORMAT_SPEC_INCLUDED + +///\ingroup string + +#include "platform.h" +#include "basic_format_spec.h" +#include "wstring.h" + +namespace etl +{ + typedef etl::basic_format_spec wformat_spec; +} + +#endif diff --git a/include/etl/wstring.h b/include/etl/wstring.h index 388525c7..28b55b54 100644 --- a/include/etl/wstring.h +++ b/include/etl/wstring.h @@ -55,6 +55,9 @@ namespace etl { public: + typedef iwstring base_type; + typedef iwstring interface_type; + typedef iwstring::value_type value_type; static const size_t MAX_SIZE = MAX_SIZE_; diff --git a/library.json b/library.json new file mode 100644 index 00000000..2a6fdd34 --- /dev/null +++ b/library.json @@ -0,0 +1,27 @@ +{ + "name": "Embedded Template Library", + "version": "10.21.2", + "authors": { + "name": "John Wellbelove", + "email": "" + }, + "homepage": "https://www.etlcpp.com/", + "license": "MIT", + "description": "A C++ template library tailored for embedded systems. Requires some support from STL. See http://andybrown.me.uk/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/ or https://github.com/mike-matera/ArduinoSTL.git for Arduino.", + "repository": "https://github.com/ETLCPP/etl.git", + "platforms": [ + "espressif8266", + "espressif32" + ], + "frameworks": "arduino", + "build": { + "flags": [ + "-I include" + ], + "srcFilter": [ + "-", + "-", + "-" + ] +} +} diff --git a/support/Release notes.txt b/support/Release notes.txt index 1ef4d2dc..b01728aa 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,66 @@ +=============================================================================== +14.23.1 +Added missing 'cleanup' to string's resize() method. + +=============================================================================== +14.23.0 +Added an optional secure mode to strings so that unused space will be cleared to zero +and also cleared on destruction. + +=============================================================================== +14.22.1 +Modified memory functions so that they will not be optimised away. + +=============================================================================== +14.22.0 +Added etl::memory_clear and etl::memory_set utility functions. +These functions will overwrite the memory allocated to the supplied object. + +=============================================================================== +14.21.0 +Added variadic min and max functions. +etl::multimin +etl::multimin_compare +etl::multimin_iter +etl::multimin_iter_compare +etl::multimax +etl::multimax_compare +etl::multimax_iter +etl::multimax_iter_compare +C++11 only. + +=============================================================================== +14.20.0 +Added etl::multi_array for multi-dimensional arrays. +C++11 only. + +=============================================================================== +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. + +=============================================================================== +14.18.3 +Unified 'to string' API. + +=============================================================================== +14.18.2 +Finalised 'to string'. + +=============================================================================== +14.18.1 +Changed etl::format_sepc template to etl::basic_firmat_spec. +Created individual format_spec typedefs for each string type. + +=============================================================================== +14.18.0 +Added etl::to_string, etl::to_wstring, etl::to_u16string and etl::to_u32string +plus formating support for integrals. + =============================================================================== 14.17.0 Added C++11 variadic templates for etl::observer. diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index daf6c462..a4bfe86e 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -154,6 +154,7 @@ + @@ -197,6 +198,7 @@ + @@ -229,6 +231,7 @@ + @@ -247,6 +250,7 @@ + @@ -297,13 +301,19 @@ + + + + + + @@ -317,6 +327,7 @@ +