# C++工具箱 **Repository Path**: izmj/cpp-toolbox ## Basic Information - **Project Name**: C++工具箱 - **Description**: C++工具箱,保存了一些可能在开发中便于进行复用的类。 - **Primary Language**: C++ - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-06-13 - **Last Updated**: 2025-06-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # C++ 工具箱 (cpp-toolbox) 这个项目包含了一系列用于C++开发的实用工具类和组件,旨在简化常见任务并提高代码质量。 ## 功能 ### 线程安全 (thread-safe) #### 数据缓存池 (DataCachePool) `DataCachePool` 是一个线程安全的键值对缓存实现,支持多线程环境下的数据安全共享。主要特点: - 线程安全的读写操作 - 支持多读单写模式(使用`std::shared_mutex`) - 提供直观的API进行数据操作 - 支持移动语义优化性能 ##### 使用示例 ```cpp #include "thread-safe/data_cache_pool.h" #include #include #include using namespace cpp_toolbox::thread_safe; int main() { // 创建一个字符串到整数的缓存池 DataCachePool cache; // 写入数据 cache.Set("one", 1); cache.Set("two", 2); // 读取数据(方式1:通过引用获取) int value; if (cache.Get("one", value)) { std::cout << "one = " << value << std::endl; } // 读取数据(方式2:使用optional) auto result = cache.Get("two"); if (result) { std::cout << "two = " << *result << std::endl; } // 检查键是否存在 if (cache.Contains("three")) { std::cout << "three exists" << std::endl; } else { std::cout << "three does not exist" << std::endl; } // 删除数据 cache.Remove("one"); // 获取缓存大小 std::cout << "Cache size: " << cache.Size() << std::endl; // 清空缓存 cache.Clear(); return 0; } ``` ## 构建和测试 ### 前提条件 - C++17 兼容的编译器 - CMake 3.10 或更高版本 ### 构建步骤 ```bash # 克隆仓库 git clone https://github.com/yourusername/cpp-toolbox.git cd cpp-toolbox # 创建构建目录 mkdir build && cd build # 配置(如果使用clang++编译器) cmake -DCMAKE_CXX_COMPILER=clang++ .. # 构建 cmake --build . # 运行测试 ctest ``` ### 测试结果示例 ``` 开始测试线程安全的数据缓存池... 测试基本功能... 基本功能测试通过! 测试多线程读写... 多线程测试结果: 写入操作: 4624 读取成功: 9386 读取失败: 4995 缓存大小: 3540 多线程测试通过! 测试移动语义... 移动语义测试通过! 所有测试通过! ``` ## 许可证 本项目采用 MIT 许可证。详情请参阅 [LICENSE](LICENSE) 文件。