add sprout::data() non-member function

This commit is contained in:
bolero-MURAKAMI 2014-07-12 21:37:49 +09:00
parent c995ffd177
commit 66cea44736
4 changed files with 125 additions and 0 deletions

View file

@ -372,6 +372,15 @@ namespace sprout {
range_end(Container const& cont) { range_end(Container const& cont) {
return cont.end(); return cont.end();
} }
// data access:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::pointer
range_data(Container& cont) {
return cont.data();
}
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::pointer
range_data(Container const& cont) {
return cont.data();
}
}; };
template<typename Container> template<typename Container>
struct container_range_traits<Container const> { struct container_range_traits<Container const> {
@ -407,6 +416,11 @@ namespace sprout {
range_at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) { range_at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::container_range_traits<Container>::range_at(cont, i); return sprout::container_range_traits<Container>::range_at(cont, i);
} }
// data access:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::pointer
range_data(Container const& cont) {
return sprout::container_range_traits<Container>::range_data(cont);
}
}; };
template<typename T, std::size_t N> template<typename T, std::size_t N>
@ -467,6 +481,15 @@ namespace sprout {
range_at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) { range_at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return arr[i]; return arr[i];
} }
// data access:
static SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::pointer
range_data(T (& arr)[N]) {
return &arr[0];
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::pointer
range_data(T const (& arr)[N]) {
return &arr[0];
}
}; };
template<typename T, std::size_t N> template<typename T, std::size_t N>
struct container_range_traits<T const[N]> { struct container_range_traits<T const[N]> {
@ -502,6 +525,11 @@ namespace sprout {
range_at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) { range_at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return sprout::container_range_traits<T[N]>::range_at(arr, i); return sprout::container_range_traits<T[N]>::range_at(arr, i);
} }
// data access:
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::pointer
range_data(T const (& arr)[N]) {
return sprout::container_range_traits<T[N]>::range_data(arr);
}
}; };
} // namespace sprout } // namespace sprout

86
sprout/container/data.hpp Normal file
View file

@ -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 <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/container/traits_fwd.hpp>
#include <sprout/container/container_traits.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
sprout::not_found_via_adl range_data(...);
} // namespace sprout_adl
namespace sprout {
namespace container_detail {
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::pointer
range_data(Container& cont) {
return sprout::container_range_traits<Container>::range_data(cont);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::pointer
range_data(Container const& cont) {
return sprout::container_range_traits<Container>::range_data(cont);
}
} // namespace container_detail
//
// data
//
// effect:
// ADL callable range_data(cont) -> range_data(cont)
// otherwise -> sprout::container_range_traits<Container>::range_data(cont)
// [default]
// cont.data()
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::pointer
data(Container& cont) {
using sprout::container_detail::range_data;
using sprout_adl::range_data;
return range_data(cont);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::pointer
data(Container const& cont) {
using sprout::container_detail::range_data;
using sprout_adl::range_data;
return range_data(cont);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::pointer
data(T (& arr)[N]) {
return sprout::container_detail::range_data(arr);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::pointer
data(T const (& arr)[N]) {
return sprout::container_detail::range_data(arr);
}
//
// cdata
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::pointer
cdata(Container const& cont) {
return sprout::data(cont);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::pointer
cdata(T const (& arr)[N]) {
return sprout::data(arr);
}
} // namespace sprout
#include <sprout/container/container_range_traits.hpp>
#endif // #ifndef SPROUT_CONTAINER_DATA_HPP

View file

@ -17,5 +17,6 @@
#include <sprout/container/front.hpp> #include <sprout/container/front.hpp>
#include <sprout/container/back.hpp> #include <sprout/container/back.hpp>
#include <sprout/container/at.hpp> #include <sprout/container/at.hpp>
#include <sprout/container/data.hpp>
#endif // #ifndef SPROUT_CONTAINER_RANGE_FUNCTIONS_HPP #endif // #ifndef SPROUT_CONTAINER_RANGE_FUNCTIONS_HPP

View file

@ -75,6 +75,16 @@ namespace sprout {
template<typename Container> template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
at(Container const& cont, typename sprout::container_traits<Container const>::size_type i); at(Container const& cont, typename sprout::container_traits<Container const>::size_type i);
//
// data
//
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container>::pointer
data(Container& cont);
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::pointer
data(Container const& cont);
} // namespace sprout } // namespace sprout