diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fa1d69..e9eccf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.12) project(duckx VERSION 0.2) @@ -13,7 +13,7 @@ option(BUILD_SAMPLES "Build provided samples" OFF) # else we might encounter errors making the library # not being able to be compiled -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) set(HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/duckx.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/include/constants.hpp" @@ -39,6 +39,7 @@ add_library(duckx::duckx ALIAS duckx) target_include_directories(duckx PUBLIC $ + $ $ ) diff --git a/include/duckx.hpp b/include/duckx.hpp index 6b8f38e..5ea66b8 100755 --- a/include/duckx.hpp +++ b/include/duckx.hpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include @@ -19,6 +21,19 @@ // TODO: Use container-iterator design pattern! namespace duckx { + +template +std::optional> +get_by_index(T& obj, size_t index){ + if(index == 0) { + return obj; + } + if(obj.has_next()) { + return get_by_index(obj.next(), index - 1); + } + return std::nullopt; +} + // Run contains runs in a paragraph class Run { private: @@ -40,6 +55,7 @@ class Run { Run &next(); bool has_next() const; + std::optional> get_run_by_index(size_t index); }; // Paragraph contains a paragraph @@ -68,6 +84,7 @@ class Paragraph { Run &add_run(const char *, duckx::formatting_flag = duckx::none); Paragraph &insert_paragraph_after(const std::string &, duckx::formatting_flag = duckx::none); + std::optional> get_paragraph_by_index(size_t index); }; // TableCell contains one or more paragraphs @@ -90,6 +107,7 @@ class TableCell { TableCell &next(); bool has_next() const; + std::optional> get_table_cell_by_index(size_t index); }; // TableRow consists of one or more TableCells @@ -110,6 +128,7 @@ class TableRow { bool has_next() const; TableRow &next(); + std::optional> get_table_row_by_index(size_t index); }; // Table consists of one or more TableRow objects diff --git a/src/duckx.cpp b/src/duckx.cpp index 550ad9f..3cb10a2 100644 --- a/src/duckx.cpp +++ b/src/duckx.cpp @@ -46,6 +46,11 @@ duckx::Run &duckx::Run::next() { bool duckx::Run::has_next() const { return this->current != 0; } +std::optional> +duckx::Run::get_run_by_index(size_t index) { + return get_by_index(*this, index); +} + // Table cells duckx::TableCell::TableCell() {} @@ -67,6 +72,10 @@ void duckx::TableCell::set_current(pugi::xml_node node) { bool duckx::TableCell::has_next() const { return this->current != 0; } +std::optional> duckx::TableCell::get_table_cell_by_index(size_t index) { + return get_by_index(*this, index); +} + duckx::TableCell &duckx::TableCell::next() { this->current = this->current.next_sibling(); return *this; @@ -99,6 +108,10 @@ duckx::TableRow &duckx::TableRow::next() { return *this; } +std::optional> duckx::TableRow::get_table_row_by_index(size_t index) { + return get_by_index(*this, index); +} + duckx::TableCell &duckx::TableRow::cells() { this->cell.set_parent(this->current); return this->cell; @@ -235,6 +248,11 @@ duckx::Paragraph::insert_paragraph_after(const std::string &text, return *p; } +std::optional> +duckx::Paragraph::get_paragraph_by_index(size_t index){ + return get_by_index(*this, index); +} + duckx::Document::Document() { // TODO: this function must be removed! this->directory = "";