pfor/src/pfor/tuple_view.h

60 lines
1.3 KiB
C++

#ifndef PFOR_PFOR_TUPLE_VIEW_H
#define PFOR_PFOR_TUPLE_VIEW_H
#include <cstdio>
#include <tuple>
#include "mp/meta.h"
#include "mp/pack.h"
namespace pfor {
template<typename, typename> class TupleView;
template<typename... Ts, std::size_t... indices>
class TupleView<std::tuple<Ts...>, Pack<UIntToType<indices>...>> {
public:
using Tuple = std::tuple<Ts...>;
using View = Pack<UIntToType<indices>...>;
private:
Tuple& _tuple;
public:
TupleView(Tuple& tuple): _tuple{tuple} {}
template<std::size_t index>
decltype(auto) get() {
return std::get<PackGet<View, index>::value>(_tuple);
}
template<std::size_t index>
decltype(auto) get() const {
return std::get<PackGet<View, index>::value>(_tuple);
}
};
template<typename... Ts, std::size_t... indices>
auto makeTupleView(std::tuple<Ts...>& tuple, Pack<UIntToType<indices>...>) {
return TupleView<std::tuple<Ts...>, Pack<UIntToType<indices>...>>{tuple};
}
}
namespace std {
template<std::size_t index, typename Tuple, typename View>
constexpr decltype(auto) get(pfor::TupleView<Tuple, View>& tupleView) {
return tupleView.template get<index>();
}
template<std::size_t index, typename Tuple, typename View>
constexpr decltype(auto) get(pfor::TupleView<Tuple, View> const& tupleView) {
return tupleView.template get<index>();
}
}
#endif