Sprout/sprout/container/container_holder.hpp

190 lines
6 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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)
=============================================================================*/
2013-02-26 01:43:27 +00:00
#ifndef SPROUT_CONTAINER_CONTAINER_HOLDER_HPP
#define SPROUT_CONTAINER_CONTAINER_HOLDER_HPP
#include <utility>
2013-02-26 08:03:30 +00:00
#include <stdexcept>
2013-02-26 01:43:27 +00:00
#include <sprout/config.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/value_holder/value_holder.hpp>
namespace sprout {
//
// container_holder
//
template<typename Container>
class container_holder
: public sprout::container_traits_facade<Container>
{
private:
typedef sprout::container_traits_facade<Container> facade_type;
public:
typedef Container container_type;
typedef container_type internal_type;
typedef typename facade_type::iterator iterator;
typedef typename facade_type::const_iterator const_iterator;
typedef typename facade_type::reference reference;
typedef typename facade_type::const_reference const_reference;
typedef typename facade_type::size_type size_type;
typedef typename facade_type::difference_type difference_type;
typedef typename facade_type::pointer pointer;
typedef typename facade_type::const_pointer const_pointer;
typedef internal_type& param_type;
typedef internal_type const& const_param_type;
typedef sprout::value_holder<param_type> holder_type;
protected:
holder_type container;
public:
SPROUT_CONSTEXPR container_holder()
: container()
{}
explicit SPROUT_CONSTEXPR container_holder(param_type x)
: container(x)
{}
2013-10-29 10:15:52 +00:00
container_holder(container_holder const&) = default;
2013-02-26 01:43:27 +00:00
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR void swap(container_holder& other)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout::swap(other.container, container))
2013-02-26 01:43:27 +00:00
{
sprout::swap(other.container, container);
}
// iterators:
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR iterator begin() {
2013-02-26 01:43:27 +00:00
return sprout::begin(*container);
}
SPROUT_CONSTEXPR const_iterator begin() const {
return sprout::begin(*container);
}
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR iterator end() {
2013-02-26 01:43:27 +00:00
return sprout::end(*container);
}
SPROUT_CONSTEXPR const_iterator end() const {
return sprout::end(*container);
}
SPROUT_CONSTEXPR const_iterator cbegin() const {
return sprout::begin(*container);
}
SPROUT_CONSTEXPR const_iterator cend() const {
return sprout::end(*container);
}
// capacity:
SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT {
return sprout::size(*container);
}
SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT {
return sprout::empty(*container);
}
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR param_type get_internal() {
2013-02-26 01:43:27 +00:00
return *container;
}
SPROUT_CONSTEXPR const_param_type get_internal() const {
return *container;
}
2013-02-26 08:03:30 +00:00
// element access:
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR reference operator[](size_type i) {
2013-02-26 08:03:30 +00:00
return *sprout::next(begin(), i);
}
SPROUT_CONSTEXPR const_reference operator[](size_type i) const {
return *sprout::next(begin(), i);
}
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR reference at(size_type i) {
2013-02-26 08:03:30 +00:00
return i < size() ? (*this)[i]
: (throw std::out_of_range("container_holder<>: index out of range"), (*this)[i])
;
}
SPROUT_CONSTEXPR const_reference at(size_type i) const {
return i < size() ? (*this)[i]
: (throw std::out_of_range("container_holder<>: index out of range"), (*this)[i])
;
}
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR reference front() {
2013-02-26 08:03:30 +00:00
return *begin();
}
SPROUT_CONSTEXPR const_reference front() const {
return *begin();
}
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR reference back() {
2013-02-26 08:03:30 +00:00
return *sprout::next(begin(), size() - 1);
}
SPROUT_CONSTEXPR const_reference back() const {
return *sprout::next(begin(), size() - 1);
}
2013-02-26 01:43:27 +00:00
};
//
// swap
//
template<typename Container>
2013-11-02 09:28:18 +00:00
inline SPROUT_CXX14_CONSTEXPR void
2013-02-26 01:43:27 +00:00
swap(sprout::container_holder<Container>& lhs, sprout::container_holder<Container>& rhs)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
2013-02-26 01:43:27 +00:00
{
lhs.swap(rhs);
}
//
// container_construct_traits
//
template<typename Container>
struct container_construct_traits<sprout::container_holder<Container> > {
public:
typedef typename sprout::container_construct_traits<Container>::copied_type copied_type;
public:
template<typename Cont>
static SPROUT_CONSTEXPR copied_type
deep_copy(Cont&& cont) {
return sprout::deep_copy(sprout::get_internal(SPROUT_FORWARD(Cont, cont)));
2013-02-26 01:43:27 +00:00
}
template<typename... Args>
static SPROUT_CONSTEXPR copied_type
make(Args&&... args) {
return sprout::make<copied_type>(SPROUT_FORWARD(Args, args)...);
2013-02-26 01:43:27 +00:00
}
template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type
remake(Cont&& cont, typename sprout::container_traits<Container>::difference_type size, Args&&... args) {
return sprout::remake<copied_type>(sprout::get_internal(SPROUT_FORWARD(Cont, cont)), size, SPROUT_FORWARD(Args, args)...);
2013-02-26 01:43:27 +00:00
}
};
//
// sub_container_traits
//
template<typename Container>
struct sub_container_traits<sprout::container_holder<Container> > {
private:
static typename sprout::container_holder<Container>::param_type
call(sprout::container_holder<Container>& cont) {
return cont.get_internal();
}
static SPROUT_CONSTEXPR typename sprout::container_holder<Container>::const_param_type
call(sprout::container_holder<Container> const& cont) {
return cont.get_internal();
}
public:
template<typename Cont>
struct internal {
public:
typedef decltype(call(std::declval<Cont&&>())) type;
};
public:
template<typename Cont>
static SPROUT_CONSTEXPR typename internal<Cont>::type
get_internal(Cont&& cont) {
return call(SPROUT_FORWARD(Cont, cont));
2013-02-26 01:43:27 +00:00
}
};
} // namespace sprout
#endif // #ifndef SPROUT_CONTAINER_CONTAINER_HOLDER_HPP