Skip to content

Commit

Permalink
refactor(printable.hpp): use printable instead of Exportable
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 4, 2024
1 parent 59d6127 commit a4154ef
Show file tree
Hide file tree
Showing 23 changed files with 564 additions and 381 deletions.
20 changes: 13 additions & 7 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Simple

```C++
using myxml;
using namespace myxml;
// `std::string xml`
document doc = document::parse(xml);
// or
Expand All @@ -20,6 +20,15 @@ optional<element> elem = doc.first_elem("elem");

### Element

#### Basic

```C++
using namespace myxml;
element elem("root");
fmt::println(root->name());
root->set_name("far");
```
#### Attributes
```C++
Expand All @@ -35,11 +44,6 @@ fmt::println(elem["hello"]);
elem["hello"] = "world!";
// if key not exist, query it will create an empty attribute
elem["hello"]; // == elem["hello"] = "";
// use attribute to get an optional reference
if (auto value = elem.attribute("hello"); value)
{
fmt::println("element has attribute \"hello\" with value {}", *value);
}
```

#### Children
Expand All @@ -58,7 +62,9 @@ std::vector<element> children = root.elems("child");
### Text

```C++
// Simple Query
// create from string
text txt = "Hello";
// or from query
text txt = root.first_text();
// print raw
fmt::println(txt);
Expand Down
6 changes: 4 additions & 2 deletions include/myxml/cdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ namespace myxml
explicit CData(std::string);

virtual ~CData() = default;
virtual std::string ExportRaw() const override;
virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override;
// virtual std::string ExportRaw() const override;
// virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override;
virtual void entity_encoding(bool) override;
virtual void platform_specific_newline(bool) override {}
virtual void print(std::ostream &) const override;
};
}
30 changes: 16 additions & 14 deletions include/myxml/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Declaration and Documant are both NOT Node
namespace myxml
{
struct declaration : public exportable
struct declaration : public printable
{
std::string version;
std::optional<std::string> encoding;
Expand All @@ -19,37 +19,39 @@ namespace myxml
/* Exportable */
virtual std::string ExportRaw() const;
virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const;
virtual void entity_encoding(bool) {};
virtual void platform_specific_newline(bool) {};
virtual void print(std::ostream &os) const {}
};

class document : public exportable
class document : public printable
{
private:
declaration decl;
std::shared_ptr<Element> root;
element root;

public:
/* Manipulate */
void set_declaration(const declaration &);
void set_root(std::shared_ptr<Element> root);
void set_root(std::shared_ptr<element_impl> root);

/* Query */
const declaration &get_declaration() const;
declaration &get_declaration();
const std::shared_ptr<Element> &get_root() const;
std::shared_ptr<Element> get_root();
std::shared_ptr<Element> first_elem(std::string_view);
std::shared_ptr<Element> first_elem();
std::shared_ptr<Text> first_text();
const element &get_root() const;
element &get_root();
element first_elem(std::string_view);
element first_elem();
text first_text();

/** Load */
static document parse(std::string_view);
static document load(std::string fileName);

/* Exportable */
virtual std::string ExportRaw() const;
virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const;
virtual void entity_encoding(bool);
virtual void platform_specific_newline(bool);
/* Implement printable */
virtual void print(std::ostream &os) const override;
virtual void entity_encoding(bool) override;
virtual void platform_specific_newline(bool) override;
};

namespace util
Expand Down
85 changes: 54 additions & 31 deletions include/myxml/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,77 @@
#include <map>

#include "myxml/text.hpp"
#include "myxml/exportable.hpp"
#include "myxml/printable.hpp"

namespace myxml
{
class Element : public CompositeNode // public std::enable_shared_from_this<Element>, public Node

enum class ClosingType
{
public:
enum class ClosingType
{
Closed,
Closing,
};
Closed,
Closing,
};

class element;

namespace literals
{
// Custom String Literal for Element
element operator""_elem(const char *, std::size_t);
}

class element : public printable
{

friend element literals::operator""_elem(const char *, std::size_t);
friend class document;

private:
std::shared_ptr<element_impl> _impl;
element(std::shared_ptr<element_impl>);

public:
element() : _impl(nullptr) {}
explicit element(std::string_view);

static element parse(std::string_view);
static element load(std::string_view);
std::string &operator[](const std::string &);
std::string_view name();

element first_elem();
element first_elem(std::string_view);
text first_text();

/* Implement printable */
virtual void print(std::ostream &) const override;
virtual void entity_encoding(bool) override;
virtual void platform_specific_newline(bool) override;
};

struct element_impl : public CompositeNode // public std::enable_shared_from_this<Element>, public Node
{
public:
std::string name;
std::map<std::string, std::string, std::less<>> attributes;

/* Set initializer as private to avoid using Element without share_ptr*/
explicit Element(std::string_view name);
Element() = default;

public:
virtual ~Element() = default;

virtual ~element_impl() = default;
explicit element_impl(std::string_view name);
element_impl() = default;
/* Builder */
// Wraps creating shared_ptr
static std::shared_ptr<Element> New(std::string_view name);
static std::shared_ptr<Element> New();
static std::shared_ptr<Element> Parse(std::string_view buf);

/* Query */
std::optional<std::string_view> GetAttribute(std::string_view name);
std::string_view GetName() const;
static std::shared_ptr<element_impl> _new(std::string_view name);
static std::shared_ptr<element_impl> _new();
static std::shared_ptr<element_impl> parse(std::string_view buf);
static std::shared_ptr<element_impl> load(std::string_view path);

/* Manipulate */
void SetName(std::string_view);
void SetAttribute(std::string key, std::string value);
void ExtendAttributes(std::map<std::string, std::string>);
void extend_attributes(std::map<std::string, std::string>);
std::string &operator[](const std::string &);

/* Implement Exportable */
virtual std::string ExportRaw() const override;
virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override;
virtual void print(std::ostream &) const override;
};

namespace literals
{
// Custom String Literal for Element
std::shared_ptr<Element> operator""_elem(const char *, std::size_t);
}
}
27 changes: 0 additions & 27 deletions include/myxml/exportable.hpp

This file was deleted.

42 changes: 21 additions & 21 deletions include/myxml/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include <optional>
#include <map>

#include "myxml/exportable.hpp"
#include "myxml/printable.hpp"

namespace myxml
{
// defined in element.hpp
class Element;
class element_impl;
// defined in text.hpp
class Text;
class text_impl;
// defined below
class CompositeNode;

// Element, Text are Node.
class Node : public std::enable_shared_from_this<Node>, public exportable
class Node : public std::enable_shared_from_this<Node>, public printable
{
private:
template <typename T, typename = std::enable_if_t<std::is_base_of_v<Node, T>>>
Expand Down Expand Up @@ -59,10 +59,10 @@ namespace myxml
/* Query */
std::shared_ptr<Node> NextSibiling();
std::shared_ptr<Node> PrevSibiling();
std::shared_ptr<Element> NextElem();
std::shared_ptr<Element> PrevElem();
std::shared_ptr<Text> NextText();
std::shared_ptr<Text> PrevText();
std::shared_ptr<element_impl> NextElem();
std::shared_ptr<element_impl> PrevElem();
std::shared_ptr<text_impl> NextText();
std::shared_ptr<text_impl> PrevText();

/** Implement Export */
virtual void entity_encoding(bool) override;
Expand All @@ -75,7 +75,19 @@ namespace myxml
private:
std::shared_ptr<Node> firstChild;
std::shared_ptr<Node> lastChild;
std::map<std::string, std::weak_ptr<Element>, std::less<>> nameToElemBuffer;
std::map<std::string, std::weak_ptr<element_impl>, std::less<>> nameToElemBuffer;

public:
virtual ~CompositeNode() = default;

/* Query */
std::shared_ptr<Node> FirstChild();
const std::shared_ptr<Node> &FirstChild() const;
std::shared_ptr<Node> LastChild();
const std::shared_ptr<Node> &LastChild() const;
std::shared_ptr<element_impl> Elem(std::string_view name);
std::shared_ptr<element_impl> FirstElem();
std::shared_ptr<text_impl> FirstText();

template <typename T, typename = std::enable_if_t<std::is_base_of_v<Node, T>>>
std::shared_ptr<T> First()
Expand All @@ -90,18 +102,6 @@ namespace myxml
return nullptr;
}

public:
virtual ~CompositeNode() = default;

/* Query */
std::shared_ptr<Node> FirstChild();
const std::shared_ptr<Node> &FirstChild() const;
std::shared_ptr<Node> LastChild();
const std::shared_ptr<Node> &LastChild() const;
std::shared_ptr<Element> Elem(std::string_view name);
std::shared_ptr<Element> FirstElem();
std::shared_ptr<Text> FirstText();

/* Manipulate */
std::shared_ptr<Node> InsertAtFront(const std::shared_ptr<Node> &);
std::shared_ptr<Node> InsertAtEnd(const std::shared_ptr<Node> &);
Expand Down
6 changes: 3 additions & 3 deletions include/myxml/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace myxml
/**
* @throws `SyntaxError` if faild to find `<`
*/
std::shared_ptr<Text> parseText();
std::shared_ptr<text_impl> parseText();
/**
* @returns `std::nullopt` if not start with `<!CDATA[`
*/
Expand All @@ -72,7 +72,7 @@ namespace myxml
* @throws `SyntaxError`
* @throws `SemanticError`
*/
std::shared_ptr<Element> parseElementWithHeader(ElementTag header);
std::shared_ptr<element_impl> parseElementWithHeader(ElementTag header);
/**
* @returns std::nullopt if not starts with `<?xml`
* @throws `UnexpectedEndOfInput`
Expand All @@ -82,7 +82,7 @@ namespace myxml
std::optional<declaration> parseDeclaration();

public:
std::shared_ptr<Element> ParseElement();
std::shared_ptr<element_impl> ParseElement();
/**
* @returns std::nullopt if no heading `<`
* @throws `SyntaxError` if the heading character is `<` and the trailing characters are in incorrect format
Expand Down
Loading

0 comments on commit a4154ef

Please sign in to comment.