Apollo  6.0
Open source self driving car software
all_latest.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_DATA_FUSION_ALL_LATEST_H_
18 #define CYBER_DATA_FUSION_ALL_LATEST_H_
19 
20 #include <deque>
21 #include <memory>
22 #include <string>
23 #include <tuple>
24 #include <type_traits>
25 #include <typeinfo>
26 #include <vector>
27 
28 #include "cyber/common/types.h"
31 
32 namespace apollo {
33 namespace cyber {
34 namespace data {
35 namespace fusion {
36 
37 template <typename M0, typename M1 = NullType, typename M2 = NullType,
38  typename M3 = NullType>
39 class AllLatest : public DataFusion<M0, M1, M2, M3> {
40  using FusionDataType = std::tuple<std::shared_ptr<M0>, std::shared_ptr<M1>,
41  std::shared_ptr<M2>, std::shared_ptr<M3>>;
42 
43  public:
44  AllLatest(const ChannelBuffer<M0>& buffer_0,
45  const ChannelBuffer<M1>& buffer_1,
46  const ChannelBuffer<M2>& buffer_2,
47  const ChannelBuffer<M3>& buffer_3)
48  : buffer_m0_(buffer_0),
49  buffer_m1_(buffer_1),
50  buffer_m2_(buffer_2),
51  buffer_m3_(buffer_3),
52  buffer_fusion_(buffer_m0_.channel_id(),
53  new CacheBuffer<std::shared_ptr<FusionDataType>>(
54  buffer_0.Buffer()->Capacity() - uint64_t(1))) {
55  buffer_m0_.Buffer()->SetFusionCallback(
56  [this](const std::shared_ptr<M0>& m0) {
57  std::shared_ptr<M1> m1;
58  std::shared_ptr<M2> m2;
59  std::shared_ptr<M3> m3;
60  if (!buffer_m1_.Latest(m1) || !buffer_m2_.Latest(m2) ||
61  !buffer_m3_.Latest(m3)) {
62  return;
63  }
64 
65  auto data = std::make_shared<FusionDataType>(m0, m1, m2, m3);
66  std::lock_guard<std::mutex> lg(buffer_fusion_.Buffer()->Mutex());
67  buffer_fusion_.Buffer()->Fill(data);
68  });
69  }
70 
71  bool Fusion(uint64_t* index, std::shared_ptr<M0>& m0, std::shared_ptr<M1>& m1,
72  std::shared_ptr<M2>& m2, std::shared_ptr<M3>& m3) override {
73  std::shared_ptr<FusionDataType> fusion_data;
74  if (!buffer_fusion_.Fetch(index, fusion_data)) {
75  return false;
76  }
77  m0 = std::get<0>(*fusion_data);
78  m1 = std::get<1>(*fusion_data);
79  m2 = std::get<2>(*fusion_data);
80  m3 = std::get<3>(*fusion_data);
81  return true;
82  }
83 
84  private:
85  ChannelBuffer<M0> buffer_m0_;
86  ChannelBuffer<M1> buffer_m1_;
87  ChannelBuffer<M2> buffer_m2_;
88  ChannelBuffer<M3> buffer_m3_;
89  ChannelBuffer<FusionDataType> buffer_fusion_;
90 };
91 
92 template <typename M0, typename M1, typename M2>
93 class AllLatest<M0, M1, M2, NullType> : public DataFusion<M0, M1, M2> {
94  using FusionDataType =
95  std::tuple<std::shared_ptr<M0>, std::shared_ptr<M1>, std::shared_ptr<M2>>;
96 
97  public:
98  AllLatest(const ChannelBuffer<M0>& buffer_0,
99  const ChannelBuffer<M1>& buffer_1,
100  const ChannelBuffer<M2>& buffer_2)
101  : buffer_m0_(buffer_0),
102  buffer_m1_(buffer_1),
103  buffer_m2_(buffer_2),
104  buffer_fusion_(buffer_m0_.channel_id(),
105  new CacheBuffer<std::shared_ptr<FusionDataType>>(
106  buffer_0.Buffer()->Capacity() - uint64_t(1))) {
107  buffer_m0_.Buffer()->SetFusionCallback(
108  [this](const std::shared_ptr<M0>& m0) {
109  std::shared_ptr<M1> m1;
110  std::shared_ptr<M2> m2;
111  if (!buffer_m1_.Latest(m1) || !buffer_m2_.Latest(m2)) {
112  return;
113  }
114 
115  auto data = std::make_shared<FusionDataType>(m0, m1, m2);
116  std::lock_guard<std::mutex> lg(buffer_fusion_.Buffer()->Mutex());
117  buffer_fusion_.Buffer()->Fill(data);
118  });
119  }
120 
121  bool Fusion(uint64_t* index, std::shared_ptr<M0>& m0, std::shared_ptr<M1>& m1,
122  std::shared_ptr<M2>& m2) override {
123  std::shared_ptr<FusionDataType> fusion_data;
124  if (!buffer_fusion_.Fetch(index, fusion_data)) {
125  return false;
126  }
127  m0 = std::get<0>(*fusion_data);
128  m1 = std::get<1>(*fusion_data);
129  m2 = std::get<2>(*fusion_data);
130  return true;
131  }
132 
133  private:
134  ChannelBuffer<M0> buffer_m0_;
135  ChannelBuffer<M1> buffer_m1_;
136  ChannelBuffer<M2> buffer_m2_;
137  ChannelBuffer<FusionDataType> buffer_fusion_;
138 };
139 
140 template <typename M0, typename M1>
141 class AllLatest<M0, M1, NullType, NullType> : public DataFusion<M0, M1> {
142  using FusionDataType = std::tuple<std::shared_ptr<M0>, std::shared_ptr<M1>>;
143 
144  public:
145  AllLatest(const ChannelBuffer<M0>& buffer_0,
146  const ChannelBuffer<M1>& buffer_1)
147  : buffer_m0_(buffer_0),
148  buffer_m1_(buffer_1),
149  buffer_fusion_(buffer_m0_.channel_id(),
150  new CacheBuffer<std::shared_ptr<FusionDataType>>(
151  buffer_0.Buffer()->Capacity() - uint64_t(1))) {
152  buffer_m0_.Buffer()->SetFusionCallback(
153  [this](const std::shared_ptr<M0>& m0) {
154  std::shared_ptr<M1> m1;
155  if (!buffer_m1_.Latest(m1)) {
156  return;
157  }
158 
159  auto data = std::make_shared<FusionDataType>(m0, m1);
160  std::lock_guard<std::mutex> lg(buffer_fusion_.Buffer()->Mutex());
161  buffer_fusion_.Buffer()->Fill(data);
162  });
163  }
164 
165  bool Fusion(uint64_t* index, std::shared_ptr<M0>& m0,
166  std::shared_ptr<M1>& m1) override {
167  std::shared_ptr<FusionDataType> fusion_data;
168  if (!buffer_fusion_.Fetch(index, fusion_data)) {
169  return false;
170  }
171  m0 = std::get<0>(*fusion_data);
172  m1 = std::get<1>(*fusion_data);
173  return true;
174  }
175 
176  private:
177  ChannelBuffer<M0> buffer_m0_;
178  ChannelBuffer<M1> buffer_m1_;
179  ChannelBuffer<FusionDataType> buffer_fusion_;
180 };
181 
182 } // namespace fusion
183 } // namespace data
184 } // namespace cyber
185 } // namespace apollo
186 
187 #endif // CYBER_DATA_FUSION_ALL_LATEST_H_
bool Fetch(uint64_t *index, std::shared_ptr< T > &m)
Definition: channel_buffer.h:57
AllLatest(const ChannelBuffer< M0 > &buffer_0, const ChannelBuffer< M1 > &buffer_1, const ChannelBuffer< M2 > &buffer_2, const ChannelBuffer< M3 > &buffer_3)
Definition: all_latest.h:44
bool Fusion(uint64_t *index, std::shared_ptr< M0 > &m0, std::shared_ptr< M1 > &m1) override
Definition: all_latest.h:165
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: future.h:29
bool Latest(std::shared_ptr< T > &m)
Definition: channel_buffer.h:80
bool Fusion(uint64_t *index, std::shared_ptr< M0 > &m0, std::shared_ptr< M1 > &m1, std::shared_ptr< M2 > &m2) override
Definition: all_latest.h:121
AllLatest(const ChannelBuffer< M0 > &buffer_0, const ChannelBuffer< M1 > &buffer_1)
Definition: all_latest.h:145
Definition: data_fusion.h:36
Definition: types.h:25
Definition: cache_buffer.h:30
std::shared_ptr< BufferType > Buffer() const
Definition: channel_buffer.h:49
AllLatest(const ChannelBuffer< M0 > &buffer_0, const ChannelBuffer< M1 > &buffer_1, const ChannelBuffer< M2 > &buffer_2)
Definition: all_latest.h:98
Definition: all_latest.h:39
bool Fusion(uint64_t *index, std::shared_ptr< M0 > &m0, std::shared_ptr< M1 > &m1, std::shared_ptr< M2 > &m2, std::shared_ptr< M3 > &m3) override
Definition: all_latest.h:71