NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
ne_stl_tuple.h
浏览该文件的文档.
1#ifndef NE_STL_INCLUDENE_STL_TUPLE_H
2#define NE_STL_INCLUDENE_STL_TUPLE_H
3
4#include "ne_stl_any.h"
5#include "ne_stl_build.h"
7#include "ne_stl_memory.h"
8
9namespace nstd {
10
11template <int32_t... Ns>
12struct int_sequence {};
13template <int32_t... Ns>
15template <int32_t I, int32_t... Ns>
16struct int_sequence_gen<I, Ns...> {
17 using type = typename int_sequence_gen<I - 1, I - 1, Ns...>::type;
18};
19template <int32_t... Ns>
20struct int_sequence_gen<0, Ns...> {
21 using type = int_sequence<Ns...>;
22};
23template <int32_t N>
25template <typename... TArgs>
26struct tuple;
27template <>
28struct tuple<> {
29 std::size_t size() const { return values_.size(); }
31};
32template <typename T, typename... TArgs>
33struct tuple<T, TArgs...> : tuple<TArgs...> {
34 using value_type = T;
35 using base_type = tuple<TArgs...>;
36 using this_type = tuple<T, TArgs...>;
37 using base_type::values_;
38 tuple(const T& v, const TArgs&... tails)
39 : tuple<TArgs...>(tails...) {
40 values_.push_back(v);
41 }
42};
43template <uint32_t N, typename... TArgs>
44struct tuple_at;
45template <uint32_t N, typename T, typename... TArgs>
46struct tuple_at<N, tuple<T, TArgs...> > {
47 using value_type = typename tuple_at<N - 1, tuple<TArgs...> >::value_type;
48 using tuple_type = typename tuple_at<N - 1, tuple<TArgs...> >::tuple_type;
49};
50template <typename T, typename... TArgs>
51struct tuple_at<0, tuple<T, TArgs...> > {
52 using value_type = T;
53 using tuple_type = tuple<T, TArgs...>;
54};
55template <uint32_t N, typename... TArgs>
56const typename tuple_at<N, tuple<TArgs...> >::value_type& tuple_get(const tuple<TArgs...>& tuple_) {
57 return nstd::any_cast<const typename tuple_at<N, tuple<TArgs...> >::value_type&>(tuple_.values_[tuple_.values_.size() - 1 - N]);
58}
59template <uint32_t N, typename... TArgs>
60typename tuple_at<N, tuple<TArgs...> >::value_type tuple_get(tuple<TArgs...>& tuple_) {
61 using tuple_type = tuple<TArgs...>;
62 using base_tuple_type = typename tuple_at<N, tuple_type>::tuple_type;
63 return nstd::any_cast<typename tuple_at<N, tuple<TArgs...> >::value_type&>(*(tuple_.values_.rbegin() + N));
64}
65template <typename... Type>
66tuple<Type...> make_tuple(const Type&... args) {
67 return tuple<Type...>(args...);
68}
69template <typename TTuple, uint32_t... Ns>
70auto extract_tuple(TTuple& tup) -> decltype(make_tuple(tuple_get<Ns>(tup)...)) {
71 return make_tuple(tuple_get<Ns>(tup)...);
72}
73template <uint32_t... Ns, typename... TArgs>
74auto extract_tuple(tuple<TArgs...>& tup) -> decltype(make_tuple(tuple_get<Ns>(tup)...)) {
75 return make_tuple(tuple_get<Ns>(tup)...);
76}
77} // namespace nstd
78
79#endif // !NE_STL_INCLUDENE_STL_TUPLE_H
Definition: ne_stl_continuous_container.h:15
Definition: ne_stl_any.h:7
typename int_sequence_gen< N >::type sequence_t
Definition: ne_stl_tuple.h:24
auto extract_tuple(TTuple &tup) -> decltype(make_tuple(tuple_get< Ns >(tup)...))
Definition: ne_stl_tuple.h:70
tuple< Type... > make_tuple(const Type &... args)
Definition: ne_stl_tuple.h:66
const tuple_at< N, tuple< TArgs... > >::value_type & tuple_get(const tuple< TArgs... > &tuple_)
Definition: ne_stl_tuple.h:56
_ValueType * any_cast(any *operand)
Definition: ne_stl_any.h:117
typename int_sequence_gen< I - 1, I - 1, Ns... >::type type
Definition: ne_stl_tuple.h:17
Definition: ne_stl_tuple.h:14
Definition: ne_stl_tuple.h:12
Definition: ne_stl_tuple.h:33
tuple(const T &v, const TArgs &... tails)
Definition: ne_stl_tuple.h:38
T value_type
Definition: ne_stl_tuple.h:34
std::size_t size() const
Definition: ne_stl_tuple.h:29
nstd::vector< nstd::any > values_
Definition: ne_stl_tuple.h:30
T value_type
Definition: ne_stl_tuple.h:52
typename tuple_at< N - 1, tuple< TArgs... > >::value_type value_type
Definition: ne_stl_tuple.h:47
typename tuple_at< N - 1, tuple< TArgs... > >::tuple_type tuple_type
Definition: ne_stl_tuple.h:48
Definition: ne_stl_tuple.h:44
Definition: ne_stl_tuple.h:26