生成代码工程

This commit is contained in:
root 2026-05-19 16:32:36 +08:00
parent 290dde6fcb
commit daeed9d17c
8 changed files with 4215 additions and 2 deletions

52
CMakeLists.txt Normal file
View File

@ -0,0 +1,52 @@
cmake_minimum_required(VERSION 3.14)
project(odf VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# ============================================================
# MSVC UTF-8
# ============================================================
if (MSVC)
add_compile_options(/utf-8)
endif()
# ============================================================
#
# ============================================================
set(ODF_SOURCES
src/app.cpp
)
set(ODF_HEADERS
include/app.hpp
)
# ============================================================
#
# ============================================================
add_executable(odf_main
src/main.cpp
${ODF_SOURCES}
${ODF_HEADERS}
)
target_include_directories(odf_main
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)
# ============================================================
# 使 assert
# ============================================================
add_executable(odf_test
tests/basic_test.cpp
${ODF_SOURCES}
${ODF_HEADERS}
)
target_include_directories(odf_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)

View File

@ -1,3 +1,42 @@
# ODF光纤配线单元 # ODF - Object Document Framework
暂无描述 一个轻量的 C++ 面向对象文档数据处理框架。
## 功能
- **Document** — 文档模型,包含多个段落
- **Paragraph** — 段落,包含多个文本片段
- **TextRun** — 文本片段,带格式属性(粗体、斜体)
- 导出文档为纯文本格式
## 编译
```bash
mkdir build && cd build
cmake ..
cmake --build .
```
## 运行
```bash
# 运行主程序
./odf_main
# 运行测试
./odf_test
```
## 工程结构
```
├── CMakeLists.txt
├── README.md
├── include/
│ └── app.hpp # 公开 API 头文件
├── src/
│ ├── app.cpp # 实现文件
│ └── main.cpp # 命令行入口
└── tests/
└── basic_test.cpp # 基本测试
```

3703
events.ndjson Normal file

File diff suppressed because it is too large Load Diff

28
generation.json Normal file
View File

@ -0,0 +1,28 @@
{
"projectId": 41,
"generationId": "codegen_0e6c4fcfb36447e3a1d82fda9d4a6d1f",
"language": "C++",
"status": "completed",
"fileIds": [],
"outputDir": "D:\\pro\\DocumentGenerateAgent\\agents\\ai_agents\\project-files\\codegen-runs\\codegen_0e6c4fcfb36447e3a1d82fda9d4a6d1f",
"relativeOutputDir": "codegen-runs/codegen_0e6c4fcfb36447e3a1d82fda9d4a6d1f",
"generatedFiles": [
"CMakeLists.txt",
"README.md",
"events.ndjson",
"include/app.hpp",
"src/app.cpp",
"src/main.cpp",
"tests/basic_test.cpp"
],
"analysisSummary": "未提供参考文件请仅根据用户自然语言描述生成C++工程。",
"eventLogFile": "D:\\pro\\DocumentGenerateAgent\\agents\\ai_agents\\project-files\\codegen-runs\\codegen_0e6c4fcfb36447e3a1d82fda9d4a6d1f\\events.ndjson",
"repoSettings": {
"username": "root",
"password": "pAssW0rd",
"repoUrl": "http://47.108.255.216:3000/root/ODF_TEST.git",
"branch": "main"
},
"repoUrl": "http://47.108.255.216:3000/root/ODF_TEST.git",
"branch": "main"
}

126
include/app.hpp Normal file
View File

@ -0,0 +1,126 @@
#ifndef ODF_APP_HPP
#define ODF_APP_HPP
#include <string>
#include <vector>
#include <memory>
#include <ostream>
namespace odf {
// ====================================================================
// 前置声明
// ====================================================================
class TextRun;
class Paragraph;
class Document;
// ====================================================================
// TextRun — 文本片段
// ====================================================================
/**
* @brief
*/
class TextRun {
public:
/**
* @brief TextRun
* @param text
* @param bold false
* @param italic false
*/
explicit TextRun(std::string text, bool bold = false, bool italic = false);
/// @brief 返回文本内容(只读)。
const std::string& text() const noexcept { return text_; }
/// @brief 是否加粗。
bool is_bold() const noexcept { return bold_; }
/// @brief 是否斜体。
bool is_italic() const noexcept { return italic_; }
/**
* @brief TextRun
* @param os
*/
void write(std::ostream& os) const;
private:
std::string text_;
bool bold_;
bool italic_;
};
// ====================================================================
// Paragraph — 段落
// ====================================================================
/**
* @brief TextRun
*/
class Paragraph {
public:
/// @brief 默认构造空段落。
Paragraph() = default;
/**
* @brief
* @param run TextRun
*/
void add_run(TextRun run);
/// @brief 返回段落中所有文本片段(只读)。
const std::vector<TextRun>& runs() const noexcept { return runs_; }
/**
* @brief
* @param os
*/
void write(std::ostream& os) const;
private:
std::vector<TextRun> runs_;
};
// ====================================================================
// Document — 文档模型
// ====================================================================
/**
* @brief
*/
class Document {
public:
/// @brief 默认构造空文档。
Document() = default;
/**
* @brief
* @param para Paragraph
*/
void add_paragraph(Paragraph para);
/// @brief 返回文档中的所有段落(只读)。
const std::vector<Paragraph>& paragraphs() const noexcept { return paragraphs_; }
/**
* @brief
* @param os
*/
void write(std::ostream& os) const;
/**
* @brief
* @return
*/
std::string to_string() const;
private:
std::vector<Paragraph> paragraphs_;
};
} // namespace odf
#endif // ODF_APP_HPP

96
src/app.cpp Normal file
View File

@ -0,0 +1,96 @@
#include "app.hpp"
#include <sstream>
#include <ostream>
namespace odf {
// ====================================================================
// TextRun
// ====================================================================
/**
* @brief TextRun
*
* /
*/
TextRun::TextRun(std::string text, bool bold, bool italic)
: text_(std::move(text)), bold_(bold), italic_(italic) {
}
/**
* @brief TextRun
*
*
* - _文本_
* - **
* - **_文本_**
*/
void TextRun::write(std::ostream& os) const {
if (bold_ && italic_) {
os << "***" << text_ << "***";
} else if (bold_) {
os << "*" << text_ << "*";
} else if (italic_) {
os << "_" << text_ << "_";
} else {
os << text_;
}
}
// ====================================================================
// Paragraph
// ====================================================================
/**
* @brief
*/
void Paragraph::add_run(TextRun run) {
runs_.push_back(std::move(run));
}
/**
* @brief
*
* TextRun
*/
void Paragraph::write(std::ostream& os) const {
for (const auto& run : runs_) {
run.write(os);
}
os << '\n';
}
// ====================================================================
// Document
// ====================================================================
/**
* @brief
*/
void Document::add_paragraph(Paragraph para) {
paragraphs_.push_back(std::move(para));
}
/**
* @brief
*
*
*/
void Document::write(std::ostream& os) const {
for (const auto& para : paragraphs_) {
para.write(os);
}
}
/**
* @brief
*
* 使 std::ostringstream write
*/
std::string Document::to_string() const {
std::ostringstream oss;
write(oss);
return oss.str();
}
} // namespace odf

51
src/main.cpp Normal file
View File

@ -0,0 +1,51 @@
/**
* @file main.cpp
* @brief ODFObject Document Framework
*
*
*/
#include "app.hpp"
#include <iostream>
/**
* @brief
*
*
* - 1 TextRun TextRun
* - 2 TextRun + TextRun
*/
static void run_demo() {
using namespace odf;
Document doc;
// ---- 段落1 ----
{
Paragraph p;
p.add_run(TextRun("Hello, "));
p.add_run(TextRun("ODF Framework!", false, true)); // 斜体
doc.add_paragraph(std::move(p));
}
// ---- 段落2 ----
{
Paragraph p;
p.add_run(TextRun("This is ", true, false)); // 加粗
p.add_run(TextRun("bold and italic.", true, true)); // 加粗 + 斜体
doc.add_paragraph(std::move(p));
}
std::cout << "===== Object Document Framework Demo =====\n\n";
doc.write(std::cout);
std::cout << "\n===== End =====" << std::endl;
}
/**
* @brief
* @return 0
*/
int main() {
run_demo();
return 0;
}

118
tests/basic_test.cpp Normal file
View File

@ -0,0 +1,118 @@
/**
* @file basic_test.cpp
* @brief 使 assert ODF
*/
#include "app.hpp"
#include <cassert>
#include <sstream>
#include <string>
/**
* @brief TextRun 访
*/
static void test_text_run() {
using namespace odf;
TextRun plain("hello");
assert(plain.text() == "hello");
assert(!plain.is_bold());
assert(!plain.is_italic());
TextRun bold("bold", true, false);
assert(bold.text() == "bold");
assert(bold.is_bold());
assert(!bold.is_italic());
TextRun italic("italic", false, true);
assert(italic.text() == "italic");
assert(!italic.is_bold());
assert(italic.is_italic());
TextRun both("both", true, true);
assert(both.text() == "both");
assert(both.is_bold());
assert(both.is_italic());
}
/**
* @brief TextRun::write
*/
static void test_text_run_write() {
using namespace odf;
std::ostringstream oss;
TextRun("plain").write(oss);
assert(oss.str() == "plain");
oss.str("");
TextRun("b", true, false).write(oss);
assert(oss.str() == "*b*");
oss.str("");
TextRun("i", false, true).write(oss);
assert(oss.str() == "_i_");
oss.str("");
TextRun("bi", true, true).write(oss);
assert(oss.str() == "***bi***");
}
/**
* @brief Paragraph add_run write
*/
static void test_paragraph() {
using namespace odf;
Paragraph p;
p.add_run(TextRun("A "));
p.add_run(TextRun("B", true, false));
assert(p.runs().size() == 2);
std::ostringstream oss;
p.write(oss);
assert(oss.str() == "A *B*\n");
}
/**
* @brief Document add_paragraphwrite to_string
*/
static void test_document() {
using namespace odf;
Document doc;
assert(doc.paragraphs().empty());
{
Paragraph p;
p.add_run(TextRun("Line1"));
doc.add_paragraph(std::move(p));
}
{
Paragraph p;
p.add_run(TextRun("Line2"));
doc.add_paragraph(std::move(p));
}
assert(doc.paragraphs().size() == 2);
std::ostringstream oss;
doc.write(oss);
assert(oss.str() == "Line1\nLine2\n");
// to_string 应与 write 结果一致
assert(doc.to_string() == "Line1\nLine2\n");
}
/**
* @brief
*/
int main() {
test_text_run();
test_text_run_write();
test_paragraph();
test_document();
return 0;
}