Apollo  6.0
Open source self driving car software
intra_writer.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_BLOCKER_INTRA_WRITER_H_
18 #define CYBER_BLOCKER_INTRA_WRITER_H_
19 
20 #include <memory>
21 
23 #include "cyber/node/writer.h"
24 
25 namespace apollo {
26 namespace cyber {
27 namespace blocker {
28 
29 template <typename MessageT>
30 class 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 
48 template <typename MessageT>
49 IntraWriter<MessageT>::IntraWriter(const proto::RoleAttributes& attr)
50  : Writer<MessageT>(attr) {}
51 
52 template <typename MessageT>
54  Shutdown();
55 }
56 
57 template <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 
72 template <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 
84 template <typename MessageT>
85 bool 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 
93 template <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_
Definition: intra_writer.h:30
std::mutex lock_
Definition: writer_base.h:99
proto::RoleAttributes role_attr_
Definition: writer_base.h:98
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool init_
Definition: writer_base.h:100
static const std::shared_ptr< BlockerManager > & Instance()
Definition: blocker_manager.h:38
bool Write(const MessageT &msg) override
Write a MessageT instance.
Definition: intra_writer.h:85
Definition: writer.h:42
Definition: blocker.h:50
void Shutdown() override
Shutdown the Writer.
Definition: intra_writer.h:73
bool IsInit() const
Is Writer initialized?
Definition: writer_base.h:92
std::shared_ptr< BlockerManager > BlockerManagerPtr
Definition: intra_writer.h:33
bool Init() override
Init the Writer.
Definition: intra_writer.h:58
IntraWriter(const proto::RoleAttributes &attr)
Definition: intra_writer.h:49
virtual ~IntraWriter()
Definition: intra_writer.h:53
std::shared_ptr< MessageT > MessagePtr
Definition: intra_writer.h:32