every_n.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // 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
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_POLICIES_EVERY_N_HPP
16 #define BELUGA_POLICIES_EVERY_N_HPP
17 
19 
25 namespace beluga::policies {
26 
27 namespace detail {
28 
30 
34  public:
36 
39  explicit constexpr every_n_policy(std::size_t count) : count_(count) {}
40 
42 
47  constexpr bool operator()() {
48  current_ = (current_ + 1) % count_;
49  return current_ == 0;
50  }
51 
52  private:
53  std::size_t count_{0};
54  std::size_t current_{0};
55 };
56 
58 struct every_n_fn {
60  constexpr auto operator()(std::size_t count) const { return beluga::make_policy(every_n_policy{count}); }
61 };
62 
63 } // namespace detail
64 
66 
70 inline constexpr detail::every_n_fn every_n;
71 
72 } // namespace beluga::policies
73 
74 #endif
beluga::policies
Definition: every_n.hpp:25
beluga::make_policy
constexpr detail::make_policy_fn make_policy
Make policy function objects.
Definition: policy.hpp:42
beluga::policies::detail::every_n_policy
Implementation detail for the every_n_policy.
Definition: every_n.hpp:33
beluga::policies::detail::every_n_policy::every_n_policy
constexpr every_n_policy(std::size_t count)
Constructor.
Definition: every_n.hpp:39
beluga::policies::detail::every_n_policy::operator()
constexpr bool operator()()
Call operator overload.
Definition: every_n.hpp:47
beluga::policies::every_n
constexpr detail::every_n_fn every_n
Policy that triggers an action every N calls.
Definition: every_n.hpp:70
beluga::policies::detail::every_n_fn
Implementation detail for an every_n_fn object.
Definition: every_n.hpp:58
beluga::policies::detail::every_n_policy::current_
std::size_t current_
The current count of calls.
Definition: every_n.hpp:54
policy.hpp
beluga::policies::detail::every_n_policy::count_
std::size_t count_
The count specifying when the action should be triggered.
Definition: every_n.hpp:53
beluga::policies::detail::every_n_fn::operator()
constexpr auto operator()(std::size_t count) const
Overload that creates a policy closure.
Definition: every_n.hpp:60


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53