diff --git a/src/memory.h b/src/memory.h index c7abe285..e276d875 100644 --- a/src/memory.h +++ b/src/memory.h @@ -55,7 +55,21 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_fill(TOutputIterator o_begin, TOutputIterator o_end, const T& value) + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_fill(TOutputIterator o_begin, TOutputIterator o_end, T value) + { + std::fill(o_begin, o_end, value); + + return o_end; + } + + //***************************************************************************** + /// Fills uninitialised memory range with a value. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_fill(TOutputIterator o_begin, TOutputIterator o_end, const T& value) { typedef typename std::iterator_traits::value_type value_type; @@ -65,7 +79,7 @@ namespace etl ++o_begin; } - return o_begin; + return o_end; } //***************************************************************************** @@ -74,7 +88,23 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_fill(TOutputIterator o_begin, TOutputIterator o_end, const T& value, TCounter& count) + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_fill(TOutputIterator o_begin, TOutputIterator o_end, T value, TCounter& count) + { + std::fill(o_begin, o_end, value); + count += std::distance(o_begin, o_end); + + return o_end; + } + + //***************************************************************************** + /// Fills uninitialised memory range with a value. + /// Debug counter version. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_fill(TOutputIterator o_begin, TOutputIterator o_end, const T& value, TCounter& count) { typedef typename std::iterator_traits::value_type value_type; @@ -85,7 +115,7 @@ namespace etl ++count; } - return o_begin; + return o_end; } //***************************************************************************** @@ -93,7 +123,7 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_fill_n(TOutputIterator o_begin, TSize n, const T& value) + inline TOutputIterator uninitialized_fill_n(TOutputIterator o_begin, TSize n, const T& value) { return etl::uninitialized_fill(o_begin, o_begin + n, value); } @@ -104,7 +134,7 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_fill_n(TOutputIterator o_begin, TSize n, const T& value, TCounter& count) + inline TOutputIterator uninitialized_fill_n(TOutputIterator o_begin, TSize n, const T& value, TCounter& count) { return etl::uninitialized_fill(o_begin, o_begin + n, value, count); } @@ -113,8 +143,23 @@ namespace etl /// Copies a range of objects to uninitialised memory. ///\ingroup memory //***************************************************************************** - template - TOutputIterator uninitialized_copy(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin) + template + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_copy(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin) + { + TOutputIterator o_end = std::copy(i_begin, i_end, o_begin); + count += std::distance(o_begin, o_end); + + return o_end; + } + + //***************************************************************************** + /// Copies a range of objects to uninitialised memory. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_copy(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin) { typedef typename std::iterator_traits::value_type value_type; @@ -123,6 +168,7 @@ namespace etl ::new (static_cast(etl::addressof(*o_begin))) value_type(*i_begin); ++i_begin; ++o_begin; + ++count; } return o_begin; @@ -134,7 +180,23 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_copy(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TCounter& count) + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_copy(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TCounter& count) + { + TOutputIterator o_end = std::copy(i_begin, i_end, o_begin); + count += std::distance(o_begin, o_end); + + return o_end; + } + + //***************************************************************************** + /// Copies a range of objects to uninitialised memory. + /// Debug counter version. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_copy(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin, TCounter& count) { typedef typename std::iterator_traits::value_type value_type; @@ -154,7 +216,7 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_copy_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin) + inline TOutputIterator uninitialized_copy_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin) { return etl::uninitialized_copy(i_begin, i_begin + n, o_begin); } @@ -165,7 +227,7 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_copy_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TCounter& count) + inline TOutputIterator uninitialized_copy_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TCounter& count) { return etl::uninitialized_copy(i_begin, i_begin + n, o_begin, count); } @@ -175,17 +237,25 @@ namespace etl ///\ingroup memory //***************************************************************************** template - void uninitialized_default_construct(TOutputIterator o_begin, TOutputIterator o_end) + typename etl::enable_if::value_type>::value, void>::type + uninitialized_default_construct(TOutputIterator o_begin, TOutputIterator o_end) { - if (!etl::is_pod::type>::value) - { - typedef typename std::iterator_traits::value_type value_type; + } - while (o_begin != o_end) - { - ::new (static_cast(etl::addressof(*o_begin))) value_type; - ++o_begin; - } + //***************************************************************************** + /// Default initialises a range of objects to uninitialised memory. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, void>::type + uninitialized_default_construct(TOutputIterator o_begin, TOutputIterator o_end) + { + typedef typename std::iterator_traits::value_type value_type; + + while (o_begin != o_end) + { + ::new (static_cast(etl::addressof(*o_begin))) value_type; + ++o_begin; } } @@ -195,22 +265,28 @@ namespace etl ///\ingroup memory //***************************************************************************** template - void uninitialized_default_construct(TOutputIterator o_begin, TOutputIterator o_end, TCounter& count) + typename etl::enable_if::value_type>::value, void>::type + uninitialized_default_construct(TOutputIterator o_begin, TOutputIterator o_end, TCounter& count) { - if (!etl::is_pod::type>::value) - { - typedef typename std::iterator_traits::value_type value_type; + count = std::distance(o_begin, o_end); + } - while (o_begin != o_end) - { - ::new (static_cast(etl::addressof(*o_begin))) value_type; - ++o_begin; - ++count; - } - } - else + //***************************************************************************** + /// Default initialises a range of objects to uninitialised memory. + /// Debug counter version. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, void>::type + uninitialized_default_construct(TOutputIterator o_begin, TOutputIterator o_end, TCounter& count) + { + typedef typename std::iterator_traits::value_type value_type; + + while (o_begin != o_end) { - count = std::distance(o_begin, o_end); + ::new (static_cast(etl::addressof(*o_begin))) value_type; + ++o_begin; + ++count; } } @@ -219,14 +295,25 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_default_construct_n(TOutputIterator o_begin, TSize n) + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_default_construct_n(TOutputIterator o_begin, TSize n) { TOutputIterator o_end = o_begin + n; - if (!etl::is_pod::type>::value) - { - etl::uninitialized_default_construct(o_begin, o_end); - } + return o_end; + } + + //***************************************************************************** + /// Default initialises N objects to uninitialised memory. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_default_construct_n(TOutputIterator o_begin, TSize n) + { + TOutputIterator o_end = o_begin + n; + + etl::uninitialized_default_construct(o_begin, o_end); return o_end; } @@ -237,18 +324,28 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TOutputIterator uninitialized_default_construct_n(TOutputIterator o_begin, TSize n, TCounter& count) + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_default_construct_n(TOutputIterator o_begin, TSize n, TCounter& count) { TOutputIterator o_end = o_begin + n; - if (!etl::is_pod::type>::value) - { - etl::uninitialized_default_construct(o_begin, o_end, count); - } - else - { - count += n; - } + count += n; + + return o_end; + } + + //***************************************************************************** + /// Default initialises N objects to uninitialised memory. + /// Debug counter version. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TOutputIterator>::type + uninitialized_default_construct_n(TOutputIterator o_begin, TSize n, TCounter& count) + { + TOutputIterator o_end = o_begin + n; + + etl::uninitialized_default_construct(o_begin, o_end, count); return o_end; } @@ -367,15 +464,23 @@ namespace etl ///\ingroup memory //***************************************************************************** template - void destroy(TIterator i_begin, TIterator i_end) + typename etl::enable_if::value_type>::value, void>::type + destroy(TIterator i_begin, TIterator i_end) { - if (!etl::is_pod::type>::value) + } + + //***************************************************************************** + /// Destroys a range of items. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, void>::type + destroy(TIterator i_begin, TIterator i_end) + { + while (i_begin != i_end) { - while (i_begin != i_end) - { - etl::destroy_at(etl::addressof(*i_begin)); - ++i_begin; - } + etl::destroy_at(etl::addressof(*i_begin)); + ++i_begin; } } @@ -385,20 +490,26 @@ namespace etl ///\ingroup memory //***************************************************************************** template - void destroy(TIterator i_begin, TIterator i_end, TCounter& count) + typename etl::enable_if::value_type>::value, void>::type + destroy(TIterator i_begin, TIterator i_end, TCounter& count) { - if (!etl::is_pod::type>::value) + count -= std::distance(i_begin, i_end); + } + + //***************************************************************************** + /// Destroys a range of items. + /// Debug counter version. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, void>::type + destroy(TIterator i_begin, TIterator i_end, TCounter& count) + { + while (i_begin != i_end) { - while (i_begin != i_end) - { - etl::destroy_at(etl::addressof(*i_begin)); - ++i_begin; - --count; - } - } - else - { - count -= std::distance(i_begin, i_end); + etl::destroy_at(etl::addressof(*i_begin)); + ++i_begin; + --count; } } @@ -407,23 +518,28 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TIterator destroy_n(TIterator i_begin, TSize n) + typename etl::enable_if::value_type>::value, TIterator>::type + destroy_n(TIterator i_begin, TSize n) { - if (!etl::is_pod::type>::value) - { - while (n > 0) - { - etl::destroy_at(etl::addressof(*i_begin)); - ++i_begin; - --n; - } + return i_begin + n; + } - return i_begin; - } - else + //***************************************************************************** + /// Destroys a number of items. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TIterator>::type + destroy_n(TIterator i_begin, TSize n) + { + while (n > 0) { - return i_begin + n; + etl::destroy_at(etl::addressof(*i_begin)); + ++i_begin; + --n; } + + return i_begin; } //***************************************************************************** @@ -432,25 +548,31 @@ namespace etl ///\ingroup memory //***************************************************************************** template - TIterator destroy_n(TIterator i_begin, TSize n, TCounter& count) + typename etl::enable_if::value_type>::value, TIterator>::type + destroy_n(TIterator i_begin, TSize n, TCounter& count) { - if (!etl::is_pod::type>::value) - { - while (n > 0) - { - etl::destroy_at(etl::addressof(*i_begin)); - ++i_begin; - --n; - --count; - } + count -= n; + return i_begin + n; + } - return i_begin; - } - else + //***************************************************************************** + /// Destroys a number of items. + /// Debug counter version. + ///\ingroup memory + //***************************************************************************** + template + typename etl::enable_if::value_type>::value, TIterator>::type + destroy_n(TIterator i_begin, TSize n, TCounter& count) + { + while (n > 0) { - count -= n; - return i_begin + n; + etl::destroy_at(etl::addressof(*i_begin)); + ++i_begin; + --n; + --count; } + + return i_begin; } }