fifo_impl.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * fkie_message_filters
4  * Copyright © 2018-2020 Fraunhofer FKIE
5  * Author: Timo Röhling
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ****************************************************************************/
20 
21 #ifndef INCLUDE_FKIE_MESSAGE_FILTERS_COMBINER_POLICIES_FIFO_IMPL_H_
22 #define INCLUDE_FKIE_MESSAGE_FILTERS_COMBINER_POLICIES_FIFO_IMPL_H_
23 
24 #include "fifo.h"
25 #include "../helpers/tuple.h"
26 #include "../helpers/scoped_unlock.h"
27 
28 namespace fkie_message_filters
29 {
30 namespace combiner_policies
31 {
32 
33 template<typename... IOs>
34 Fifo<IOs...>::Fifo(std::size_t max_queue_size)
35 {
36  if (max_queue_size == 0) throw std::invalid_argument("max_queue_size must not be zero");
37  helpers::for_each_apply<sizeof...(IOs)>(
38  [this, max_queue_size](auto I)
39  {
40  std::get<I>(this->in_).set_capacity(max_queue_size);
41  }
42  );
43 }
44 
45 template<typename... IOs>
46 template<std::size_t N>
47 void Fifo<IOs...>::add(std::unique_lock<std::mutex>& lock, const std::tuple_element_t<N, IncomingTuples>& item)
48 {
49  std::get<N>(in_).push_back(item);
50  while (has_complete_tuple())
51  {
52  MaybeOutgoingTuples tmp = assemble_output();
53  helpers::index_apply<sizeof...(IOs)>(
54  [this, &tmp, &lock](auto... Is)
55  {
56  auto unlock = helpers::with_scoped_unlock(lock);
57  this->emit(std::tuple_cat(*std::get<Is>(tmp)...));
58  }
59  );
60  }
61 }
62 
63 template<typename... IOs>
65 {
66  bool result = true;
67  helpers::for_each_apply<sizeof...(IOs)>(
68  [this, &result](auto I)
69  {
70  if (std::get<I>(this->in_).empty()) result = false;
71  }
72  );
73  return result;
74 }
75 
76 
77 template<typename... IOs>
79 {
81  helpers::for_each_apply<sizeof...(IOs)>(
82  [this, &tmp](auto I)
83  {
84  std::get<I>(tmp) = std::get<I>(this->in_).front();
85  std::get<I>(this->in_).pop_front();
86  }
87  );
88  return tmp;
89 }
90 
91 template<typename... IOs>
92 void Fifo<IOs...>::reset() noexcept
93 {
94  helpers::for_each_apply<sizeof...(IOs)>(
95  [this](auto I)
96  {
97  std::get<I>(this->in_).clear();
98  }
99  );
100 }
101 
102 } // namespace combiner_policies
103 } // namespace fkie_message_filters
104 
105 #endif /* INCLUDE_FKIE_MESSAGE_FILTERS_COMBINER_POLICIES_FIFO_IMPL_H_ */
void add(std::unique_lock< std::mutex > &, const std::tuple_element_t< N, IncomingTuples > &)
Input function.
Definition: fifo_impl.h:47
void reset() noexcept override
Reset internal state.
Definition: fifo_impl.h:92
First-In-First-Out policy.
Definition: fifo.h:43
Fifo(std::size_t max_queue_size=1)
Constructor.
Definition: fifo_impl.h:34
std::tuple< boost::optional< helpers::io_tuple_t< IOs > >... > MaybeOutgoingTuples
Tuple of outgoing tuple candidates.
Definition: policy_base.h:64
void for_each_apply(Function f)
Definition: tuple.h:52
Primary namespace.
Definition: buffer.h:33
ScopedUnlock< BasicLockable > with_scoped_unlock(BasicLockable &lockable)
Definition: scoped_unlock.h:59
MaybeOutgoingTuples assemble_output() noexcept
Definition: fifo_impl.h:78
auto index_apply(Function f)
Definition: tuple.h:40


fkie_message_filters
Author(s): Timo Röhling
autogenerated on Mon Feb 28 2022 22:21:43