From 66cea447368986fd7ffa9810295b3d44c510d2f0 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 12 Jul 2014 21:37:49 +0900 Subject: [PATCH] add sprout::data() non-member function --- sprout/container/container_range_traits.hpp | 28 +++++++ sprout/container/data.hpp | 86 +++++++++++++++++++++ sprout/container/range_functions.hpp | 1 + sprout/container/range_functions_fwd.hpp | 10 +++ 4 files changed, 125 insertions(+) create mode 100644 sprout/container/data.hpp diff --git a/sprout/container/container_range_traits.hpp b/sprout/container/container_range_traits.hpp index 0b9db527..e062f9c7 100644 --- a/sprout/container/container_range_traits.hpp +++ b/sprout/container/container_range_traits.hpp @@ -372,6 +372,15 @@ namespace sprout { range_end(Container const& cont) { return cont.end(); } + // data access: + static SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(Container& cont) { + return cont.data(); + } + static SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(Container const& cont) { + return cont.data(); + } }; template struct container_range_traits { @@ -407,6 +416,11 @@ namespace sprout { range_at(Container const& cont, typename sprout::container_traits::size_type i) { return sprout::container_range_traits::range_at(cont, i); } + // data access: + static SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(Container const& cont) { + return sprout::container_range_traits::range_data(cont); + } }; template @@ -467,6 +481,15 @@ namespace sprout { range_at(T const (& arr)[N], typename sprout::container_traits::size_type i) { return arr[i]; } + // data access: + static SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(T (& arr)[N]) { + return &arr[0]; + } + static SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(T const (& arr)[N]) { + return &arr[0]; + } }; template struct container_range_traits { @@ -502,6 +525,11 @@ namespace sprout { range_at(T const (& arr)[N], typename sprout::container_traits::size_type i) { return sprout::container_range_traits::range_at(arr, i); } + // data access: + static SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(T const (& arr)[N]) { + return sprout::container_range_traits::range_data(arr); + } }; } // namespace sprout diff --git a/sprout/container/data.hpp b/sprout/container/data.hpp new file mode 100644 index 00000000..4d14d1bf --- /dev/null +++ b/sprout/container/data.hpp @@ -0,0 +1,86 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_CONTAINER_DATA_HPP +#define SPROUT_CONTAINER_DATA_HPP + +#include +#include +#include +#include +#include + +namespace sprout_adl { + sprout::not_found_via_adl range_data(...); +} // namespace sprout_adl + +namespace sprout { + namespace container_detail { + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(Container& cont) { + return sprout::container_range_traits::range_data(cont); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + range_data(Container const& cont) { + return sprout::container_range_traits::range_data(cont); + } + } // namespace container_detail + + // + // data + // + // effect: + // ADL callable range_data(cont) -> range_data(cont) + // otherwise -> sprout::container_range_traits::range_data(cont) + // [default] + // cont.data() + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + data(Container& cont) { + using sprout::container_detail::range_data; + using sprout_adl::range_data; + return range_data(cont); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + data(Container const& cont) { + using sprout::container_detail::range_data; + using sprout_adl::range_data; + return range_data(cont); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + data(T (& arr)[N]) { + return sprout::container_detail::range_data(arr); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + data(T const (& arr)[N]) { + return sprout::container_detail::range_data(arr); + } + + // + // cdata + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + cdata(Container const& cont) { + return sprout::data(cont); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::pointer + cdata(T const (& arr)[N]) { + return sprout::data(arr); + } +} // namespace sprout + +#include + +#endif // #ifndef SPROUT_CONTAINER_DATA_HPP diff --git a/sprout/container/range_functions.hpp b/sprout/container/range_functions.hpp index a4e594a4..5d144b36 100644 --- a/sprout/container/range_functions.hpp +++ b/sprout/container/range_functions.hpp @@ -17,5 +17,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_CONTAINER_RANGE_FUNCTIONS_HPP diff --git a/sprout/container/range_functions_fwd.hpp b/sprout/container/range_functions_fwd.hpp index 44b1da03..9bbd6db3 100644 --- a/sprout/container/range_functions_fwd.hpp +++ b/sprout/container/range_functions_fwd.hpp @@ -75,6 +75,16 @@ namespace sprout { template SPROUT_CONSTEXPR typename sprout::container_traits::reference at(Container const& cont, typename sprout::container_traits::size_type i); + + // + // data + // + template + SPROUT_CONSTEXPR typename sprout::container_traits::pointer + data(Container& cont); + template + SPROUT_CONSTEXPR typename sprout::container_traits::pointer + data(Container const& cont); } // namespace sprout