Apollo 11.0
自动驾驶开放平台
intra_writer.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_BLOCKER_INTRA_WRITER_H_
18#define CYBER_BLOCKER_INTRA_WRITER_H_
19
20#include <memory>
21
23#include "cyber/node/writer.h"
24
25namespace apollo {
26namespace cyber {
27namespace blocker {
28
29template <typename MessageT>
30class IntraWriter : public apollo::cyber::Writer<MessageT> {
31 public:
32 using MessagePtr = std::shared_ptr<MessageT>;
33 using BlockerManagerPtr = std::shared_ptr<BlockerManager>;
34
35 explicit IntraWriter(const proto::RoleAttributes& attr);
36 virtual ~IntraWriter();
37
38 bool Init() override;
39 void Shutdown() override;
40
41 bool Write(const MessageT& msg) override;
42 bool Write(const MessagePtr& msg_ptr) override;
43
44 private:
45 BlockerManagerPtr blocker_manager_;
46};
47
48template <typename MessageT>
51
52template <typename MessageT>
56
57template <typename MessageT>
59 {
60 std::lock_guard<std::mutex> g(this->lock_);
61 if (this->init_) {
62 return true;
63 }
64 blocker_manager_ = BlockerManager::Instance();
65 blocker_manager_->GetOrCreateBlocker<MessageT>(
66 BlockerAttr(this->role_attr_.channel_name()));
67 this->init_ = true;
68 }
69 return true;
70}
71
72template <typename MessageT>
74 {
75 std::lock_guard<std::mutex> g(this->lock_);
76 if (!this->init_) {
77 return;
78 }
79 this->init_ = false;
80 }
81 blocker_manager_ = nullptr;
82}
83
84template <typename MessageT>
85bool IntraWriter<MessageT>::Write(const MessageT& msg) {
86 if (!WriterBase::IsInit()) {
87 return false;
88 }
89 return blocker_manager_->Publish<MessageT>(this->role_attr_.channel_name(),
90 msg);
91}
92
93template <typename MessageT>
95 if (!WriterBase::IsInit()) {
96 return false;
97 }
98 return blocker_manager_->Publish<MessageT>(this->role_attr_.channel_name(),
99 msg_ptr);
100}
101
102} // namespace blocker
103} // namespace cyber
104} // namespace apollo
105
106#endif // CYBER_BLOCKER_INTRA_WRITER_H_
bool IsInit() const
Is Writer initialized?
Definition writer_base.h:99
static const std::shared_ptr< BlockerManager > & Instance()
bool Init() override
Init the Writer
std::shared_ptr< MessageT > MessagePtr
void Shutdown() override
Shutdown the Writer
bool Write(const MessageT &msg) override
Write a MessageT instance
IntraWriter(const proto::RoleAttributes &attr)
std::shared_ptr< BlockerManager > BlockerManagerPtr
class register implement
Definition arena_queue.h:37