Apollo  6.0
Open source self driving car software
routine_factory.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_CROUTINE_ROUTINE_FACTORY_H_
18 #define CYBER_CROUTINE_ROUTINE_FACTORY_H_
19 
20 #include <memory>
21 #include <utility>
22 
24 #include "cyber/common/log.h"
28 
29 namespace apollo {
30 namespace cyber {
31 namespace croutine {
32 
34  public:
35  using VoidFunc = std::function<void()>;
36  using CreateRoutineFunc = std::function<VoidFunc()>;
37  // We can use routine_func directly.
39  inline std::shared_ptr<data::DataVisitorBase> GetDataVisitor() const {
40  return data_visitor_;
41  }
42  inline void SetDataVisitor(const std::shared_ptr<data::DataVisitorBase>& dv) {
43  data_visitor_ = dv;
44  }
45 
46  private:
47  std::shared_ptr<data::DataVisitorBase> data_visitor_ = nullptr;
48 };
49 
50 template <typename M0, typename F>
52  F&& f, const std::shared_ptr<data::DataVisitor<M0>>& dv) {
53  RoutineFactory factory;
54  factory.SetDataVisitor(dv);
55  factory.create_routine = [=]() {
56  return [=]() {
57  std::shared_ptr<M0> msg;
58  for (;;) {
60  if (dv->TryFetch(msg)) {
61  f(msg);
63  } else {
65  }
66  }
67  };
68  };
69  return factory;
70 }
71 
72 template <typename M0, typename M1, typename F>
74  F&& f, const std::shared_ptr<data::DataVisitor<M0, M1>>& dv) {
75  RoutineFactory factory;
76  factory.SetDataVisitor(dv);
77  factory.create_routine = [=]() {
78  return [=]() {
79  std::shared_ptr<M0> msg0;
80  std::shared_ptr<M1> msg1;
81  for (;;) {
83  if (dv->TryFetch(msg0, msg1)) {
84  f(msg0, msg1);
86  } else {
88  }
89  }
90  };
91  };
92  return factory;
93 }
94 
95 template <typename M0, typename M1, typename M2, typename F>
97  F&& f, const std::shared_ptr<data::DataVisitor<M0, M1, M2>>& dv) {
98  RoutineFactory factory;
99  factory.SetDataVisitor(dv);
100  factory.create_routine = [=]() {
101  return [=]() {
102  std::shared_ptr<M0> msg0;
103  std::shared_ptr<M1> msg1;
104  std::shared_ptr<M2> msg2;
105  for (;;) {
107  if (dv->TryFetch(msg0, msg1, msg2)) {
108  f(msg0, msg1, msg2);
110  } else {
111  CRoutine::Yield();
112  }
113  }
114  };
115  };
116  return factory;
117 }
118 
119 template <typename M0, typename M1, typename M2, typename M3, typename F>
121  F&& f, const std::shared_ptr<data::DataVisitor<M0, M1, M2, M3>>& dv) {
122  RoutineFactory factory;
123  factory.SetDataVisitor(dv);
124  factory.create_routine = [=]() {
125  return [=]() {
126  std::shared_ptr<M0> msg0;
127  std::shared_ptr<M1> msg1;
128  std::shared_ptr<M2> msg2;
129  std::shared_ptr<M3> msg3;
130  for (;;) {
132  if (dv->TryFetch(msg0, msg1, msg2, msg3)) {
133  f(msg0, msg1, msg2, msg3);
135  } else {
136  CRoutine::Yield();
137  }
138  }
139  };
140  };
141  return factory;
142 }
143 
144 template <typename Function>
146  RoutineFactory factory;
147  factory.create_routine = [f = std::forward<Function&&>(f)]() { return f; };
148  return factory;
149 }
150 
151 } // namespace croutine
152 } // namespace cyber
153 } // namespace apollo
154 
155 #endif // CYBER_CROUTINE_ROUTINE_FACTORY_H_
CreateRoutineFunc create_routine
Definition: routine_factory.h:38
void SetDataVisitor(const std::shared_ptr< data::DataVisitorBase > &dv)
Definition: routine_factory.h:42
RoutineFactory CreateRoutineFactory(F &&f, const std::shared_ptr< data::DataVisitor< M0 >> &dv)
Definition: routine_factory.h:51
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
static CRoutine * GetCurrentRoutine()
Definition: croutine.h:135
Definition: routine_factory.h:33
std::function< void()> VoidFunc
Definition: routine_factory.h:35
static void Yield()
Definition: croutine.h:131
void set_state(const RoutineState &state)
Definition: croutine.h:145
std::function< VoidFunc()> CreateRoutineFunc
Definition: routine_factory.h:36
std::shared_ptr< data::DataVisitorBase > GetDataVisitor() const
Definition: routine_factory.h:39
Definition: data_visitor.h:48