Skip to content

Commit 528538c

Browse files
authored
Merge pull request #16 from snandasena/dev
Dev
2 parents 2fef0b3 + 19059ac commit 528538c

File tree

5 files changed

+227
-1
lines changed

5 files changed

+227
-1
lines changed

Udemy/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
add_subdirectory(SOLID)
1+
add_subdirectory(SOLID)
2+
3+
add_subdirectory(builder )

Udemy/builder/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
project(builder)
2+
3+
#add_executable(motivation src/motivation_demo.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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Created by sajith on 6/10/22.
3+
//
4+
5+
#include <iostream>
6+
#include <string>
7+
#include <sstream>
8+
9+
using namespace std;
10+
11+
int main()
12+
{
13+
auto text = "hello";
14+
string output;
15+
output += "<p>";
16+
output += text;
17+
output += "</p>";
18+
19+
cout << output << endl;
20+
21+
string words[] = {"hello", "world"};
22+
ostringstream oss;
23+
24+
oss << "<ul>\n";
25+
for (const auto &word: words)
26+
{
27+
oss << " <li> " << word << " </li> \n";
28+
}
29+
oss << "</ul>";
30+
31+
cout << oss.str() << endl;
32+
return 0;
33+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// Created by sajith on 6/10/22.
3+
//
4+
5+
#include <iostream>
6+
#include <sstream>
7+
#include <vector>
8+
9+
10+
using namespace std;
11+
12+
struct HtmlElement
13+
{
14+
string name;
15+
string text;
16+
vector<HtmlElement> elements;
17+
const size_t indent_size = 2;
18+
19+
HtmlElement() = default;
20+
21+
HtmlElement(const string &n, const string &t) : name{n}, text{t} {}
22+
23+
string str(int indent = 0) const
24+
{
25+
ostringstream oss;
26+
string i(indent_size * indent, ' ');
27+
oss << i << "<" << name << ">" << endl;
28+
if (!text.empty())
29+
{
30+
oss << string(indent_size * (indent + 1), ' ') << text << endl;
31+
}
32+
33+
for (const auto &item: elements)
34+
{
35+
oss << item.str(indent + 1);
36+
}
37+
oss << i << "</" << name << ">" << endl;
38+
return oss.str();
39+
}
40+
};
41+
42+
struct HtmlBuilder
43+
{
44+
HtmlElement root;
45+
46+
HtmlBuilder(const string &root_name)
47+
{
48+
root.name = root_name;
49+
}
50+
51+
void add_element(const string &child_name, const string &child_text)
52+
{
53+
HtmlElement e{child_name, child_text};
54+
root.elements.emplace_back(e);
55+
}
56+
57+
string str() const { return root.str(); }
58+
};
59+
60+
int main()
61+
{
62+
HtmlBuilder builder{"ul"};
63+
builder.add_element("li", "Hello");
64+
builder.add_element("li", "World!");
65+
66+
cout << builder.str() << endl;
67+
return 0;
68+
}

0 commit comments

Comments
 (0)