Skip to content

Commit 07c96ac

Browse files
committed
Imoproved
1 parent 19059ac commit 07c96ac

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ project(design_patterns)
44
set(CMAKE_CXX_STANDARD 17)
55
FIND_PACKAGE(Boost 1.67.0 REQUIRED)
66

7-
set(CMAKE_CXX_FLAGS "-fsanitize=address -ggdb3 -Wall -Wextra")
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -ggdb3 -Wall -Wextra")
8+
89
add_definitions(-std=c++17)
910
add_definitions(-DCMAKE_BUILD_TYPE=Debug)
1011

11-
set(CXX_FLAGS "-fsanitize=address -ggdb3 -Wall -Wextra")
12-
1312
add_subdirectory(Udemy)
1413

1514
# creational

Udemy/builder/CMakeLists.txt

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

33
#add_executable(motivation src/motivation_demo.cpp)
44
#add_executable(motivation_v1 src/motivation_improved_v1.cpp)
5-
add_executable(fluent_builder src/fluent_builder.cpp)
5+
#add_executable(fluent_builder src/fluent_builder.cpp)
6+
add_executable(groovy_style_builder src/groovy_style_builder.cpp)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// Created by sajith on 6/10/22.
3+
//
4+
5+
6+
#include <iostream>
7+
#include <sstream>
8+
#include <vector>
9+
#include <memory>
10+
11+
using namespace std;
12+
13+
14+
struct Tag
15+
{
16+
string name;
17+
string text;
18+
vector<Tag> children;
19+
vector<pair<string, string >> attributes;
20+
21+
friend ostream &operator<<(ostream &out, const Tag &tag)
22+
{
23+
out << "<" << tag.name;
24+
25+
for (const auto &[first, second]: tag.attributes)
26+
{
27+
out << " " << first << "=\"" << second << "\"";
28+
}
29+
30+
if (tag.children.empty() && tag.text.empty())
31+
{
32+
out << "/>" << endl;
33+
}
34+
else
35+
{
36+
out << ">" << endl;
37+
if (!tag.text.empty())
38+
{
39+
out << tag.text << endl;
40+
}
41+
42+
for (const auto &child: tag.children)
43+
{
44+
out << child;
45+
}
46+
out << "</" << tag.name << ">" << endl;
47+
}
48+
return out;
49+
}
50+
51+
protected:
52+
53+
public:
54+
55+
Tag(const string &n, const string &t) : name{n}, text{text} {}
56+
57+
Tag(const string &n, const vector<Tag> &c) : name{n}, children(c) {}
58+
};
59+
60+
struct P : Tag
61+
{
62+
P(const string &text) : Tag{"p", text} {}
63+
64+
P(initializer_list<Tag> children) : Tag{"p", children} {}
65+
};
66+
67+
struct IMG : Tag
68+
{
69+
explicit IMG(const string &url) : Tag{"img", ""}
70+
{
71+
attributes.emplace_back(make_pair("src", url));
72+
}
73+
};
74+
75+
int main()
76+
{
77+
cout <<
78+
P{
79+
IMG{"http.example.com/image.png"}
80+
}
81+
<< endl;
82+
83+
return 0;
84+
}
85+

0 commit comments

Comments
 (0)