Apollo
10.0
自动驾驶开放平台
routine_factory.h
浏览该文件的文档.
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
23
#include "
cyber/common/global_data.h
"
24
#include "
cyber/common/log.h
"
25
#include "
cyber/croutine/croutine.h
"
26
#include "
cyber/data/data_visitor.h
"
27
#include "
cyber/event/perf_event_cache.h
"
28
29
namespace
apollo
{
30
namespace
cyber {
31
namespace
croutine {
32
33
class
RoutineFactory
{
34
public
:
35
using
VoidFunc
= std::function<void()>;
36
using
CreateRoutineFunc
= std::function<
VoidFunc
()>;
37
// We can use routine_func directly.
38
CreateRoutineFunc
create_routine
;
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>
51
RoutineFactory
CreateRoutineFactory
(
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
(;;) {
59
CRoutine::GetCurrentRoutine
()->
set_state
(
RoutineState::DATA_WAIT
);
60
if
(dv->TryFetch(msg)) {
61
f
(msg);
62
CRoutine::Yield
(
RoutineState::READY
);
63
}
else
{
64
CRoutine::Yield
();
65
}
66
}
67
};
68
};
69
return
factory;
70
}
71
72
template
<
typename
M0,
typename
M1,
typename
F>
73
RoutineFactory
CreateRoutineFactory
(
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
(;;) {
82
CRoutine::GetCurrentRoutine
()->
set_state
(
RoutineState::DATA_WAIT
);
83
if
(dv->TryFetch(msg0, msg1)) {
84
f
(msg0, msg1);
85
CRoutine::Yield
(
RoutineState::READY
);
86
}
else
{
87
CRoutine::Yield
();
88
}
89
}
90
};
91
};
92
return
factory;
93
}
94
95
template
<
typename
M0,
typename
M1,
typename
M2,
typename
F>
96
RoutineFactory
CreateRoutineFactory
(
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
(;;) {
106
CRoutine::GetCurrentRoutine
()->
set_state
(
RoutineState::DATA_WAIT
);
107
if
(dv->TryFetch(msg0, msg1, msg2)) {
108
f
(msg0, msg1, msg2);
109
CRoutine::Yield
(
RoutineState::READY
);
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>
120
RoutineFactory
CreateRoutineFactory
(
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
(;;) {
131
CRoutine::GetCurrentRoutine
()->
set_state
(
RoutineState::DATA_WAIT
);
132
if
(dv->TryFetch(msg0, msg1, msg2, msg3)) {
133
f
(msg0, msg1, msg2, msg3);
134
CRoutine::Yield
(
RoutineState::READY
);
135
}
else
{
136
CRoutine::Yield
();
137
}
138
}
139
};
140
};
141
return
factory;
142
}
143
144
template
<
typename
Function>
145
RoutineFactory
CreateRoutineFactory
(Function&&
f
) {
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_
f
double f
Definition
a_star_strategy.cc:33
apollo::cyber::croutine::CRoutine::Yield
static void Yield()
Definition
croutine.h:131
apollo::cyber::croutine::CRoutine::set_state
void set_state(const RoutineState &state)
Definition
croutine.h:145
apollo::cyber::croutine::CRoutine::GetCurrentRoutine
static CRoutine * GetCurrentRoutine()
Definition
croutine.h:135
apollo::cyber::croutine::RoutineFactory
Definition
routine_factory.h:33
apollo::cyber::croutine::RoutineFactory::GetDataVisitor
std::shared_ptr< data::DataVisitorBase > GetDataVisitor() const
Definition
routine_factory.h:39
apollo::cyber::croutine::RoutineFactory::create_routine
CreateRoutineFunc create_routine
Definition
routine_factory.h:38
apollo::cyber::croutine::RoutineFactory::CreateRoutineFunc
std::function< VoidFunc()> CreateRoutineFunc
Definition
routine_factory.h:36
apollo::cyber::croutine::RoutineFactory::SetDataVisitor
void SetDataVisitor(const std::shared_ptr< data::DataVisitorBase > &dv)
Definition
routine_factory.h:42
apollo::cyber::croutine::RoutineFactory::VoidFunc
std::function< void()> VoidFunc
Definition
routine_factory.h:35
apollo::cyber::data::DataVisitor
Definition
data_visitor.h:48
croutine.h
data_visitor.h
global_data.h
log.h
apollo::cyber::croutine::CreateRoutineFactory
RoutineFactory CreateRoutineFactory(F &&f, const std::shared_ptr< data::DataVisitor< M0 > > &dv)
Definition
routine_factory.h:51
apollo::cyber::croutine::RoutineState::READY
@ READY
apollo::cyber::croutine::RoutineState::DATA_WAIT
@ DATA_WAIT
apollo
class register implement
Definition
arena_queue.h:37
perf_event_cache.h
cyber
croutine
routine_factory.h