diff --git a/include/etl/unaligned_type.h b/include/etl/unaligned_type.h index 54b72205..4e5cb5dc 100644 --- a/include/etl/unaligned_type.h +++ b/include/etl/unaligned_type.h @@ -217,6 +217,9 @@ namespace etl //************************************************************************* /// unaligned_type + ///\brief Allows an arithmetic type to be stored at an unaligned address. + ///\tparam T The arithmetic type. + ///\tparam Endian The endianness of the arithmetic type. //************************************************************************* template class unaligned_type : public private_unaligned_type::unaligned_type_common diff --git a/include/etl/utility.h b/include/etl/utility.h index 509896de..00f54248 100644 --- a/include/etl/utility.h +++ b/include/etl/utility.h @@ -93,11 +93,11 @@ namespace etl } #endif - /** - * @brief pair holds two objects of arbitrary type - * - * @tparam T1, T2 The types of the elements that the pair stores - */ + //*************************************************************************** + ///\brief pair holds two objects of arbitrary type + /// + ///\tparam T1, T2 The types of the elements that the pair stores + //*************************************************************************** template struct pair { @@ -107,22 +107,22 @@ namespace etl T1 first; ///< @c first is a copy of the first object T2 second; ///< @c second is a copy of the second object - /** - * @brief Default constructor - * - * The default constructor creates @c first and @c second using their respective default constructors. - */ + //*************************************************************************** + ///\brief Default constructor + /// + /// The default constructor creates @c first and @c second using their respective default constructors. + //*************************************************************************** ETL_CONSTEXPR pair() : first(T1()) , second(T2()) { } - /** - * @brief Constructor from parameters - * - * Two objects may be passed to a @c pair constructor to be copied. - */ + //*************************************************************************** + ///\brief Constructor from parameters + /// + /// Two objects may be passed to a @c pair constructor to be copied. + //*************************************************************************** ETL_CONSTEXPR14 pair(const T1& a, const T2& b) : first(a) , second(b) @@ -130,9 +130,9 @@ namespace etl } #if ETL_USING_CPP11 - /** - * @brief Move constructor from parameters. - */ + //*************************************************************************** + ///\brief Move constructor from parameters. + //*************************************************************************** template ETL_CONSTEXPR14 pair(U1&& a, U2&& b) : first(etl::forward(a)) @@ -141,11 +141,11 @@ namespace etl } #endif - /** - * @brief Copy constructor - * - * There is also a templated copy constructor for the @c pair class itself. - */ + //*************************************************************************** + ///\brief Copy constructor + /// + /// There is also a templated copy constructor for the @c pair class itself. + //*************************************************************************** template ETL_CONSTEXPR14 pair(const pair& other) : first(other.first) @@ -242,14 +242,14 @@ namespace etl #endif }; - /** - * @brief A convenience wrapper for creating a @ref pair from two objects. - * - * @param a The first object. - * @param b The second object. - * - * @return A newly-constructed @ref pair object of the appropriate type. - */ + //*************************************************************************** + ///\brief A convenience wrapper for creating a @ref pair from two objects. + /// + ///\param a The first object. + ///\param b The second object. + /// + ///\return A newly-constructed @ref pair object of the appropriate type. + //*************************************************************************** #if ETL_USING_CPP11 template inline pair make_pair(T1&& a, T2&& b) @@ -313,74 +313,94 @@ namespace etl return !(a < b); } - /** - * @brief Functor to select @ref pair::first - * - * @ref select1st is a functor object that takes a single argument, a @ref pair, and returns the @ref pair::first element. - * - * @b Example - * @snippet test_utility.cpp test_select1st_example - * - * @tparam TPair The function object's argument type. - * - * @see select2nd - */ + //*************************************************************************** + ///\brief Functor to select @ref pair::first + /// + ///\ref select1st is a functor object that takes a single argument, a @ref pair, and returns the @ref pair::first element. + /// + ///\b Example + ///\snippet test_utility.cpp test_select1st_example + /// + ///\tparam TPair The function object's argument type. + /// + ///\see select2nd + //*************************************************************************** template struct select1st { typedef typename TPair::first_type type; ///< type of member @ref pair::first. - /** - * @brief Function call that return @c p.first. - * @return a reference to member @ref pair::first of the @c pair `p` - */ + //*************************************************************************** + ///\brief Function call that return @c p.first. + ///\return a reference to member @ref pair::first of the @c pair `p` + //*************************************************************************** type& operator()(TPair& p) const { return p.first; } - /** - * @copydoc operator()(TPair&)const - */ + //*************************************************************************** + ///\copydoc operator()(TPair&)const + // const type& operator()(const TPair& p) const { return p.first; } + +#if ETL_CPP11_SUPPORTED + //*************************************************************************** + ///\copydoc operator()(TPair&)const + //*************************************************************************** + type&& operator()(TPair&& p) const + { + return etl::move(p.first); + } +#endif }; - /** - * @brief Functor to select @ref pair::second - * - * @ref select2nd is a functor object that takes a single argument, a @ref pair, and returns the @ref pair::second element. - * - * @b Example - * @snippet test_utility.cpp test_select2nd_example - * - * @tparam TPair The function object's argument type. - * - * @see select1st - */ + //*************************************************************************** + ///\brief Functor to select @ref pair::second + /// + ///\ref select2nd is a functor object that takes a single argument, a @ref pair, and returns the @ref pair::second element. + /// + ///\b Example + ///\snippet test_utility.cpp test_select2nd_example + /// + ///\tparam TPair The function object's argument type. + /// + ///\see select1st + //*************************************************************************** template struct select2nd { typedef typename TPair::second_type type; ///< type of member @ref pair::second. - /** - * @brief Function call. The return value is `p.second`. - * @return a reference to member `second` of the pair `p`. - */ + //*************************************************************************** + ///\brief Function call. The return value is `p.second`. + ///\return a reference to member `second` of the pair `p`. + //*************************************************************************** type& operator()(TPair& p) const { return p.second; } - /** - * @copydoc operator()(TPair&)const - */ + //*************************************************************************** + ///\copydoc operator()(TPair&)const + //*************************************************************************** const type& operator()(const TPair& p) const { return p.second; } + +#if ETL_CPP11_SUPPORTED + //*************************************************************************** + ///\copydoc operator()(TPair&)const + //*************************************************************************** + type&& operator()(TPair&& p) const + { + return etl::move(p.second); + } +#endif }; #if ETL_NOT_USING_STL || ETL_CPP14_NOT_SUPPORTED diff --git a/test/test_utility.cpp b/test/test_utility.cpp index b686092d..aa4c66c9 100644 --- a/test/test_utility.cpp +++ b/test/test_utility.cpp @@ -32,6 +32,7 @@ SOFTWARE. #include #include +#include #include "data.h" @@ -348,11 +349,23 @@ namespace CHECK_EQUAL(2, selector(sp2)); } + //************************************************************************* + TEST(test_select1st_move) + { + typedef etl::pair EtlPair; + typedef std::pair StdPair; + + auto selector = etl::select1st(); + + CHECK_EQUAL(1, selector(std::move(EtlPair(1, "Hello")))); + CHECK_EQUAL(2, selector(std::move(StdPair(2, "Hello")))); + } + //************************************************************************* TEST(test_select1st_example) { //! [test_select1st_example] - using Map = std::map; + using Map = std::map; using Vector = std::vector; const Map map = {{1, 0.3}, @@ -360,7 +373,7 @@ namespace {33, 0.1}}; Vector result{}; - // extract the map keys into a vector + // Extract the map keys into a vector std::transform(map.begin(), map.end(), std::back_inserter(result), etl::select1st()); //! [test_select1st_example] @@ -384,11 +397,25 @@ namespace CHECK_EQUAL(std::string("World"), selector(sp2)); } + //************************************************************************* + TEST(test_select2nd_move) + { + typedef etl::pair EtlPair; + typedef std::pair StdPair; + + EtlPair ep1(1, "Hello"); + StdPair sp2(2, "World"); + + auto selector = etl::select2nd(); + CHECK_EQUAL(std::string("Hello"), selector(std::move(EtlPair(1, "Hello")))); + CHECK_EQUAL(std::string("World"), selector(std::move(StdPair(1, "World")))); + } + //************************************************************************* TEST(test_select2nd_example) { //! [test_select2nd_example] - using Map = std::map; + using Map = std::map; using Vector = std::vector; const Map map = {{1, 0.3}, @@ -396,7 +423,7 @@ namespace {33, 0.1}}; Vector result{}; - // extract the map values into a vector + // Extract the map values into a vector std::transform(map.begin(), map.end(), std::back_inserter(result), etl::select2nd()); //! [test_select2nd_example]