CARLA
 
载入中...
搜索中...
未找到
IniFile.h
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7// 这是一个预处理指令,用于确保头文件只被包含一次,避免重复定义等问题
8#pragma once
9
10// 包含 "ConfigCacheIni.h" 头文件,可能其中定义了与配置文件相关的类、结构体等内容,
11// 是当前类操作的基础依赖
12#include "ConfigCacheIni.h"
13// 包含标准库中关于数值极限的头文件,用于后续对数值类型范围的判断等操作
14#include <limits>
15
16// 为类 FIniFile 提供了一个 API 标识,具体取决于所在项目中 CARLA_API 的定义,
17// 通常用于表明该类是对外公开的接口类之类的用途
18class CARLA_API FIniFile : private NonCopyable
19{
20private:
21 // 定义一个模板函数,用于安全地将一种类型的值转换为另一种类型的值
22 // @param source 要转换的源值
23 // @param target 转换后存储的目标值(引用传递,用于接收转换结果)
24 template <typename TARGET, typename SOURCE>
25 static void SafeCastTo(SOURCE source, TARGET &target)
26 {
27 // 判断源值是否在目标类型所能表示的数值范围内,如果在范围内,则进行类型转换
28 if ((source >= std::numeric_limits<TARGET>::lowest()) &&
29 (source <= std::numeric_limits<TARGET>::max())) {
30 target = static_cast<TARGET>(source);
31 } else {
32 // 如果类型转换失败,输出错误日志信息到 Unreal 的日志系统中(假设 UE_LOG 是 Unreal 中的日志宏)
33 UE_LOG(LogCarla, Error, TEXT("FIniFile: Type cast failed"));
34 }
35 }
36
37public:
38 // ===========================================================================
39 /// @name Constructor 构造函数相关区域的开始标记,用于文档化代码结构,方便阅读代码时区分不同功能块
40 // ===========================================================================
41 /// @{ 开始一个代码块的文档化分组,一般配合 @} 使用来明确一个功能组的范围
42
43 // 默认构造函数,使用默认实现,通常用于创建一个初始状态的对象实例
44 FIniFile() = default;
45
46 // 显式构造函数,接受一个文件名作为参数
47 // @param FileName 要读取的配置文件的文件名(类型为 FString,应该是 Unreal 中的字符串类型)
48 explicit FIniFile(const FString &FileName)
49 {
50 // 调用 ConfigFile(FConfigFile 类型的成员变量)的 Read 函数,尝试读取指定的配置文件
51 ConfigFile.Read(FileName);
52 }
53
54 /// @} 结束构造函数相关的代码块文档化分组
55 // ===========================================================================
56 /// @name Other functions 其他功能函数相关区域的开始标记
57 // ===========================================================================
58 /// @{
59
60 // 尝试将另一个配置文件的内容合并到当前配置文件对象中
61 // @param FileName 要合并的配置文件的文件名
62 // @return 合并操作是否成功的布尔值
63 bool Combine(const FString &FileName)
64 {
65 return ConfigFile.Combine(FileName);
66 }
67
68 // 处理输入的配置文件内容字符串,将其解析并应用到当前配置文件对象中
69 // @param INIFileContents 包含配置文件内容的字符串
70 void ProcessInputFileContents(const FString &INIFileContents)
71 {
72 ConfigFile.ProcessInputFileContents(INIFileContents);
73 }
74
75 // 检查配置文件中是否存在指定的节(section)
76 // @param Section 要检查的配置文件节名称(字符串类型)
77 // @return 如果配置文件存在且能找到指定节,则返回 true,否则返回 false
78 bool HasSection(const FString &Section) const
79 {
80 return (ConfigFile.Num() > 0) && (ConfigFile.Find(Section)!= nullptr);
81 }
82
83 // 如果配置文件中不存在指定的节,则添加该节(添加一个空的配置节对象)
84 // @param Section 要添加的配置文件节名称
85 void AddSectionIfMissing(const FString &Section)
86 {
87 if (!HasSection(Section)) {
88 ConfigFile.Add(Section, FConfigSection());
89 }
90 }
91
92 // 将配置文件对象的内容写入磁盘上指定的文件中
93 // @param Filename 要写入的目标文件名
94 // @return 写入操作是否成功的布尔值
95 bool Write(const FString &Filename)
96 {
97 return ConfigFile.Write(Filename);
98 }
99
100 // 获取当前配置文件对象对应的 Unreal 的 FConfigFile 对象(可能用于更底层的操作等)
101 // @return 不可修改的(const)FConfigFile 对象引用
102 const FConfigFile &GetFConfigFile() const
103 {
104 return ConfigFile;
105 }
106
107 /// @} 结束其他功能函数相关的代码块文档化分组
108 // ===========================================================================
109 /// @name Get functions 获取配置项相关函数区域的开始标记
110 // ===========================================================================
111 /// @{
112
113 // 获取配置文件中指定节、指定键对应的整数值,并安全地转换为指定类型存储到目标变量中
114 // @param Section 配置文件节名称
115 // @param Key 配置文件键名称
116 // @param Target 用于接收获取到的整数值的目标变量(引用传递,会被修改),类型由模板参数指定
117 template <typename T>
118 void GetInt(const TCHAR* Section, const TCHAR* Key, T &Target) const
119 {
120 int64 Value;
121 // 先尝试从配置文件中获取 int64 类型的值
122 if (ConfigFile.GetInt64(Section, Key, Value)) {
123 // 如果获取成功,调用 SafeCastTo 函数安全地转换为目标类型并赋值给 Target
124 SafeCastTo<T>(Value, Target);
125 }
126 }
127
128 // 获取配置文件中指定节、指定键对应的字符串值,并赋值给目标字符串变量
129 // @param Section 配置文件节名称
130 // @param Key 配置文件键名称
131 // @param Target 用于接收获取到的字符串值的目标变量(引用传递,会被修改)
132 void GetString(const TCHAR* Section, const TCHAR* Key, FString &Target) const
133 {
134 FString Value;
135 // 尝试从配置文件中获取字符串值
136 if (ConfigFile.GetString(Section, Key, Value)) {
137 Target = Value;
138 }
139 }
140
141 // 获取配置文件中指定节、指定键对应的布尔值,并赋值给目标布尔变量
142 // @param Section 配置文件节名称
143 // @param Key 配置文件键名称
144 // @param Target 用于接收获取到的布尔值的目标变量(引用传递,会被修改)
145 void GetBool(const TCHAR* Section, const TCHAR* Key, bool &Target) const
146 {
147 bool Value;
148 // 尝试从配置文件中获取布尔值
149 if (ConfigFile.GetBool(Section, Key, Value)) {
150 Target = Value;
151 }
152 }
153
154 // 获取配置文件中指定节、指定键对应的浮点数值,可根据指定的因子进行缩放,并赋值给目标浮点变量
155 // @param Section 配置文件节名称
156 // @param Key 配置文件键名称
157 // @param Target 用于接收获取到的浮点数值的目标变量(引用传递,会被修改)
158 // @param Factor 用于缩放获取到的浮点数值的因子,默认值为 1.0f
159 void GetFloat(const TCHAR* Section, const TCHAR* Key, float &Target, const float Factor = 1.0f) const
160 {
161 FString Value;
162 // 先尝试从配置文件中获取字符串形式的数值表示
163 if (ConfigFile.GetString(Section, Key, Value)) {
164 // 将获取到的字符串转换为浮点数,并根据因子进行缩放后赋值给 Target
165 Target = Factor * FCString::Atof(*Value);
166 }
167 }
168
169 // 获取配置文件中指定节、指定键对应的线性颜色值,并赋值给目标线性颜色变量
170 // @param Section 配置文件节名称
171 // @param Key 配置文件键名称
172 // @param Target 用于接收获取到的线性颜色值的目标变量(引用传递,会被修改)
173 void GetLinearColor(const TCHAR* Section, const TCHAR* Key, FLinearColor &Target) const
174 {
175 FString Value;
176 // 尝试从配置文件中获取表示线性颜色的字符串值
177 if (ConfigFile.GetString(Section, Key, Value)) {
178 // 从字符串初始化目标线性颜色对象
179 Target.InitFromString(Value);
180 }
181 }
182
183 /// @} 结束获取配置项相关函数的代码块文档化分组
184 // ===========================================================================
185 /// @name Set functions 设置配置项相关函数区域的开始标记
186 // ===========================================================================
187 /// @{
188
189 // 在配置文件中设置指定节、指定键对应的整数值(实际存储为 int64 类型)
190 // @param Section 配置文件节名称
191 // @param Key 配置文件键名称
192 // @param Value 要设置的整数值
193 void SetInt(const TCHAR* Section, const TCHAR* Key, const int64 Value)
194 {
195 ConfigFile.SetInt64(Section, Key, Value);
196 }
197
198 // 在配置文件中设置指定节、指定键对应的字符串值
199 // @param Section 配置文件节名称
200 // @param Key 配置文件键名称
201 // @param Value 要设置的字符串值(以 TCHAR* 类型传入)
202 void SetString(const TCHAR* Section, const TCHAR* Key, const TCHAR* Value)
203 {
204 ConfigFile.SetString(Section, Key, Value);
205 }
206
207 // 在配置文件中设置指定节、指定键对应的字符串值(以 FString 类型传入)
208 // 内部会调用上面的 SetString 函数将 FString 转换为 TCHAR* 后进行设置
209 // @param Section 配置文件节名称
210 // @param Key 配置文件键名称
211 // @param Value 要设置的字符串值(FString 类型)
212 void SetString(const TCHAR* Section, const TCHAR* Key, const FString &Value)
213 {
214 SetString(Section, Key, *Value);
215 }
216
217 // 在配置文件中设置指定节、指定键对应的布尔值,实际是以字符串 "True" 或 "False" 形式存储
218 // @param Section 配置文件节名称
219 // @param Key 配置文件键名称
220 // @param Value 要设置的布尔值
221 void SetBool(const TCHAR* Section, const TCHAR* Key, const bool Value)
222 {
223 SetString(Section, Key, Value? TEXT("True") : TEXT("False"));
224 }
225
226 // 在配置文件中设置指定节、指定键对应的浮点数值,会先将浮点数转换为字符串形式再进行存储
227 // @param Section 配置文件节名称
228 // @param Key 配置文件键名称
229 // @param Value 要设置的浮点数值
230 void SetFloat(const TCHAR* Section, const TCHAR* Key, const float Value)
231 {
232 SetString(Section, Key, FText::AsNumber(Value).ToString());
233 }
234
235 // 在配置文件中设置指定节、指定键对应的线性颜色值,会先将线性颜色对象转换为字符串形式再进行存储
236 // @param Section 配置文件节名称
237 // @param Key 配置文件键名称
238 // @param Value 要设置的线性颜色值(FLinearColor 类型)
239 void SetLinearColor(const TCHAR* Section, const TCHAR* Key, const FLinearColor &Value)
240 {
241 SetString(Section, Key, Value.ToString());
242 }
243
244 /// @} 结束设置配置项相关函数的代码块文档化分组
245
246private:
247 // FConfigFile 类型的成员变量,用于实际存储和操作配置文件的内容,是整个类功能实现的核心数据对象
248 FConfigFile ConfigFile;
249};
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
void SetLinearColor(const TCHAR *Section, const TCHAR *Key, const FLinearColor &Value)
Definition IniFile.h:239
FConfigFile ConfigFile
Definition IniFile.h:248
const FConfigFile & GetFConfigFile() const
Definition IniFile.h:102
void ProcessInputFileContents(const FString &INIFileContents)
Definition IniFile.h:70
bool Combine(const FString &FileName)
Definition IniFile.h:63
bool Write(const FString &Filename)
Definition IniFile.h:95
static void SafeCastTo(SOURCE source, TARGET &target)
Definition IniFile.h:25
bool HasSection(const FString &Section) const
Definition IniFile.h:78
void SetFloat(const TCHAR *Section, const TCHAR *Key, const float Value)
Definition IniFile.h:230
FIniFile()=default
使用来明确一个功能组的范围
void GetBool(const TCHAR *Section, const TCHAR *Key, bool &Target) const
Definition IniFile.h:145
FIniFile(const FString &FileName)
Definition IniFile.h:48
void GetFloat(const TCHAR *Section, const TCHAR *Key, float &Target, const float Factor=1.0f) const
Definition IniFile.h:159
void GetString(const TCHAR *Section, const TCHAR *Key, FString &Target) const
Definition IniFile.h:132
void SetString(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value)
Definition IniFile.h:202
void SetString(const TCHAR *Section, const TCHAR *Key, const FString &Value)
Definition IniFile.h:212
void AddSectionIfMissing(const FString &Section)
Definition IniFile.h:85
void GetInt(const TCHAR *Section, const TCHAR *Key, T &Target) const
Definition IniFile.h:118
void SetInt(const TCHAR *Section, const TCHAR *Key, const int64 Value)
Definition IniFile.h:193
void SetBool(const TCHAR *Section, const TCHAR *Key, const bool Value)
Definition IniFile.h:221
void GetLinearColor(const TCHAR *Section, const TCHAR *Key, FLinearColor &Target) const
Definition IniFile.h:173