Sprout/sprout/null_array.hpp

318 lines
11 KiB
C++
Raw Normal View History

2011-10-06 12:45:01 +00:00
#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;
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);
2011-10-06 12:45:01 +00:00
}
// iterators:
2011-10-06 12:45:01 +00:00
iterator begin() {
return iterator(elem, static_size);
}
SPROUT_CONSTEXPR const_iterator begin() const {
return const_iterator(elem, static_size);
}
iterator end() SPROUT_NOEXCEPT {
return iterator();
}
SPROUT_CONSTEXPR const_iterator end() 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());
}
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_iterator cbegin() const SPROUT_NOEXCEPT {
return const_iterator(elem, static_size);
}
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
return const_iterator();
}
SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT {
return const_reverse_iterator(end());
}
2011-10-06 12:45:01 +00:00
SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT {
return const_reverse_iterator(begin());
}
// capacity:
SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT {
return static_size;
}
SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT {
return size();
}
SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT {
return static_size == 0;
}
// element access:
2011-10-06 12:45:01 +00:00
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;
}
// others:
void rangecheck(size_type i) const {
if (i >= size()) {
throw std::out_of_range("null_array<>: index out of range");
}
2011-10-06 12:45:01 +00:00
}
};
2011-10-10 12:06:34 +00:00
template<typename Container>
SPROUT_CONSTEXPR typename sprout::null_array<Container>::size_type sprout::null_array<Container>::static_size;
template<typename Container>
SPROUT_CONSTEXPR typename sprout::null_array<Container>::size_type sprout::null_array<Container>::fixed_size;
//
// operator==
// operator!=
// operator<
// operator>
// operator<=
// operator>=
//
2011-10-06 12:45:01 +00:00
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