mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
null_array 追加
This commit is contained in:
parent
a52257117e
commit
6ae26b7470
7 changed files with 680 additions and 11 deletions
|
@ -131,12 +131,12 @@ namespace sprout {
|
|||
void swap(array<T, N>& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval<T&>(), std::declval<T&>()))) {
|
||||
std::swap_ranges(other.begin(), other.end(), begin());
|
||||
}
|
||||
template <typename T2>
|
||||
template<typename T2>
|
||||
array<T, N>& operator=(array<T2, N> const& rhs) {
|
||||
std::copy(rhs.begin(), rhs.end(), begin());
|
||||
return *this;
|
||||
}
|
||||
template <typename T2>
|
||||
template<typename T2>
|
||||
array<T, N>& operator=(array<T2, N>&& rhs) {
|
||||
std::move(rhs.begin(), rhs.end(), begin());
|
||||
return *this;
|
||||
|
|
|
@ -18,8 +18,13 @@ namespace sprout {
|
|||
return sprout::remake_clone_functor<Container>().template operator()(other, size, args...);
|
||||
}
|
||||
template<typename Container, typename Other, typename... Args>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fixed_container_traits<Container>::clone_type remake_clone(Other const& other, Args const&... args) {
|
||||
return sprout::remake_clone_functor<Container>().template operator()(other, args...);
|
||||
SPROUT_CONSTEXPR inline typename sprout::fixed_container_traits<Container>::clone_type remake_clone(
|
||||
Other const& other,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type size,
|
||||
Args const&... args
|
||||
)
|
||||
{
|
||||
return sprout::remake_clone_functor<Container>().template operator()(other, size, args...);
|
||||
}
|
||||
|
||||
//
|
||||
|
|
195
sprout/iterator/value_iterator.hpp
Normal file
195
sprout/iterator/value_iterator.hpp
Normal file
|
@ -0,0 +1,195 @@
|
|||
#ifndef SPROUT_ITERATOR_VALUE_ITERATOR_HPP
|
||||
#define SPROUT_ITERATOR_VALUE_ITERATOR_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <utility>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/next.hpp>
|
||||
#include <sprout/iterator/prev.hpp>
|
||||
#include <sprout/utility/value_holder.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// value_iterator
|
||||
//
|
||||
template<typename T>
|
||||
class value_iterator
|
||||
: public std::iterator<
|
||||
std::random_access_iterator_tag,
|
||||
typename sprout::value_holder<T>::value_type,
|
||||
std::ptrdiff_t,
|
||||
typename sprout::value_holder<T>::mutable_or_const_pointer,
|
||||
typename sprout::value_holder<T>::mutable_or_const_reference
|
||||
>
|
||||
{
|
||||
public:
|
||||
typedef T type;
|
||||
private:
|
||||
typedef std::iterator<
|
||||
std::random_access_iterator_tag,
|
||||
typename sprout::value_holder<T>::value_type,
|
||||
std::ptrdiff_t,
|
||||
typename sprout::value_holder<T>::mutable_or_const_pointer,
|
||||
typename sprout::value_holder<T>::mutable_or_const_reference
|
||||
> base_type;
|
||||
public:
|
||||
typedef typename base_type::iterator_category iterator_category;
|
||||
typedef typename base_type::value_type value_type;
|
||||
typedef typename base_type::difference_type difference_type;
|
||||
typedef typename base_type::pointer pointer;
|
||||
typedef typename base_type::reference reference;
|
||||
private:
|
||||
sprout::value_holder<T> holder_;
|
||||
difference_type count_;
|
||||
private:
|
||||
SPROUT_CONSTEXPR value_iterator(sprout::value_holder<T> const& r, std::size_t count)
|
||||
: holder_(r)
|
||||
, count_(count)
|
||||
{}
|
||||
public:
|
||||
SPROUT_CONSTEXPR value_iterator()
|
||||
: holder_()
|
||||
, count_()
|
||||
{}
|
||||
value_iterator(value_iterator const&) = default;
|
||||
SPROUT_CONSTEXPR explicit value_iterator(typename sprout::value_holder<T>::param_type p, std::size_t count = -1)
|
||||
: holder_(p)
|
||||
, count_(count)
|
||||
{}
|
||||
SPROUT_CONSTEXPR value_iterator next() const {
|
||||
return value_iterator(holder_, count_ != 0 ? count_ - 1 : count_);
|
||||
}
|
||||
SPROUT_CONSTEXPR value_iterator prev() const {
|
||||
return value_iterator(holder_, count_ != 0 ? count_ + 1 : count_);
|
||||
}
|
||||
void swap(value_iterator& other) {
|
||||
using std::swap;
|
||||
swap(holder_, other.holder_);
|
||||
swap(count_, other.count_);
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator==(value_iterator const& lhs, value_iterator const& rhs) {
|
||||
return lhs.count_ == rhs.count_ && (lhs.count_ == 0 || lhs.holder_.get() == rhs.holder_.get());
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator!=(value_iterator const& lhs, value_iterator const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
friend bool operator<(value_iterator const& lhs, value_iterator const& rhs) {
|
||||
return lhs.count_ > rhs.count_;
|
||||
}
|
||||
friend bool operator>(value_iterator const& lhs, value_iterator const& rhs) {
|
||||
return rhs < lhs;
|
||||
}
|
||||
friend bool operator<=(value_iterator const& lhs, value_iterator const& rhs) {
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
friend bool operator>=(value_iterator const& lhs, value_iterator const& rhs) {
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
SPROUT_CONSTEXPR reference operator*() const {
|
||||
return count_ != 0
|
||||
? holder_.get()
|
||||
: (throw "assert(count_ != 0)", holder_.get())
|
||||
;
|
||||
}
|
||||
SPROUT_CONSTEXPR pointer operator->() const {
|
||||
return count_ != 0
|
||||
? holder_.get_pointer()
|
||||
: throw "assert(count_ != 0)"
|
||||
;
|
||||
}
|
||||
value_iterator& operator++() {
|
||||
value_iterator temp(next());
|
||||
temp.swap(*this);
|
||||
return *this;
|
||||
}
|
||||
value_iterator operator++(int) {
|
||||
value_iterator result(*this);
|
||||
++*this;
|
||||
return result;
|
||||
}
|
||||
value_iterator& operator--() {
|
||||
value_iterator temp(prev());
|
||||
temp.swap(*this);
|
||||
return *this;
|
||||
}
|
||||
value_iterator operator--(int) {
|
||||
value_iterator result(*this);
|
||||
--*this;
|
||||
return result;
|
||||
}
|
||||
SPROUT_CONSTEXPR value_iterator operator+(difference_type n) const {
|
||||
return value_iterator(holder_, count_ - n);
|
||||
}
|
||||
SPROUT_CONSTEXPR value_iterator operator-(difference_type n) const {
|
||||
return value_iterator(holder_, count_ + n);
|
||||
}
|
||||
value_iterator& operator+=(difference_type n) {
|
||||
value_iterator temp(holder_, count_ - n);
|
||||
temp.swap(this);
|
||||
return this;
|
||||
}
|
||||
value_iterator& operator-=(difference_type n) {
|
||||
value_iterator temp(holder_, count_ + n);
|
||||
temp.swap(this);
|
||||
return this;
|
||||
}
|
||||
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
|
||||
return holder_.get();
|
||||
}
|
||||
friend difference_type operator-(value_iterator const& lhs, value_iterator const& rhs) {
|
||||
return rhs.count_ - lhs.count_;
|
||||
}
|
||||
friend SPROUT_CONSTEXPR value_iterator operator+(difference_type n, value_iterator const& it) {
|
||||
return it + n;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// swap
|
||||
//
|
||||
template<typename T>
|
||||
void swap(sprout::value_iterator<T>& lhs, sprout::value_iterator<T>& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
//
|
||||
// next
|
||||
//
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR sprout::value_iterator<T> next(
|
||||
sprout::value_iterator<T> const& it
|
||||
)
|
||||
{
|
||||
return it.next();
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR sprout::value_iterator<T> next(
|
||||
sprout::value_iterator<T> const& it,
|
||||
typename sprout::value_iterator<T>::difference_type n
|
||||
)
|
||||
{
|
||||
return it + n;
|
||||
}
|
||||
|
||||
//
|
||||
// prev
|
||||
//
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR sprout::value_iterator<T> prev(
|
||||
sprout::value_iterator<T> const& it
|
||||
)
|
||||
{
|
||||
return it.prev();
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR sprout::value_iterator<T> prev(
|
||||
sprout::value_iterator<T> const& it,
|
||||
typename sprout::value_iterator<T>::difference_type n
|
||||
)
|
||||
{
|
||||
return it - n;
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ITERATOR_VALUE_ITERATOR_HPP
|
300
sprout/null_array.hpp
Normal file
300
sprout/null_array.hpp
Normal file
|
@ -0,0 +1,300 @@
|
|||
#ifndef SPROUT_NULL_ARRAY_HPP
|
||||
#define SPROUT_NULL_ARRAY_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/iterator.hpp>
|
||||
#include <sprout/iterator/value_iterator.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// null_array
|
||||
//
|
||||
template<typename Container>
|
||||
class null_array {
|
||||
public:
|
||||
typedef Container container_type;
|
||||
typedef null_array fixed_container_type;
|
||||
typedef container_type internal_type;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::clone_type clone_type;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::value_type value_type;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::reference reference;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::const_reference const_reference;
|
||||
typedef typename sprout::value_iterator<reference> iterator;
|
||||
typedef typename sprout::value_iterator<const_reference> const_iterator;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::size_type size_type;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::difference_type difference_type;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::pointer pointer;
|
||||
typedef typename sprout::fixed_container_traits<internal_type>::const_pointer const_pointer;
|
||||
typedef typename sprout::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef typename sprout::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
public:
|
||||
SPROUT_STATIC_CONSTEXPR size_type static_size = sprout::fixed_container_traits<fixed_container_type>::fixed_size;
|
||||
SPROUT_STATIC_CONSTEXPR size_type fixed_size = static_size;
|
||||
public:
|
||||
value_type elem;
|
||||
public:
|
||||
null_array() = default;
|
||||
SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT {
|
||||
return static_size;
|
||||
}
|
||||
SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT {
|
||||
return static_size == 0;
|
||||
}
|
||||
SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT {
|
||||
return size();
|
||||
}
|
||||
void rangecheck(size_type i) const {
|
||||
if (i >= size()) {
|
||||
throw std::out_of_range("null_array<>: index out of range");
|
||||
}
|
||||
}
|
||||
iterator begin() {
|
||||
return iterator(elem, static_size);
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator begin() const {
|
||||
return const_iterator(elem, static_size);
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(elem, static_size);
|
||||
}
|
||||
iterator end() SPROUT_NOEXCEPT {
|
||||
return iterator();
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT {
|
||||
return const_iterator();
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
|
||||
return const_iterator();
|
||||
}
|
||||
reverse_iterator rbegin() SPROUT_NOEXCEPT {
|
||||
return reverse_iterator(end());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT {
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT {
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
reverse_iterator rend() SPROUT_NOEXCEPT {
|
||||
return reverse_iterator(begin());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_reverse_iterator rend() const SPROUT_NOEXCEPT {
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT {
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
reference operator[](size_type i) {
|
||||
return elem;
|
||||
}
|
||||
SPROUT_CONSTEXPR const_reference operator[](size_type i) const {
|
||||
return elem;
|
||||
}
|
||||
reference at(size_type i) {
|
||||
rangecheck(i);
|
||||
return elem;
|
||||
}
|
||||
const_reference at(size_type i) const {
|
||||
rangecheck(i);
|
||||
return elem;
|
||||
}
|
||||
reference front() {
|
||||
return elem;
|
||||
}
|
||||
SPROUT_CONSTEXPR const_reference front() const {
|
||||
return elem;
|
||||
}
|
||||
reference back() {
|
||||
return elem;
|
||||
}
|
||||
SPROUT_CONSTEXPR const_reference back() const {
|
||||
return elem;
|
||||
}
|
||||
void swap(null_array& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval<value_type&>(), std::declval<value_type&>()))) {
|
||||
using std::swap;
|
||||
swap(elem, other.elem);
|
||||
}
|
||||
};
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR inline bool operator==(sprout::null_array<Container> const& lhs, sprout::null_array<Container> const& rhs) {
|
||||
return lhs.front() == rhs.front();
|
||||
}
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR inline bool operator!=(sprout::null_array<Container> const& lhs, sprout::null_array<Container> const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR inline bool operator<(sprout::null_array<Container> const& lhs, sprout::null_array<Container> const& rhs) {
|
||||
return lhs.front() < rhs.front();
|
||||
}
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR inline bool operator>(sprout::null_array<Container> const& lhs, sprout::null_array<Container> const& rhs) {
|
||||
return rhs < lhs;
|
||||
}
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR inline bool operator<=(sprout::null_array<Container> const& lhs, sprout::null_array<Container> const& rhs) {
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR inline bool operator>=(sprout::null_array<Container> const& lhs, sprout::null_array<Container> const& rhs) {
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
//
|
||||
// swap
|
||||
//
|
||||
template<typename Container>
|
||||
inline void swap(sprout::null_array<Container>& lhs, sprout::null_array<Container>& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
//
|
||||
// fixed_container_traits
|
||||
//
|
||||
template<typename Container>
|
||||
struct fixed_container_traits<sprout::null_array<Container> >
|
||||
: public sprout::detail::fixed_container_traits_base<sprout::null_array<Container> >
|
||||
{
|
||||
public:
|
||||
typedef typename sprout::null_array<Container>::fixed_container_type fixed_container_type;
|
||||
typedef typename sprout::null_array<Container>::internal_type internal_type;
|
||||
typedef typename sprout::null_array<Container>::clone_type clone_type;
|
||||
public:
|
||||
SPROUT_STATIC_CONSTEXPR typename sprout::detail::fixed_container_traits_base<sprout::null_array<Container> >::size_type fixed_size
|
||||
= std::tuple_size<typename std::remove_const<internal_type>::type>::value
|
||||
;
|
||||
};
|
||||
|
||||
//
|
||||
// rebind_fixed_size
|
||||
//
|
||||
template<typename Container>
|
||||
struct rebind_fixed_size<sprout::null_array<Container> > {
|
||||
public:
|
||||
template<typename sprout::fixed_container_traits<sprout::null_array<Container> >::size_type S>
|
||||
struct apply {
|
||||
public:
|
||||
typedef sprout::null_array<
|
||||
typename sprout::rebind_fixed_size<
|
||||
typename sprout::fixed_container_traits<sprout::null_array<Container> >::internal_type
|
||||
>::template apply<S>::type
|
||||
> type;
|
||||
};
|
||||
};
|
||||
|
||||
//
|
||||
// clone_functor
|
||||
//
|
||||
template<typename Container>
|
||||
struct clone_functor<sprout::null_array<Container> > {
|
||||
private:
|
||||
typedef typename sprout::fixed_container_traits<sprout::null_array<Container> >::clone_type clone_type;
|
||||
public:
|
||||
clone_type operator()(sprout::null_array<Container>& cont) const {
|
||||
return clone_type();
|
||||
}
|
||||
SPROUT_CONSTEXPR clone_type operator()(sprout::null_array<Container> const& cont) const {
|
||||
return clone_type();
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// make_clone_functor
|
||||
//
|
||||
template<typename Container>
|
||||
struct make_clone_functor<sprout::null_array<Container> > {
|
||||
private:
|
||||
typedef typename sprout::fixed_container_traits<sprout::null_array<Container> >::clone_type clone_type;
|
||||
typedef typename sprout::fixed_container_traits<sprout::null_array<Container> >::internal_type internal_type;
|
||||
public:
|
||||
template<typename... Args>
|
||||
SPROUT_CONSTEXPR clone_type operator()(Args const&... args) const {
|
||||
return sprout::make_clone<internal_type>(args...);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// remake_clone_functor
|
||||
//
|
||||
template<typename Container>
|
||||
struct remake_clone_functor<sprout::null_array<Container> > {
|
||||
private:
|
||||
typedef typename sprout::fixed_container_traits<sprout::null_array<Container> >::clone_type clone_type;
|
||||
typedef typename sprout::fixed_container_traits<sprout::null_array<Container> >::internal_type internal_type;
|
||||
public:
|
||||
template<typename Other, typename... Args>
|
||||
clone_type operator()(
|
||||
Other& other,
|
||||
typename sprout::fixed_container_traits<sprout::null_array<Container> >::difference_type size,
|
||||
Args const&... args
|
||||
) const
|
||||
{
|
||||
return sprout::remake_clone<internal_type, Other>(other, size, args...);
|
||||
}
|
||||
template<typename Other, typename... Args>
|
||||
SPROUT_CONSTEXPR clone_type operator()(
|
||||
Other const& other,
|
||||
typename sprout::fixed_container_traits<sprout::null_array<Container> >::difference_type size,
|
||||
Args const&... args
|
||||
) const
|
||||
{
|
||||
return sprout::remake_clone<internal_type, Other>(other, size, args...);
|
||||
}
|
||||
};
|
||||
} // namespace sprout
|
||||
|
||||
namespace std {
|
||||
//
|
||||
// tuple_size
|
||||
//
|
||||
template<typename Container>
|
||||
struct tuple_size<sprout::null_array<Container> > {
|
||||
public:
|
||||
typedef std::integral_constant<std::size_t, sprout::fixed_container_traits<sprout::null_array<Container> >::fixed_size> type;
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t value = type::value;
|
||||
};
|
||||
|
||||
//
|
||||
// tuple_element
|
||||
//
|
||||
template<std::size_t I, typename Container>
|
||||
struct tuple_element<I, sprout::null_array<Container> > {
|
||||
public:
|
||||
static_assert(I < sprout::fixed_container_traits<sprout::null_array<Container> >::fixed_size, "tuple_element<>: index out of range");
|
||||
typedef typename sprout::fixed_container_traits<sprout::null_array<Container> >::value_type type;
|
||||
};
|
||||
|
||||
//
|
||||
// get
|
||||
//
|
||||
template<std::size_t I, typename Container>
|
||||
typename sprout::fixed_container_traits<sprout::null_array<Container> >::value_type& get(
|
||||
sprout::null_array<Container>& arr
|
||||
) SPROUT_NOEXCEPT
|
||||
{
|
||||
static_assert(I < sprout::fixed_container_traits<sprout::null_array<Container> >::fixed_size, "get: index out of range");
|
||||
return arr[I];
|
||||
}
|
||||
template<std::size_t I, typename Container>
|
||||
SPROUT_CONSTEXPR typename sprout::fixed_container_traits<sprout::null_array<Container> >::value_type const& get(
|
||||
sprout::null_array<Container> const& arr
|
||||
) SPROUT_NOEXCEPT
|
||||
{
|
||||
static_assert(I < sprout::fixed_container_traits<sprout::null_array<Container> >::fixed_size, "get: index out of range");
|
||||
return arr[I];
|
||||
}
|
||||
template<std::size_t I, typename Container>
|
||||
typename sprout::fixed_container_traits<sprout::null_array<Container> >::value_type&& get(
|
||||
sprout::null_array<Container>&& arr
|
||||
) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::move(std::get<I>(arr))))
|
||||
{
|
||||
return std::move(std::get<I>(arr));
|
||||
}
|
||||
} // namespace std
|
||||
|
||||
#endif // #ifndef SPROUT_NULL_ARRAY_HPP
|
|
@ -123,7 +123,7 @@ namespace sprout {
|
|||
;
|
||||
}
|
||||
SPROUT_CONSTEXPR pointer operator->() const {
|
||||
return count_ > 0
|
||||
return count_ != 0
|
||||
? &random_.result()
|
||||
: throw "assert(count_ != 0)"
|
||||
;
|
||||
|
|
|
@ -926,18 +926,27 @@ namespace std {
|
|||
//
|
||||
// get
|
||||
//
|
||||
template<std::size_t I, typename T, typename Container>
|
||||
T& get(sprout::sub_array<Container>& arr) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(*sprout::next(sprout::fixed_begin(arr), I))) {
|
||||
template<std::size_t I, typename Container>
|
||||
typename sprout::fixed_container_traits<sprout::sub_array<Container> >::value_type& get(
|
||||
sprout::sub_array<Container>& arr
|
||||
) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(*sprout::next(sprout::fixed_begin(arr), I)))
|
||||
{
|
||||
static_assert(I < sprout::fixed_container_traits<sprout::sub_array<Container> >::fixed_size, "get: index out of range");
|
||||
return *sprout::next(sprout::fixed_begin(arr), I);
|
||||
}
|
||||
template<std::size_t I, typename T, typename Container>
|
||||
SPROUT_CONSTEXPR T const& get(sprout::sub_array<Container> const& arr) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(*sprout::next(sprout::fixed_begin(arr), I))) {
|
||||
template<std::size_t I, typename Container>
|
||||
SPROUT_CONSTEXPR typename sprout::fixed_container_traits<sprout::sub_array<Container> >::value_type const& get(
|
||||
sprout::sub_array<Container> const& arr
|
||||
) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(*sprout::next(sprout::fixed_begin(arr), I)))
|
||||
{
|
||||
static_assert(I < sprout::fixed_container_traits<sprout::sub_array<Container> >::fixed_size, "get: index out of range");
|
||||
return *sprout::next(sprout::fixed_begin(arr), I);
|
||||
}
|
||||
template<std::size_t I, typename T, typename Container>
|
||||
T&& get(sprout::sub_array<Container>&& arr) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::move(std::get<I>(arr)))) {
|
||||
template<std::size_t I, typename Container>
|
||||
typename sprout::fixed_container_traits<sprout::sub_array<Container> >::value_type&& get(
|
||||
sprout::sub_array<Container>&& arr
|
||||
) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::move(std::get<I>(arr))))
|
||||
{
|
||||
return std::move(std::get<I>(arr));
|
||||
}
|
||||
} // namespace std
|
||||
|
|
160
sprout/utility/value_holder.hpp
Normal file
160
sprout/utility/value_holder.hpp
Normal file
|
@ -0,0 +1,160 @@
|
|||
#ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP
|
||||
#define SPROUT_UTILITY_VALUE_HOLDER_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
struct holder_helper {
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T& reference;
|
||||
typedef T const& const_reference;
|
||||
typedef T const& mutable_or_const_reference;
|
||||
typedef T* pointer;
|
||||
typedef T const* const_pointer;
|
||||
typedef T const* mutable_or_const_pointer;
|
||||
typedef T const& param_type;
|
||||
typedef T holder_type;
|
||||
public:
|
||||
static SPROUT_CONSTEXPR holder_type const& hold(param_type p) {
|
||||
return p;
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type& r) {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type& r) {
|
||||
return &r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) {
|
||||
return &r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct holder_helper<T const> {
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T const& reference;
|
||||
typedef T const& const_reference;
|
||||
typedef T const& mutable_or_const_reference;
|
||||
typedef T const* pointer;
|
||||
typedef T const* const_pointer;
|
||||
typedef T const* mutable_or_const_pointer;
|
||||
typedef T const& param_type;
|
||||
typedef T holder_type;
|
||||
public:
|
||||
static SPROUT_CONSTEXPR holder_type const& hold(param_type p) {
|
||||
return &p;
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type& r) {
|
||||
return *r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) {
|
||||
return *r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type& r) {
|
||||
return &r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) {
|
||||
return &r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct holder_helper<T&> {
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T& reference;
|
||||
typedef T const& const_reference;
|
||||
typedef T& mutable_or_const_reference;
|
||||
typedef T* pointer;
|
||||
typedef T const* const_pointer;
|
||||
typedef T* mutable_or_const_pointer;
|
||||
typedef T& param_type;
|
||||
typedef T* holder_type;
|
||||
public:
|
||||
static SPROUT_CONSTEXPR holder_type hold(param_type p) {
|
||||
return &p;
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type r) {
|
||||
return *r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct holder_helper<T const&> {
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T const& reference;
|
||||
typedef T const& const_reference;
|
||||
typedef T const& mutable_or_const_reference;
|
||||
typedef T const* pointer;
|
||||
typedef T const* const_pointer;
|
||||
typedef T const* mutable_or_const_pointer;
|
||||
typedef T const& param_type;
|
||||
typedef T const* holder_type;
|
||||
public:
|
||||
static SPROUT_CONSTEXPR holder_type hold(param_type p) {
|
||||
return &p;
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type r) {
|
||||
return *r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
//
|
||||
// value_holder
|
||||
//
|
||||
template<typename T>
|
||||
class value_holder {
|
||||
public:
|
||||
typedef T type;
|
||||
private:
|
||||
typedef sprout::detail::holder_helper<type> helper_type;
|
||||
typedef typename helper_type::holder_type holder_type;
|
||||
public:
|
||||
typedef typename helper_type::value_type value_type;
|
||||
typedef typename helper_type::reference reference;
|
||||
typedef typename helper_type::const_reference const_reference;
|
||||
typedef typename helper_type::mutable_or_const_reference mutable_or_const_reference;
|
||||
typedef typename helper_type::pointer pointer;
|
||||
typedef typename helper_type::const_pointer const_pointer;
|
||||
typedef typename helper_type::mutable_or_const_pointer mutable_or_const_pointer;
|
||||
typedef typename helper_type::param_type param_type;
|
||||
private:
|
||||
holder_type holder_;
|
||||
public:
|
||||
value_holder() = default;
|
||||
SPROUT_CONSTEXPR explicit value_holder(param_type p)
|
||||
: holder_(helper_type::hold(p))
|
||||
{}
|
||||
operator reference() {
|
||||
return helper_type::ref(holder_);
|
||||
}
|
||||
SPROUT_CONSTEXPR operator const_reference() const {
|
||||
return helper_type::ref(holder_);
|
||||
}
|
||||
reference get() {
|
||||
return helper_type::ref(holder_);
|
||||
}
|
||||
SPROUT_CONSTEXPR mutable_or_const_reference get() const {
|
||||
return helper_type::ref(holder_);
|
||||
}
|
||||
pointer get_pointer() {
|
||||
return helper_type::ptr(holder_);
|
||||
}
|
||||
SPROUT_CONSTEXPR mutable_or_const_pointer get_pointer() const {
|
||||
return helper_type::ptr(holder_);
|
||||
}
|
||||
};
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP
|
Loading…
Reference in a new issue