提交 dfd41c6b 编写于 作者: muleisheng's avatar muleisheng 提交者: Calvin Miao

Bridge: use shared_ptr to replace pointer

上级 2814c18f
......@@ -17,6 +17,7 @@
#pragma once
#include <string>
#include <memory>
#include "modules/bridge/common/bridge_proto_diserialized_buf.h"
#include "modules/canbus/proto/chassis.pb.h"
#include "modules/common/adapters/adapter_gflags.h"
......@@ -26,10 +27,11 @@ namespace bridge {
class ProtoDiserializedBufBaseFactory {
public:
static ProtoDiserializedBufBase *CreateObj(const BridgeHeader &header) {
ProtoDiserializedBufBase *obj = nullptr;
static std::shared_ptr<ProtoDiserializedBufBase> CreateObj(
const BridgeHeader &header) {
std::shared_ptr<ProtoDiserializedBufBase> obj;
if (strcmp("Chassis", header.GetMsgName().c_str()) == 0) {
obj = new BridgeProtoDiserializedBuf<canbus::Chassis>(
obj = std::make_shared<BridgeProtoDiserializedBuf<canbus::Chassis>>(
FLAGS_chassis_topic);
}
return obj;
......
......@@ -59,6 +59,23 @@ bool RemoveItem(std::vector<T *> *list, const T *t) {
}
return true;
}
template <typename T>
bool RemoveItem(std::vector<std::shared_ptr<T>> *list, std::shared_ptr<T> t) {
if (!list) {
return false;
}
typename std::vector<std::shared_ptr<T>>::iterator itor = list->begin();
for (; itor != list->end();) {
if (itor->get() == t.get()) {
itor = list->erase(itor);
continue;
}
++itor;
}
return true;
}
int GetProtoSize(const char *buf, size_t size);
} // namespace bridge
......
......@@ -53,22 +53,21 @@ void UDPBridgeMultiReceiverComponent::MsgDispatcher() {
listener_->Listen();
}
ProtoDiserializedBufBase
*UDPBridgeMultiReceiverComponent::CreateBridgeProtoBuf(
std::shared_ptr<ProtoDiserializedBufBase>
UDPBridgeMultiReceiverComponent::CreateBridgeProtoBuf(
const BridgeHeader &header) {
std::shared_ptr<ProtoDiserializedBufBase> proto_buf;
if (IsTimeout(header.GetTimeStamp())) {
std::vector<ProtoDiserializedBufBase *>::iterator itor
std::vector<std::shared_ptr<ProtoDiserializedBufBase>>::iterator itor
= proto_list_.begin();
for (; itor != proto_list_.end();) {
if ((*itor)->IsTheProto(header)) {
ProtoDiserializedBufBase *tmp = *itor;
FREE_POINTER(tmp);
itor = proto_list_.erase(itor);
break;
}
++itor;
}
return nullptr;
return proto_buf;
}
for (auto proto : proto_list_) {
......@@ -77,10 +76,9 @@ ProtoDiserializedBufBase
}
}
ProtoDiserializedBufBase *proto_buf =
ProtoDiserializedBufBaseFactory::CreateObj(header);
proto_buf = ProtoDiserializedBufBaseFactory::CreateObj(header);
if (!proto_buf) {
return nullptr;
return proto_buf;
}
proto_buf->Initialize(header, node_);
proto_list_.push_back(proto_buf);
......@@ -156,7 +154,8 @@ bool UDPBridgeMultiReceiverComponent::MsgHandle(int fd) {
ADEBUG << "proto frame index: " << header.GetIndex();
std::lock_guard<std::mutex> lock(mutex_);
ProtoDiserializedBufBase *proto_buf = CreateBridgeProtoBuf(header);
std::shared_ptr<ProtoDiserializedBufBase> proto_buf =
CreateBridgeProtoBuf(header);
if (!proto_buf) {
return false;
}
......@@ -178,12 +177,11 @@ bool UDPBridgeMultiReceiverComponent::RemoveInvalidBuf(uint32_t msg_id,
if (msg_id == 0) {
return false;
}
std::vector<ProtoDiserializedBufBase *>::iterator itor =
std::vector<std::shared_ptr<ProtoDiserializedBufBase>>::iterator itor =
proto_list_.begin();
for (; itor != proto_list_.end();) {
if ((*itor)->GetMsgID() < msg_id) {
ProtoDiserializedBufBase *tmp = *itor;
FREE_POINTER(tmp);
if ((*itor)->GetMsgID() < msg_id &&
strcmp((*itor)->GetMsgName().c_str(), msg_name.c_str()) == 0) {
itor = proto_list_.erase(itor);
continue;
}
......
......@@ -46,7 +46,8 @@ class UDPBridgeMultiReceiverComponent final : public cyber::Component<> {
bool Init() override;
std::string Name() const { return FLAGS_bridge_module_name; }
ProtoDiserializedBufBase *CreateBridgeProtoBuf(const BridgeHeader &header);
std::shared_ptr<ProtoDiserializedBufBase> CreateBridgeProtoBuf(
const BridgeHeader &header);
bool IsProtoExist(const BridgeHeader &header);
bool IsTimeout(double time_stamp);
void MsgDispatcher();
......@@ -63,7 +64,7 @@ class UDPBridgeMultiReceiverComponent final : public cyber::Component<> {
unsigned int bind_port_ = 0;
bool enable_timeout_ = true;
std::mutex mutex_;
std::vector<ProtoDiserializedBufBase *> proto_list_;
std::vector<std::shared_ptr<ProtoDiserializedBufBase>> proto_list_;
};
} // namespace bridge
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册