CARLA
 
载入中...
搜索中...
未找到
CarlaRecorderEventAdd.cpp
浏览该文件的文档.
1// 版权所有 (c) 2017 巴塞罗那自治大学 (UAB) 计算机视觉中心 (CVC)。
2//
3// 本作品根据 MIT 许可证的条款进行许可。
4// 有关副本,请参阅 <https://opensource.org/licenses/MIT>。
5
6#include "CarlaRecorder.h"
9
10// CarlaRecorderEventAdd类的Write函数,用于将该类相关的数据写入到输出流(OutFile)中
11// 通常用于将对象的状态信息持久化存储等操作,比如记录到文件中
12void CarlaRecorderEventAdd::Write(std::ostream &OutFile) const
13{
14 // 将数据库的唯一标识符(DatabaseId)写入输出流,WriteValue应该是一个自定义的用于按特定格式写入数据的函数,这里写入的是32位无符号整数类型的数据
15 WriteValue<uint32_t>(OutFile, this->DatabaseId);
16 // 将类型(Type)信息写入输出流,此处写入的是8位无符号整数类型的数据
17 WriteValue<uint8_t>(OutFile, this->Type);
18
19 // 写入位置信息(Location)到输出流,WriteFVector应该是用于写入向量类型数据的自定义函数,用于记录对象在空间中的位置
20 WriteFVector(OutFile, this->Location);
21 // 写入旋转信息(Rotation)到输出流,同样使用WriteFVector函数,用于记录对象的旋转状态
22 WriteFVector(OutFile, this->Rotation);
23
24 // 写入描述类型相关的唯一标识符(UId)到输出流,它是32位无符号整数类型
25 WriteValue<uint32_t>(OutFile, this->Description.UId);
26 // 写入描述类型的具体标识(Id)到输出流,WriteFString应该是用于写入字符串类型数据的自定义函数
27 WriteFString(OutFile, this->Description.Id);
28
29 // 开始处理属性(Attributes)相关的数据写入
30 // 获取属性的总数量
31 uint16_t Total = this->Description.Attributes.size();
32 // 将属性的总数量写入输出流,同样使用WriteValue函数,这里写入的是16位无符号整数类型的数据
33 WriteValue<uint16_t>(OutFile, Total);
34 // 遍历所有的属性
35 for (uint16_t i = 0; i < Total; ++i)
36 {
37 // 写入属性的类型(Type)信息到输出流,8位无符号整数类型
38 WriteValue<uint8_t>(OutFile, this->Description.Attributes[i].Type);
39 // 写入属性的标识(Id)字符串到输出流
40 WriteFString(OutFile, this->Description.Attributes[i].Id);
41 // 写入属性的值(Value)字符串到输出流
42 WriteFString(OutFile, this->Description.Attributes[i].Value);
43 }
44}
45
46// CarlaRecorderEventAdd类的Read函数,用于从输入流(InFile)中读取数据并恢复该类对象的状态
47// 通常用于从文件等存储介质中读取之前保存的对象信息来重建对象
48void CarlaRecorderEventAdd::Read(std::istream &InFile)
49{
50 // 从输入流中读取数据库的唯一标识符(DatabaseId)并赋值给当前对象的对应成员变量,ReadValue是自定义的按特定格式读取数据的函数
51 ReadValue<uint32_t>(InFile, this->DatabaseId);
52
53 // 从输入流中读取数据库的类型(Type)信息并赋值给当前对象的对应成员变量
54 ReadValue<uint8_t>(InFile, this->Type);
55
56 // 从输入流中读取位置信息(Location)并赋值给当前对象的对应成员变量,ReadFVector是用于读取向量类型数据的自定义函数
57 ReadFVector(InFile, this->Location);
58 // 从输入流中读取旋转信息(Rotation)并赋值给当前对象的对应成员变量
59 ReadFVector(InFile, this->Rotation);
60
61 // 从输入流中读取描述类型相关的唯一标识符(UId)并赋值给当前对象的对应成员变量
62 ReadValue<uint32_t>(InFile, this->Description.UId);
63 // 从输入流中读取描述类型的具体标识(Id)并赋值给当前对象的对应成员变量,ReadFString用于读取字符串类型数据
64 ReadFString(InFile, this->Description.Id);
65
66 // 开始处理属性(Attributes)相关的数据读取
67 uint16_t Total;
68 // 从输入流中读取属性的总数量
69 ReadValue<uint16_t>(InFile, Total);
70 // 先清空当前对象的属性容器,准备重新填充数据
71 this->Description.Attributes.clear();
72 // 预留足够的空间来存储即将读取的属性数据,避免后续频繁的内存分配操作,提高效率
73 this->Description.Attributes.reserve(Total);
74 // 遍历所有要读取的属性
75 for (uint16_t i = 0; i < Total; ++i)
76 {
77 // 创建一个临时的属性对象(Att),用于存储从输入流中读取的单个属性信息
79 // 从输入流中读取属性的类型(Type)信息并赋值给临时属性对象的对应成员变量
80 ReadValue<uint8_t>(InFile, Att.Type);
81 // 从输入流中读取属性的标识(Id)字符串并赋值给临时属性对象的对应成员变量
82 ReadFString(InFile, Att.Id);
83 // 从输入流中读取属性的值(Value)字符串并赋值给临时属性对象的对应成员变量
84 ReadFString(InFile, Att.Value);
85 // 将填充好的临时属性对象添加到当前对象的属性容器中
86 this->Description.Attributes.push_back(std::move(Att));
87 }
88}
89
90//---------------------------------------------
91
92// CarlaRecorderEventsAdd类的Clear函数,用于清空存储的事件列表(Events)
93// 一般用于重置对象的状态,释放相关内存等操作
95{
96 Events.clear();
97}
98
99// CarlaRecorderEventsAdd类的Add函数,用于向事件列表(Events)中添加一个CarlaRecorderEventAdd类型的事件
100// 通过移动语义(std::move)来高效地传递参数,避免不必要的拷贝操作
102{
103 Events.push_back(std::move(Event));
104}
105
106// CarlaRecorderEventsAdd类的Write函数,用于将该类所管理的所有事件信息写入到输出流(OutFile)中
107// 可能用于将一系列事件数据保存到文件等操作,包含了一些计算和写入包大小等逻辑
108void CarlaRecorderEventsAdd::Write(std::ostream &OutFile)
109{
110 // 写入数据包的标识符(CarlaRecorderPacketId::EventAdd)到输出流,这里将其转换为字符类型(char)后写入,用于标识数据包的类型
111 WriteValue<char>(OutFile, static_cast<char>(CarlaRecorderPacketId::EventAdd));
112
113 // 获取当前输出流的写入位置,用于后续计算数据包大小等操作,PosStart记录了开始写入数据包主体内容前的位置
114 std::streampos PosStart = OutFile.tellp();
115
116 // 先写入一个虚拟的数据包大小(初始设为0),后续会重新计算并更新这个值,这里先占位,方便后续定位和修改
117 uint32_t Total = 0;
118 WriteValue<uint32_t>(OutFile, Total);
119
120 // 写入事件的总数量到输出流,这里的Total获取了存储在Events容器中的事件个数,并转换为16位无符号整数类型写入
121 Total = Events.size();
122 WriteValue<uint16_t>(OutFile, Total);
123
124 // 遍历所有的事件,调用每个事件对象自身的Write函数将其详细信息写入输出流
125 for (uint16_t i = 0; i < Total; ++i)
126 Events[i].Write(OutFile);
127
128 // 获取当前输出流的写入位置,PosEnd记录了写完所有事件数据后的位置,通过它与PosStart的差值来计算实际的数据包大小(减去之前写入的虚拟大小占位的4个字节,即sizeof(uint32_t))
129 std::streampos PosEnd = OutFile.tellp();
130 Total = PosEnd - PosStart - sizeof(uint32_t);
131 // 将输出流的写入位置移动回之前记录的PosStart位置,以便更新之前写入的虚拟数据包大小的值
132 OutFile.seekp(PosStart, std::ios::beg);
133 // 写入实际计算得到的数据包大小
134 WriteValue<uint32_t>(OutFile, Total);
135 // 再将输出流的写入位置移动回PosEnd,以便后续可以继续写入其他可能的数据(如果有的话)
136 OutFile.seekp(PosEnd, std::ios::beg);
137}
138
139// CarlaRecorderEventsAdd类的Read函数,用于从输入流(InFile)中读取事件数据并添加到当前对象管理的事件列表(Events)中
140// 通过循环读取每个事件的数据,调用相应的Read函数来恢复事件对象,并添加到列表中
141void CarlaRecorderEventsAdd::Read(std::istream &InFile)
142{
144 uint16_t i, Total;
145 // 从输入流中读取事件的总数量
146 ReadValue<uint16_t>(InFile, Total);
147 // 遍历所有要读取的事件
148 for (i = 0; i < Total; ++i)
149 {
150 // 调用单个事件对象(EventAdd)的Read函数从输入流中读取该事件的数据并恢复其状态
151 EventAdd.Read(InFile);
152 // 将恢复好的事件对象添加到当前对象管理的事件列表(Events)中
153 Add(EventAdd);
154 }
155}
156
157// CarlaRecorderEventsAdd类的GetEvents函数,用于获取当前对象所管理的事件列表(Events)的引用
158// 外部代码可以通过这个函数来访问和操作这些事件数据,但不能通过返回的引用去改变存储事件的容器本身(比如重新赋值整个容器等操作是不行的)
159const std::vector<CarlaRecorderEventAdd>& CarlaRecorderEventsAdd::GetEvents()
160{
161 return Events;
162}
void WriteFString(std::ostream &OutFile, const FString &InObj)
void ReadFString(std::istream &InFile, FString &OutObj)
void WriteFVector(std::ostream &OutFile, const FVector &InObj)
void ReadFVector(std::istream &InFile, FVector &OutObj)
std::vector< CarlaRecorderEventAdd > Events
void Add(const CarlaRecorderEventAdd &Event)
void Read(std::istream &InFile)
void Write(std::ostream &OutFile)
const std::vector< CarlaRecorderEventAdd > & GetEvents()
std::vector< CarlaRecorderActorAttribute > Attributes
void Write(std::ostream &OutFile) const
void Read(std::istream &InFile)
CarlaRecorderActorDescription Description