From abd4c104f8aaa1b8cc233ad8a4dd08155874cf79 Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Tue, 9 Jan 2018 23:26:48 -0500 Subject: [PATCH] common: Add ceph::for_each for tuples Expand to pairs, optionals, and variants later. Signed-off-by: Adam C. Emerson --- src/common/convenience.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/common/convenience.h b/src/common/convenience.h index c28349382095f..3989c70d1043c 100644 --- a/src/common/convenience.h +++ b/src/common/convenience.h @@ -229,5 +229,45 @@ auto maybe_do_or(const std::optional& t, F&& f, U&& u) -> else return std::forward(u); } + +namespace _convenience { +template +inline void for_each_helper(const std::tuple& t, const F& f, + std::index_sequence) { + (f(std::get(t)), ..., void()); +} +template +inline void for_each_helper(std::tuple& t, const F& f, + std::index_sequence) { + (f(std::get(t)), ..., void()); +} +template +inline void for_each_helper(const std::tuple& t, F& f, + std::index_sequence) { + (f(std::get(t)), ..., void()); +} +template +inline void for_each_helper(std::tuple& t, F& f, + std::index_sequence) { + (f(std::get(t)), ..., void()); +} +} + +template +inline void for_each(const std::tuple& t, const F& f) { + _convenience::for_each_helper(t, f, std::index_sequence_for{}); +} +template +inline void for_each(std::tuple& t, const F& f) { + _convenience::for_each_helper(t, f, std::index_sequence_for{}); +} +template +inline void for_each(const std::tuple& t, F& f) { + _convenience::for_each_helper(t, f, std::index_sequence_for{}); +} +template +inline void for_each(std::tuple& t, F& f) { + _convenience::for_each_helper(t, f, std::index_sequence_for{}); +} } #endif // CEPH_COMMON_CONVENIENCE_H -- 2.39.5