• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# C++公共基础库<a name="ZH-CN_TOPIC_0000001148676553"></a>
2
3
4## 简介<a name="section11660541593"></a>
5
6C++公共基础类库为标准系统提供了一些常用的C++开发工具类,包括:
7
8-   文件、路径、字符串相关操作的能力增强接口
9-   读写锁、信号量、定时器、线程增强及线程池等接口
10-   安全数据容器、数据序列化等接口
11-   各子系统的错误码相关定义
12
13## 目录<a name="section17271017133915"></a>
14
15```
16commonlibrary/c_utils
17└─ base
18    ├── include       # 对各子系统开放的接口头文件
19    ├── src           # 源文件
20    └── test          # 测试代码
21```
22
23## 约束
24
25适用于标准系统。
26## 编译构建
27### 编译部件
28```
29./build.sh --product-name rk3568 --build-target c_utils
30```
31
32### 编译动态库
33```
34./build.sh --product-name rk3568 --build-target commonlibrary/c_utils/base:utils
35```
36
37### 编译静态库
38```
39./build.sh --product-name rk3568 --build-target commonlibrary/c_utils/base:utilsbase
40```
41## 使用说明
42### ashmem
43```
44sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
45if (ashmem != nullptr) {
46    bool ret = ashmem->MapAshmem(PROT_READ | PROT_WRITE);
47}
48
49...
50
51// 当使用结束时不要忘记解映射和关闭ashmem
52ashmem->UnmapAshmem();
53ashmem->CloseAshmem();
54```
55
56### parcel
57```
58// 写入端以某种顺序写入数据
59struct TestData {
60    bool booltest;
61    int8_t int8test;
62    int16_t int16test;
63    int32_t int32test;
64    uint8_t uint8test;
65    uint16_t uint16test;
66    uint32_t uint32test;
67};
68
69...
70
71Parcel parcel(nullptr);
72struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
73bool result = false;
74
75result = parcel.WriteBool(data.booltest);
76if (!result) {
77    // 写失败处理
78}
79
80result = parcel.WriteInt8(data.int8test);
81if (!result) {
82    // 写失败处理
83}
84
85result = parcel.WriteInt16(data.int16test);
86if (!result) {
87    // 写失败处理
88}
89
90result = parcel.WriteInt32(data.int32test);
91if (!result) {
92    // 写失败处理
93}
94
95result = parcel.WriteUint8(data.uint8test);
96if (!result) {
97    // 写失败处理
98}
99
100result = parcel.WriteUint16(data.uint16test);
101if (!result) {
102    // 写失败处理
103}
104
105result = parcel.WriteUint32(data.uint32test);
106if (!result) {
107    // 写失败处理
108}
109```
110```
111// 接收端根据写入端写入顺序读取数据
112bool readbool = parcel.ReadBool();
113
114int8_t readint8 = parcel.ReadInt8();
115
116int16_t readint16 = parcel.ReadInt16();
117
118int32_t readint32 = parcel.ReadInt32();
119
120uint8_t readuint8 = parcel.ReadUint8();
121
122uint16_t readuint16 = parcel.ReadUint16();
123
124uint32_t readuint32 = parcel.ReadUint32();
125```
126### refbase
127```
128class TestRefBase : public RefBase {
129...
130};
131...
132sptr<TestRefBase> test(new TestRefBase());
133...
134```
135### timer
136```
137void TimeOutCallback()
138{
139    ...
140}
141...
142Utils::Timer timer("test_timer");
143uint32_t ret = timer.Setup();
144timer.Register(TimeOutCallback, 1, true);
145std::this_thread::sleep_for(std::chrono::milliseconds(15));
146timer.Shutdown();
147```
148
149## Changelog
150**2022/10/10**
1511. 路径变更。由utils/native移动至commonlibrary/c_utils1522. 部件名变更。由utils_base变更为c_utils;
1533. 不再提供安全C库能力。请使用[third_party_bounds_checking_function](https://gitee.com/openharmony/third_party_bounds_checking_function)154## 相关仓<a name="section1249817110914"></a>
155
156**[commonlibrary\_c\_utils](https://gitee.com/openharmony/commonlibrary_c_utils)**
157
158[commonlibrary\_utils\_lite](https://gitee.com/openharmony/commonlibrary_utils_lite)
159
160