• Home
Name Date Size #Lines LOC

..--

base/12-May-2024-19,10313,156

.gitattributesD12-May-2024631 1615

.gitignoreD12-May-20246 21

LICENSED12-May-20249.9 KiB177150

README.mdD12-May-20243.7 KiB158127

README_zh.mdD12-May-20243.5 KiB160127

bundle.jsonD12-May-20242.8 KiB108107

README.md

1# commonlibrary/c_utils<a name="EN-US_TOPIC_0000001148676553"></a>
2
3## Introduction<a name="section11660541593"></a>
4
5The  **commonlibrary/c_utils**  repository provides the following commonly used C++ utility classes for standard system:
6
7-   Enhanced APIs for operations related to files, paths, and strings
8-   APIs related to the read-write lock, semaphore, timer, thread, and thread pool
9-   APIs related to the security data container and data serialization
10-   Error codes for each subsystem
11
12## Directory Structure<a name="section17271017133915"></a>
13
14```
15commonlibrary/c_utils
16└─ base
17    ├── include       # Header files of APIs open to other subsystems
18    ├── src           # Source files
19    └── test          # Test code
20```
21## Condition
22Suitable for standard system.
23
24## Build
25### Build Component
26```
27./build.sh --product-name rk3568 --build-target c_utils
28```
29
30### Build Shared Library
31```
32./build.sh --product-name rk3568 --build-target commonlibrary/c_utils/base:utils
33```
34
35### Build Static Library
36```
37./build.sh --product-name rk3568 --build-target commonlibrary/c_utils/base:utilsbase
38```
39## Coding Directions
40### ashmem
41```
42sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
43if (ashmem != nullptr) {
44    bool ret = ashmem->MapAshmem(PROT_READ | PROT_WRITE);
45}
46
47...
48
49// Do not forget to unmap & close ashmem at the end
50ashmem->UnmapAshmem();
51ashmem->CloseAshmem();
52```
53
54### parcel
55```
56// Write data into parcel in some order at writing port
57struct TestData {
58    bool booltest;
59    int8_t int8test;
60    int16_t int16test;
61    int32_t int32test;
62    uint8_t uint8test;
63    uint16_t uint16test;
64    uint32_t uint32test;
65};
66
67...
68
69Parcel parcel(nullptr);
70struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
71bool result = false;
72
73result = parcel.WriteBool(data.booltest);
74if (!result) {
75    // Deal with writing failure
76}
77
78result = parcel.WriteInt8(data.int8test);
79if (!result) {
80    // Deal with writing failure
81}
82
83result = parcel.WriteInt16(data.int16test);
84if (!result) {
85    // Deal with writing failure
86}
87
88result = parcel.WriteInt32(data.int32test);
89if (!result) {
90    // Deal with writing failure
91}
92
93result = parcel.WriteUint8(data.uint8test);
94if (!result) {
95    // Deal with writing failure
96}
97
98result = parcel.WriteUint16(data.uint16test);
99if (!result) {
100    // Deal with writing failure
101}
102
103result = parcel.WriteUint32(data.uint32test);
104if (!result) {
105    // Deal with writing failure
106}
107```
108```
109// Read data with the order writing in at reading port
110bool readbool = parcel.ReadBool();
111
112int8_t readint8 = parcel.ReadInt8();
113
114int16_t readint16 = parcel.ReadInt16();
115
116int32_t readint32 = parcel.ReadInt32();
117
118uint8_t readuint8 = parcel.ReadUint8();
119
120uint16_t readuint16 = parcel.ReadUint16();
121
122uint32_t readuint32 = parcel.ReadUint32();
123```
124### refbase
125```
126class TestRefBase : public RefBase {
127...
128};
129...
130sptr<TestRefBase> test(new TestRefBase());
131...
132```
133### timer
134```
135void TimeOutCallback()
136{
137    ...
138}
139...
140Utils::Timer timer("test_timer");
141uint32_t ret = timer.Setup();
142timer.Register(TimeOutCallback, 1, true);
143std::this_thread::sleep_for(std::chrono::milliseconds(15));
144timer.Shutdown();
145```
146
147## Changelog
148**2022/10/10**
1491. Move this repository from utils/native to commonlibrary/c_utils.
1502. Switch component name from utils_base to c_utils.
1513. Securec is not in this repository any more. Please use [third_party_bounds_checking_function](https://gitee.com/openharmony/third_party_bounds_checking_function).
152## Repositories Involved<a name="section1249817110914"></a>
153
154**[commonlibrary\_c\_utils](https://gitee.com/openharmony/commonlibrary_c_utils)**
155
156[commonlibrary\_utils\_lite](https://gitee.com/openharmony/commonlibrary_utils_lite)
157
158

README_zh.md

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