Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New methods for working by index. #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.12)

project(duckx VERSION 0.2)

Expand All @@ -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"
Expand All @@ -39,6 +39,7 @@ add_library(duckx::duckx ALIAS duckx)

target_include_directories(duckx PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

Expand Down
19 changes: 19 additions & 0 deletions include/duckx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <optional>
#include <functional>

#include <constants.hpp>
#include <duckxiterator.hpp>
Expand All @@ -19,6 +21,19 @@
// TODO: Use container-iterator design pattern!

namespace duckx {

template <class T>
std::optional<std::reference_wrapper<T>>
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:
Expand All @@ -40,6 +55,7 @@ class Run {

Run &next();
bool has_next() const;
std::optional<std::reference_wrapper<Run>> get_run_by_index(size_t index);
};

// Paragraph contains a paragraph
Expand Down Expand Up @@ -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<std::reference_wrapper<Paragraph>> get_paragraph_by_index(size_t index);
};

// TableCell contains one or more paragraphs
Expand All @@ -90,6 +107,7 @@ class TableCell {

TableCell &next();
bool has_next() const;
std::optional<std::reference_wrapper<TableCell>> get_table_cell_by_index(size_t index);
};

// TableRow consists of one or more TableCells
Expand All @@ -110,6 +128,7 @@ class TableRow {

bool has_next() const;
TableRow &next();
std::optional<std::reference_wrapper<TableRow>> get_table_row_by_index(size_t index);
};

// Table consists of one or more TableRow objects
Expand Down
18 changes: 18 additions & 0 deletions src/duckx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ duckx::Run &duckx::Run::next() {

bool duckx::Run::has_next() const { return this->current != 0; }

std::optional<std::reference_wrapper<duckx::Run>>
duckx::Run::get_run_by_index(size_t index) {
return get_by_index(*this, index);
}

// Table cells
duckx::TableCell::TableCell() {}

Expand All @@ -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<std::reference_wrapper<duckx::TableCell>> 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;
Expand Down Expand Up @@ -99,6 +108,10 @@ duckx::TableRow &duckx::TableRow::next() {
return *this;
}

std::optional<std::reference_wrapper<duckx::TableRow>> 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;
Expand Down Expand Up @@ -235,6 +248,11 @@ duckx::Paragraph::insert_paragraph_after(const std::string &text,
return *p;
}

std::optional<std::reference_wrapper<duckx::Paragraph>>
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 = "";
Expand Down