Skip to content

Commit 19059ac

Browse files
committed
Imoproved
1 parent 4fad504 commit 19059ac

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

Udemy/builder/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
project(builder)
22

33
#add_executable(motivation src/motivation_demo.cpp)
4-
add_executable(motivation_v1 src/motivation_improved_v1.cpp)
4+
#add_executable(motivation_v1 src/motivation_improved_v1.cpp)
5+
add_executable(fluent_builder src/fluent_builder.cpp)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// Created by sajith on 6/10/22.
3+
//
4+
5+
#include <iostream>
6+
#include <sstream>
7+
#include <vector>
8+
#include <memory>
9+
10+
11+
using namespace std;
12+
13+
14+
struct HtmlBuilder;
15+
16+
class HtmlElement
17+
{
18+
friend struct HtmlBuilder;
19+
string name;
20+
string text;
21+
vector<HtmlElement> elements;
22+
const size_t indent_size = 2;
23+
24+
HtmlElement() = default;
25+
26+
HtmlElement(const string &n, const string &t) : name{n}, text{t} {}
27+
28+
public:
29+
30+
string str(int indent = 0) const
31+
{
32+
ostringstream oss;
33+
string i(indent_size * indent, ' ');
34+
oss << i << "<" << name << ">" << endl;
35+
if (!text.empty())
36+
{
37+
oss << string(indent_size * (indent + 1), ' ') << text << endl;
38+
}
39+
40+
for (const auto &item: elements)
41+
{
42+
oss << item.str(indent + 1);
43+
}
44+
oss << i << "</" << name << ">" << endl;
45+
return oss.str();
46+
}
47+
48+
// static HtmlBuilder build(string root_name)
49+
// {
50+
// return {root_name};
51+
// }
52+
53+
static unique_ptr<HtmlBuilder> create(string root_name)
54+
{
55+
return make_unique<HtmlBuilder>(root_name);
56+
}
57+
};
58+
59+
class HtmlBuilder
60+
{
61+
HtmlElement root;
62+
63+
public:
64+
65+
explicit HtmlBuilder(const string &root_name)
66+
{
67+
root.name = root_name;
68+
}
69+
70+
HtmlBuilder &add_element(const string &child_name, const string &child_text)
71+
{
72+
HtmlElement e{child_name, child_text};
73+
root.elements.emplace_back(e);
74+
return *this;
75+
}
76+
77+
string str() const { return root.str(); }
78+
79+
HtmlElement build()
80+
{
81+
return root;
82+
}
83+
84+
operator HtmlElement() const
85+
{
86+
return root;
87+
}
88+
};
89+
90+
int main()
91+
{
92+
HtmlBuilder builder{"ul"};
93+
builder.add_element("li", "Hello");
94+
builder.add_element("li", "World!");
95+
cout << builder.str() << endl;
96+
97+
HtmlBuilder builder1{"ul"};
98+
builder1
99+
.add_element("li", "Hello")
100+
.add_element("li", "World");
101+
102+
cout << builder1.str() << endl;
103+
104+
// auto builder2 = HtmlElement::build("ul").add_element("li", "Hello").add_element("li", "World");
105+
// cout << builder2.str() << endl;
106+
//
107+
// HtmlElement elem = HtmlElement::build("ul").add_element("li", "Hello");
108+
109+
auto bilder = HtmlElement::create("ul")->
110+
add_element("li", "Hello")
111+
.add_element("li", "World")
112+
.build();
113+
cout << bilder.str() << endl;
114+
115+
// HtmlElement
116+
117+
return 0;
118+
}

0 commit comments

Comments
 (0)