forked from userver-framework/userver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat utils: publish constexpr_indices.hpp
67d1529c81286c3211465acb26b29a5eed28cd21
- Loading branch information
Showing
3 changed files
with
55 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
/// @file userver/utils/constexpr_indices.hpp | ||
/// @brief Functions for iterating over a constexpr range of integers | ||
|
||
#include <cstddef> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
namespace utils { | ||
|
||
namespace impl { | ||
|
||
template <std::size_t... Indices, typename Func> | ||
void DoForEachIndex(std::index_sequence<Indices...>, Func func) { | ||
static_assert(std::is_trivially_copyable_v<Func>); | ||
(..., func(std::integral_constant<std::size_t, Indices>{})); | ||
} | ||
|
||
template <std::size_t... Indices, typename Func> | ||
void DoWithConstexprIndex(std::index_sequence<Indices...>, | ||
std::size_t runtime_index, Func func) { | ||
static_assert(std::is_trivially_copyable_v<Func>); | ||
(..., (runtime_index == Indices | ||
? func(std::integral_constant<std::size_t, Indices>{}) | ||
: void())); | ||
} | ||
|
||
} // namespace impl | ||
|
||
/// Calls `func` with indices from range `0...<Count`, wrapped in | ||
/// `std::integral_constant`. | ||
template <std::size_t Count, typename Func> | ||
void ForEachIndex(Func func) { | ||
impl::DoForEachIndex(std::make_index_sequence<Count>{}, func); | ||
} | ||
|
||
/// Calls `func` with `runtime_index` wrapped in `std::integral_constant`. | ||
template <std::size_t Count, typename Func> | ||
void WithConstexprIndex(std::size_t runtime_index, Func func) { | ||
impl::DoWithConstexprIndex(std::make_index_sequence<Count>{}, runtime_index, | ||
func); | ||
} | ||
|
||
} // namespace utils | ||
|
||
USERVER_NAMESPACE_END |