DECLARE_SERIAL(class_name)

本文详细介绍了MFC框架中DECLARE_SERIAL宏的作用,包括如何为CObject的派生类生成串行化所需的C++代码,以及如何在运行时获取类的信息。此外,还解释了DECLARE_DYNAMIC宏的用途,即为CObject派生类提供运行时类型识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DECLARE_SERIAL(class_name)

DECLARE_SERIAL(class_name)

参数: class_name 类的实际名字(不用引号括起来)。
说明:
DECLARE_SERIAL为可以串行化的CObject的派生类生成了必要的C++代码
串行化是指将对象的内容写入文件或从文件读入对象的内容的过程
在.H模块中使用DECLARE_SERIAL宏,然后在所有需要访问这个类的对象的.CPP模块中包含这个模块。
如果在类声明中包含了DECLARE_SERIAL宏,那么必须在类实现中包含IMPLEMENT_SERIAL宏。
DECLARE_SERIAL宏包含了DECLARE_DYNAMIC和DECLARE_DYNCREATE的所有功能
你可以用AFX_API宏为那些使用了DECLARE_SERIAL和IMPLEMENT_SERIAL宏的类自动引出CArchive提取操作符。用下面的代码将类声明(在.H文件中)括起来:

#undef AFX_API
#define AFX_API AFX_EXT_CLASS

<这里是你的类声明>

#undef AFX_API
#define AFX_API

关于DECLARE_SERIAL宏的更多信息参见“Visual C++程序员指南”中的“CObject类主题”。

DECLARE_DYNAMIC

DECLARE_DYNAMIC( class_name )

参数: class_name 类的实际名字(不用引号括起来)。
说明:
当你从CObject继承一个类的时候,这个宏加入了访问类的运行时信息的能力
在类的头文件(.H)中加入DECLARE_DYNAMIC宏,然后在所有需要访问这个类的对象的.CPP模块中包含这个头文件。
如果你按照前面描述的方式使用了DECLARE_DYNAMIC和IMPLEMENT_DYNAMIC宏,你就可以在运行时利用RUNTIME_CLASS宏和CObject::IsKindOf函数来确定对象所属的类
如果在类声明中包含了DECLARE_DYNAMIC,那么必须在类的实现中包含IMPLEMENT_DYNAMIC宏。
有关DECLARE_DYNAMIC宏的更多信息参见“Visual C++程序员指南”中的“CObject类主题”。
IMPLEMENT_DYNAMIC
IMPLEMENT_DYNAMIC( class_name, base_class_name )
参数: class_name 类的实际名字(不用引号括起来)。 base_class_name 基类的名字(不要引号括起来)。
说明:
这个宏为动态的CObject派生类生成了必要的C++代码,使其能够在运行时访问类的名字及其在继承结构中的位置。在.CPP模块中使用IMPLEMENT_DYNAMIC宏,然后一次性地连接生成的目标代码。
有关的更多信息参见“Visual C++程序员指南”中的“CObject类”主题。

``` #ifndef ROBOT_SERIAL_H #define ROBOT_SERIAL_H #include <rclcpp/rclcpp.hpp> #include <geometry_msgs/msg/twist.hpp> #include "robot_msgs/msg/serial_full_key.hpp" #include "robot_msgs/msg/serial_segment_key.hpp" #include "msg_serialize.h" #include "serialPro.h" namespace my_serial { message_data Head { uint64_t SOF =0xAA; //头校验 uint64_t length = 0; //长度 uint64_t cmd_id = 0x00; //命令字 }; message_data password_send_t { uint64_t password1; //密码片段 uint64_t password2; }; message_data password_receive_t { int64_t password; //密码 }; message_data Tail { uint64_t crc16 =0xBB; //尾校验 }; class MySerial : public sp::serialPro<Head,Tail> { public: MySerial() = default; MySerial(std::string s, int band): sp::serialPro<Head,Tail>(s,band){ registerChecker([](const Head& head)-> int { return head.SOF != 0xAA; }); //头校验 registerChecker([](const Tail& tail, const uint8_t*, const int&)-> int { return tail.crc16 != 0xBB; }); setGetId([](const Head& head)-> int { return head.cmd_id;}); //返回命令字 setGetLength([](const Head& head)-> int{ return (int)head.length; }); //返回长度 } }; } class RobotSerial : public rclcpp::Node { private: my_serial::MySerial serial; rclcpp::Clock rosClock; rclcpp::Publisher<robot_msgs::msg::SerialFullKey>::SharedPtr FullKeyPublisher; rclcpp::Subscription<robot_msgs::msg::SerialSegmentKey>::SharedPtr SegmentKeySubscriber; void SegmentKeyCallback(const robot_msgs::msg::SerialSegmentKey::SharedPtr msg){ my_serial::Head head; my_serial::Tail tail; my_serial::password_send_t password_send{ (uint64_t)(msg->segment_key_1), (uint64_t)(msg->segment_key_2), }; head.length=sizeof(my_serial::password_send_t); head.cmd_id=0; serial.write(head,password_send,tail); RCLCPP_INFO(this->get_logger(),"serial write:%lu,%lu",msg->segment_key_1,msg->segment_key_2); } public: explicit RobotSerial() : Node("robot_serial_node"){ declare_parameter("serial_name", "/dev/pts/3"); serial =std::move(my_serial::MySerial(get_parameter("serial_name").as_string(),115200)); RCLCPP_INFO(this->get_logger(),"robot_serial init success"); serial.registerCallback(1,[this](const my_serial::password_receive_t& msg){ robot_msgs::msg::SerialFullKey _fullKey; _fullKey.full_key=msg.password; FullKeyPublisher->publish(_fullKey); }); FullKeyPublisher = create_publisher<robot_msgs::msg::SerialFullKey>("/serial/full_key_",1); SegmentKeySubscriber = create_subscription<robot_msgs::msg::SerialSegmentKey>("/serial/segment_key_",1, std::bind(&RobotSerial::SegmentKeyCallback,this,std::placeholders::_1)); serial.spin(true); } }; #endif //RDBOT_SERIAL_H```解释
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值