ConfigInfo::Ptr config
时间: 2023-09-30 12:02:24 浏览: 29
这是一个指向 ConfigInfo 对象的智能指针,使用了 C++ 的智能指针技术,可以自动管理资源的释放。具体而言,它是一个 std::shared_ptr<ConfigInfo> 类型的对象,可以在多个地方引用同一个 ConfigInfo 对象,并且在所有引用都被释放后,ConfigInfo 对象也会被自动释放。这种技术可以避免内存泄漏和野指针的问题。
相关问题
class Config { public: using Ptr_=std::shared_ptr<Config>; private: Config() = default; Config(Config &&) = delete; Config(const Config &)= delete; Config &operator=(Config &&)= delete; Config &operator=(const Config &)= delete; static Ptr_ config_info_; std::map<std::string, std::string> storage_; public: ~Config() = default; public: static Ptr_ GetInstance(); bool Open(std::string config_file); template <typename T> T Get(std::string key){ transform(key.begin(),key.end(),key.begin(),::tolower); // 转小写 if(storage_.count(key)>0){ try{ double value=stod(storage_[key]); return static_cast<T>(value); } catch (const std::exception &e){ std::cerr<<e.what()<<'\n'; } } else{ // LOG(ERROR)<<"The key of "<<key<<" does not exist"; // getchar(); return T(0x0); } } template <typename T> std::vector<T> GetArray(std::string key){ std::vector<T> data; transform(key.begin(),key.end(),key.begin(),::tolower); // 转小写 if(storage_.count(key)>0){ try{ auto text=TextSplit(storage_[key],","); for(auto index:text){ double value=stod(index); data.emplace_back(static_cast<T>(value)); } } catch(const std::exception &e){ std::cerr<<e.what()<<'\n'; } } else{ // LOG(ERROR)<<"The key of "<<key<<" does not exist"; // getchar(); } return data; } };
这段代码定义了一个名为`Config`的类。该类具有以下特点:
- `Ptr_`是一个`std::shared_ptr<Config>`类型的别名,用于方便管理`Config`类的实例。
- 构造函数`Config()`使用`default`关键字进行默认定义。
- 移动构造函数`Config(Config &&)`和拷贝构造函数`Config(const Config &)`被禁用,使用`= delete`来实现。
- 移动赋值运算符`operator=(Config &&)`和拷贝赋值运算符`operator=(const Config &)`也被禁用,使用`= delete`来实现。
- 静态成员变量`config_info_`是一个指向`Config`类实例的共享指针。
- `storage_`是一个存储键值对的映射,用于存储配置信息。
- 析构函数`~Config()`使用`default`关键字进行默认定义。
- 公有静态成员函数`GetInstance()`用于获取`Config`类的实例。
- 公有成员函数`Open(std::string config_file)`用于打开配置文件并读取配置信息。
- 成员函数模板`Get<T>(std::string key)`用于根据给定的键获取配置值,并以指定类型返回。
- 成员函数模板`GetArray<T>(std::string key)`用于根据给定的键获取配置值,并以指定类型的向量返回。
代码中还包含了一些注释和错误处理逻辑,但是这些部分被注释掉了,可能是为了调试或其他目的。
帮我修改下面的代码。要求建立一次weboscket链接,链接到wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840,当订阅到感知话题调用perceptionCallback时,通过wss发送serialized_data:#include "ros/ros.h" #include "std_msgs/String.h" #include <boost/thread/locks.hpp> #include <boost/thread/shared_mutex.hpp> #include "third_party/apollo/proto/perception/perception_obstacle.pb.h" #include "t3_perception.pb.h" #include <iostream> #include <websocketpp/config/asio_client.hpp> #include <websocketpp/client.hpp> #include <websocketpp/common/thread.hpp> apollo::perception::PerceptionObstacles perception_obstacles_; typedef websocketpp::clientwebsocketpp::config::asio_tls_client client; void perceptionCallback(const std_msgs::String& msg) { ROS_WARN("t3 perceptionCallback parse"); if (perception_obstacles_.ParseFromString(msg.data)) { double timestamp = perception_obstacles_.header().timestamp_sec(); ROS_INFO("t3 perceptionCallback timestamp %f count:%d", timestamp, perception_obstacles_.perception_obstacle().size()); std::string data; perception_obstacles_.SerializeToString(&data); VehData veh_data; veh_data.set_messagetype(5); veh_data.set_messagedes("PerceptionObstacles"); veh_data.set_contents(data); std::string serialized_data; veh_data.SerializeToString(&serialized_data); } else { ROS_ERROR("t3 perceptionCallback parse fail!"); } } int main(int argc, char **argv) { ros::init(argc, argv, "listener"); ros::NodeHandle n; ros::Subscriber sub = n.subscribe("/perception_node/perception_objects", 1000, perceptionCallback); ros::spin(); return 0; }
好的,这里是修改后的代码:
```c++
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
#include "third_party/apollo/proto/perception/perception_obstacle.pb.h"
#include "t3_perception.pb.h"
#include <iostream>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
using namespace std;
apollo::perception::PerceptionObstacles perception_obstacles_;
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
void perceptionCallback(const std_msgs::String& msg, client* c) {
ROS_WARN("t3 perceptionCallback parse");
if (perception_obstacles_.ParseFromString(msg.data)) {
double timestamp = perception_obstacles_.header().timestamp_sec();
ROS_INFO("t3 perceptionCallback timestamp %f count:%d", timestamp, perception_obstacles_.perception_obstacle().size());
std::string data;
perception_obstacles_.SerializeToString(&data);
VehData veh_data;
veh_data.set_messagetype(5);
veh_data.set_messagedes("PerceptionObstacles");
veh_data.set_contents(data);
std::string serialized_data;
veh_data.SerializeToString(&serialized_data);
try {
c->send(serialized_data);
} catch (const websocketpp::exception& e) {
ROS_ERROR("t3 perceptionCallback send failed: %s", e.what());
}
} else {
ROS_ERROR("t3 perceptionCallback parse fail!");
}
}
int main(int argc, char **argv) {
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/perception_node/perception_objects", 1000,
boost::bind(&perceptionCallback, _1, new client()));
client* c = sub.getCallbackQueue()->getCallbackThread().get_context<client>();
c->set_access_channels(websocketpp::log::alevel::none);
c->set_error_channels(websocketpp::log::elevel::none);
websocketpp::lib::error_code ec;
client::connection_ptr con = c->get_connection("wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840", ec);
if (ec) {
ROS_ERROR("t3 websocket connection failed: %s", ec.message().c_str());
return 1;
}
c->connect(con);
ROS_INFO("t3 websocket connected");
ros::spin();
return 0;
}
```
主要的修改包括:
1. 将 `websocketpp::clientwebsocketpp::config::asio_tls_client` 改为 `websocketpp::client<websocketpp::config::asio_tls_client>`,因为 `websocketpp` 3.0 版本后改变了类模板的定义。
2. 修改 `perceptionCallback` 函数,将 `serialized_data` 发送到 websocket 服务器。由于该函数是回调函数,需要将 client 指针作为参数传递进去,这里使用 `boost::bind` 来实现。
3. 在 `main` 函数中建立 websocket 连接,并将 client 指针传递给 `perceptionCallback` 函数。注意要将 client 的日志级别设置为 none,以避免输出过多的日志。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)