Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Enterprise C++ Framework

基于 C++20 的企业级应用程序框架,提供日志、配置管理、异步服务和 JSON 处理等核心功能模块。

功能特性

  • Logger - 基于 Boost.Log 封装的线程安全单例,支持 fmt 风格格式化
  • Config - JSON 配置管理,支持类型安全的存取和文件 I/O
  • Service - 基于 Boost.Asio 的线程池任务队列
  • JSON Utils - JSON 文件读写、路径遍历、深度合并和 CSV 转换
  • INI Utils - INI 配置文件读写,支持节/键值对管理、类型安全取值

系统要求

  • C++20 兼容编译器(GCC 11+、Clang 13+、AppleClang 15+、MSVC 19.29+)
  • CMake 3.20+
  • Boost 1.81+(thread、log、log_setup、filesystem、headers)
  • fmt 10+
  • nlohmann-json 3+

macOS 13+ 支持

本项目已适配 macOS 13 Ventura 及以上版本,支持 Apple Silicon (arm64) 和 Intel (x86_64) 平台。

# macOS 安装依赖
brew install boost fmt nlohmann-json

快速开始

构建项目

# 使用构建脚本
chmod +x scripts/*.sh
./scripts/build.sh

# 或者手动构建
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=ON
cmake --build .

运行程序

./build/src/enterprise_app

构建选项

选项 默认值 说明
ENABLE_TESTS ON 构建单元测试
ENABLE_CLANG_TIDY OFF 启用 clang-tidy 静态分析
ENABLE_COVERAGE OFF 启用代码覆盖率

构建脚本用法

./scripts/build.sh              # Release 构建(包含测试)
./scripts/build.sh -d           # Debug 构建
./scripts/build.sh --clang-tidy # 启用静态分析
./scripts/build.sh --coverage   # Debug 构建 + 代码覆盖率
./scripts/build.sh -c           # 清理后重新构建
./scripts/test.sh               # 运行所有测试
./scripts/test.sh -f "[config]" # 按标签过滤测试
./scripts/clean.sh              # 清理构建产物

使用示例

Logger - 日志模块

#include "enterprise/logger.hpp"

// 初始化日志系统(设置最低输出级别)
enterprise::Logger::instance().init(boost::log::trivial::info);

LOG_INFO("应用程序已启动");
LOG_DEBUG("调试值: {}", 42);
LOG_WARNING("磁盘空间不足: {}%", 10);
LOG_ERROR("连接失败: {}", "db.example.com");

输出示例:

[2026-05-06 12:00:00] [info] 应用程序已启动
[2026-05-06 12:00:00] [warning] 磁盘空间不足: 10%
[2026-05-06 12:00:00] [error] 连接失败: db.example.com

Config - 配置管理

#include "enterprise/config.hpp"

auto& config = enterprise::Config::instance();

// 从文件加载
config.load_from_file("config.json");

// 或从字符串加载
config.load_from_string(R"({
    "app_name": "MyApp",
    "port": 8080,
    "debug": true
})");

// 类型安全的取值(带默认值回退)
auto port = config.get_or<int>("port", 8080);
auto host = config.get_or<std::string>("host", "localhost");
auto debug = config.get_or<bool>("debug", false);

Service - 线程池

#include "enterprise/service.hpp"

// 创建 4 个工作线程的线程池
enterprise::Service service(4);
service.start();

// 提交任务
for (int i = 0; i < 10; ++i) {
    service.submit([i]() {
        // 在后台线程中执行
        std::cout << "任务 " << i << " 执行中" << std::endl;
    });
}

// 等待所有任务完成后停止
service.stop();

JSON Utils - JSON 工具

#include "enterprise/json_utils.hpp"

// 文件读写
auto j = enterprise::json_utils::load_json("data.json");
enterprise::json_utils::save_json(j, "output.json", true);

// 深度合并(递归合并嵌套对象)
auto base = R"({ "a": 1, "nested": { "x": 1, "y": 2 } })"_json;
auto overlay = R"({ "b": 2, "nested": { "y": 99, "z": 3 } })"_json;
auto merged = enterprise::json_utils::merge(base, overlay);
// 结果: { "a": 1, "b": 2, "nested": { "x": 1, "y": 99, "z": 3 } }

// 点分路径访问
auto value = enterprise::json_utils::get_path(j, "users.alice.age");
bool exists = enterprise::json_utils::has_path(j, "users.alice.age");

// JSON 转 CSV
auto json = enterprise::json_utils::from_csv("name,age\nAlice,30\nBob,25");
// json: [{"name": "Alice", "age": "30"}, {"name": "Bob", "age": "25"}]

std::string csv = enterprise::json_utils::to_csv(json_array);

INI Utils - INI 配置读写

#include "enterprise/ini_utils.hpp"

// 从文件加载 INI
auto ini = enterprise::IniUtils::load_ini("config.ini");

// 或从字符串加载
auto ini = enterprise::IniUtils::load_from_string(R"(
[database]
host=localhost
port=5432
name=mydb

[server]
host=0.0.0.0
port=8080
debug=true
)");

// 类型安全的取值(带默认值回退)
auto host = ini.get_or<std::string>("database", "host", "unknown");
auto port = ini.get_or<int>("database", "port", 0);
auto debug = ini.get_or<bool>("server", "debug", false);

// 设置值
ini.set("server", "version", "1.0.0");
ini.set("server", "port", 9090);

// 检查节和键是否存在
bool has_db = ini.has_section("database");
bool has_port = ini.has_key("server", "port");

// 获取所有节和键
auto sections = ini.get_sections();
auto keys = ini.get_keys("server");

// 保存到文件
ini.save_to_file("config.output.ini");

// 转换为字符串
std::string content = ini.to_string();

测试

测试框架使用 Catch2 v3,支持自动发现:

./scripts/test.sh                  # 运行所有测试
./scripts/test.sh -f "[config]"    # 按标签过滤
./scripts/test.sh -v               # 详细输出

代码覆盖率

./scripts/build.sh --coverage
# 覆盖率报告生成在 build/coverage/ 目录

项目结构

enterprise_cpp_project/
├── cmake/                  # CMake 模块(代码覆盖率等)
├── docs/                   # 项目文档
│   ├── ARCHITECTURE.md     # 架构设计文档
│   └── API.md              # API 文档
├── include/enterprise/     # 公共头文件
│   ├── config.hpp          # 配置管理(单例)
│   ├── json_utils.hpp      # JSON 工具函数
│   ├── ini_utils.hpp       # INI 配置读写
│   ├── logger.hpp          # 日志模块(单例)
│   └── service.hpp         # 线程池服务
├── scripts/                # 构建和工具脚本
│   ├── build.sh            # 构建脚本
│   ├── test.sh             # 测试脚本
│   └── clean.sh            # 清理脚本
├── src/
│   ├── core/               # 核心实现
│   │   ├── config.cpp
│   │   ├── json_utils.cpp
│   │   ├── ini_utils.cpp
│   │   ├── logger.cpp
│   │   └── service.cpp
│   └── main.cpp            # 应用程序入口
├── tests/                  # 单元测试
│   ├── test_config.cpp
│   ├── test_json_utils.cpp
│   ├── test_ini_utils.cpp
│   └── test_service.cpp
├── config.example.json     # JSON 配置示例
├── config.example.ini      # INI 配置示例
└── CMakeLists.txt          # 根构建配置

架构设计

┌─────────────────────────────────────────────────────────┐
│                      main.cpp                           │
│              协调 Logger、Config、Service、JSON Utils    │
├──────────┬───────────┬─────────────┬────────────────────┤
│  Logger  │  Config   │  Service    │   JSON Utils       │
│ Boost.Log│ nlohmann  │ Boost.Asio  │ nlohmann helpers   │
└──────────┴───────────┴─────────────┴────────────────────┘

设计模式

  • 单例模式 - Logger 和 Config 使用 Meyers 单例,线程安全的延迟初始化
  • 线程池模式 - Service 基于 Boost.Asio io_context 实现任务分发
  • 策略模式 - Config 以 nlohmann::json 存储值,通过模板方法实现类型安全访问

并发安全

模块 线程安全 说明
Logger ✅ 线程安全 Boost.Log 内部处理同步
Config ⚠️ 仅读取安全 初始化后只读访问是安全的
Service ✅ 线程安全 任务提交和停止操作线程安全
JSON Utils ✅ 线程安全 无状态函数,天然线程安全

依赖管理

库 版本 用途
Boost 1.81+ 日志、线程、Asio、文件系统
fmt 10+ 格式化字符串输出
nlohmann-json 3+ JSON 解析和操作
Catch2 3.6.0 单元测试框架(自动下载)

Conan 包管理器(可选)

项目也支持使用 Conan 2.x 管理依赖:

conan install . --output-folder=build --build=missing
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
cmake --build build

许可证

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages