/Users/deen/code/yugabyte-db/src/yb/util/shared_ptr_tuple.h
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | |
16 | | #ifndef YB_UTIL_SHARED_PTR_TUPLE_H |
17 | | #define YB_UTIL_SHARED_PTR_TUPLE_H |
18 | | |
19 | | #include <memory> |
20 | | |
21 | | namespace yb { |
22 | | namespace util { |
23 | | |
24 | | // This utility uses C++11 features: variadic template & static assert. |
25 | | template <typename ... Types> |
26 | | struct TypeIndexer; |
27 | | |
28 | | template <> |
29 | | struct TypeIndexer<> { |
30 | | protected: |
31 | 0 | static constexpr int num_types() { return 0; } |
32 | | |
33 | | template <typename Type> |
34 | 0 | static constexpr int index_of_type(int) { return -1; } Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJEE13index_of_typeINS_7tserver24TabletServerServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJEE13index_of_typeINS_7tserver29TabletServerAdminServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJEE13index_of_typeINS_9consensus21ConsensusServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJEE13index_of_typeINS_7tserver30TabletServerBackupServiceProxyEEEii |
35 | | |
36 | | template <class Tuple, const int = 0> |
37 | 5.74k | static void reset_tail(Tuple*) {} |
38 | | }; |
39 | | |
40 | | template <typename T, typename ... Types> |
41 | | struct TypeIndexer<T, Types ...> : private TypeIndexer<Types ...> { |
42 | 0 | static constexpr int num_types() { return super::num_types() + 1; } Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_9consensus21ConsensusServiceProxyEEE9num_typesEv Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE9num_typesEv Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE9num_typesEv Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE9num_typesEv |
43 | | |
44 | | template <typename Type> |
45 | 0 | static constexpr int index_of() { return index_of_type<Type>(0); } Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE8index_ofIS4_EEiv Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE8index_ofIS3_EEiv Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE8index_ofIS7_EEiv Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE8index_ofIS5_EEiv |
46 | | |
47 | | protected: |
48 | | typedef TypeIndexer<Types ...> super; |
49 | | |
50 | | template <typename Type> |
51 | 0 | static constexpr int index_of_type(int i) { |
52 | 0 | return std::is_same<T, Type>::value ? i : super::template index_of_type<Type>(i + 1); |
53 | 0 | } Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_9consensus21ConsensusServiceProxyEEE13index_of_typeINS_7tserver24TabletServerServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeINS2_24TabletServerServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS3_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS4_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_9consensus21ConsensusServiceProxyEEE13index_of_typeINS_7tserver29TabletServerAdminServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeINS2_29TabletServerAdminServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeINS2_29TabletServerAdminServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS3_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS3_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS5_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS6_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS7_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_9consensus21ConsensusServiceProxyEEE13index_of_typeINS_7tserver30TabletServerBackupServiceProxyEEEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS3_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS4_EEii Unexecuted instantiation: _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE13index_of_typeIS5_EEii |
54 | | |
55 | | template <class Tuple, const int i = 0> |
56 | 22.9k | static void reset_tail(Tuple* tuple) { |
57 | 22.9k | std::get<i>(*tuple).reset(); |
58 | 22.9k | super::template reset_tail<Tuple, i + 1>(tuple); |
59 | 22.9k | } _ZN2yb4util11TypeIndexerIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE10reset_tailINSt3__15tupleIJNSA_10shared_ptrIS3_EENSC_IS4_EENSC_IS5_EENSC_IS7_EEEEELi0EEEvPT_ Line | Count | Source | 56 | 5.74k | static void reset_tail(Tuple* tuple) { | 57 | 5.74k | std::get<i>(*tuple).reset(); | 58 | 5.74k | super::template reset_tail<Tuple, i + 1>(tuple); | 59 | 5.74k | } |
_ZN2yb4util11TypeIndexerIJNS_7tserver24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE10reset_tailINSt3__15tupleIJNS9_10shared_ptrINS2_29TabletServerAdminServiceProxyEEENSB_IS3_EENSB_IS4_EENSB_IS6_EEEEELi1EEEvPT_ Line | Count | Source | 56 | 5.74k | static void reset_tail(Tuple* tuple) { | 57 | 5.74k | std::get<i>(*tuple).reset(); | 58 | 5.74k | super::template reset_tail<Tuple, i + 1>(tuple); | 59 | 5.74k | } |
_ZN2yb4util11TypeIndexerIJNS_7tserver30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE10reset_tailINSt3__15tupleIJNS8_10shared_ptrINS2_29TabletServerAdminServiceProxyEEENSA_INS2_24TabletServerServiceProxyEEENSA_IS3_EENSA_IS5_EEEEELi2EEEvPT_ Line | Count | Source | 56 | 5.74k | static void reset_tail(Tuple* tuple) { | 57 | 5.74k | std::get<i>(*tuple).reset(); | 58 | 5.74k | super::template reset_tail<Tuple, i + 1>(tuple); | 59 | 5.74k | } |
_ZN2yb4util11TypeIndexerIJNS_9consensus21ConsensusServiceProxyEEE10reset_tailINSt3__15tupleIJNS6_10shared_ptrINS_7tserver29TabletServerAdminServiceProxyEEENS8_INS9_24TabletServerServiceProxyEEENS8_INS9_30TabletServerBackupServiceProxyEEENS8_IS3_EEEEELi3EEEvPT_ Line | Count | Source | 56 | 5.74k | static void reset_tail(Tuple* tuple) { | 57 | 5.74k | std::get<i>(*tuple).reset(); | 58 | 5.74k | super::template reset_tail<Tuple, i + 1>(tuple); | 59 | 5.74k | } |
|
60 | | }; |
61 | | |
62 | | // The class extends std::tuple<> functionality by ability |
63 | | // to get the tuple element by type (instead of index). |
64 | | // |
65 | | // class A {}; class B {}; class C {}; |
66 | | // |
67 | | // std::tuple<A, B, C> t; // Usual std::tuple. |
68 | | // B& b = std::get<1>(t); // Constructed by default, returned by index. |
69 | | // |
70 | | // SharedPtrTuple<A, B, C> t; |
71 | | // std::shared_ptr<B>& b = t.get<B>(); // The shared_ptr is empty. Returned by the type. |
72 | | // b.reset(new B); |
73 | | // |
74 | | // t.reset(); // Reset all internal shared_ptr-s. |
75 | | // |
76 | | template<class ... Types> |
77 | | struct SharedPtrTuple : public TypeIndexer<Types ...> { |
78 | | template <class Type> |
79 | 1.08M | std::shared_ptr<Type>& get() { |
80 | 1.08M | return std::get<checked_index_of<Type>()>(storage_); |
81 | 1.08M | } _ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE3getIS4_EERNSt3__110shared_ptrIT_EEv Line | Count | Source | 79 | 272k | std::shared_ptr<Type>& get() { | 80 | 272k | return std::get<checked_index_of<Type>()>(storage_); | 81 | 272k | } |
_ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE3getIS3_EERNSt3__110shared_ptrIT_EEv Line | Count | Source | 79 | 272k | std::shared_ptr<Type>& get() { | 80 | 272k | return std::get<checked_index_of<Type>()>(storage_); | 81 | 272k | } |
_ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE3getIS7_EERNSt3__110shared_ptrIT_EEv Line | Count | Source | 79 | 272k | std::shared_ptr<Type>& get() { | 80 | 272k | return std::get<checked_index_of<Type>()>(storage_); | 81 | 272k | } |
_ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE3getIS5_EERNSt3__110shared_ptrIT_EEv Line | Count | Source | 79 | 272k | std::shared_ptr<Type>& get() { | 80 | 272k | return std::get<checked_index_of<Type>()>(storage_); | 81 | 272k | } |
|
82 | | |
83 | | template <class Type> |
84 | | const std::shared_ptr<Type>& get() const { |
85 | | return std::get<checked_index_of<Type>()>(storage_); |
86 | | } |
87 | | |
88 | | template <class Type> |
89 | | bool has_type() const { return super::template index_of<Type>() >= 0; } |
90 | | |
91 | 5.74k | void reset() { super::template reset_tail(&storage_); } |
92 | | |
93 | | protected: |
94 | | typedef TypeIndexer<Types ...> super; |
95 | | |
96 | | template <class Type> |
97 | 0 | static constexpr int checked_index_of() { |
98 | 0 | constexpr int index = super::template index_of<Type>(); |
99 | 0 | static_assert(index >= 0, "Unknown type 'Type'"); |
100 | 0 | static_assert(index < super::num_types(), "Internal error: invalid index"); |
101 | 0 | return index; |
102 | 0 | } Unexecuted instantiation: _ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE16checked_index_ofIS4_EEiv Unexecuted instantiation: _ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE16checked_index_ofIS3_EEiv Unexecuted instantiation: _ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE16checked_index_ofIS7_EEiv Unexecuted instantiation: _ZN2yb4util14SharedPtrTupleIJNS_7tserver29TabletServerAdminServiceProxyENS2_24TabletServerServiceProxyENS2_30TabletServerBackupServiceProxyENS_9consensus21ConsensusServiceProxyEEE16checked_index_ofIS5_EEiv |
103 | | |
104 | | std::tuple<std::shared_ptr<Types> ...> storage_; |
105 | | }; |
106 | | |
107 | | } // namespace util |
108 | | } // namespace yb |
109 | | |
110 | | #endif // YB_UTIL_SHARED_PTR_TUPLE_H |