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