Skip to content

Commit 6ce0868

Browse files
authored
Merge pull request azubieta#3 from azubieta/ast_deep_copy
Ast deep copy
2 parents fb66242 + 4e8dbb7 commit 6ce0868

File tree

18 files changed

+335
-91
lines changed

18 files changed

+335
-91
lines changed

include/XdgUtils/DesktopEntry/DesktopEntryKeyPath.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace XdgUtils {
2020
public:
2121
explicit DesktopEntryKeyPath(const std::string& path);
2222

23+
DesktopEntryKeyPath(const std::string& group, const std::string& key, const std::string& locale);
24+
2325
DesktopEntryKeyPath(const DesktopEntryKeyPath& other);
2426

2527
DesktopEntryKeyPath& operator=(const DesktopEntryKeyPath& other);
@@ -71,6 +73,11 @@ namespace XdgUtils {
7173
*/
7274
void setLocale(const std::string& locale);
7375

76+
/**
77+
* @return key and locale sections
78+
*/
79+
std::string fullKey() const;
80+
7481
/**
7582
* @return string representation of the KeyPath
7683
*/

src/DesktopEntry/AST/AST.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,27 @@ namespace XdgUtils {
66
namespace DesktopEntry {
77
namespace AST {
88

9+
std::vector<std::shared_ptr<Node>>& AST::getEntries() {
10+
return entries;
11+
}
12+
913
const std::vector<std::shared_ptr<Node>>& AST::getEntries() const {
1014
return entries;
1115
}
1216

1317
void AST::setEntries(const std::vector<std::shared_ptr<Node>>& entries) {
14-
AST::entries = entries;
18+
this->entries.clear();
19+
for (const auto& entry: entries)
20+
this->entries.emplace_back(entry->clone());
1521
}
1622

1723
bool AST::operator==(const AST& rhs) const {
1824
auto aItr = entries.begin();
1925
auto bItr = rhs.entries.begin();
2026

2127
while (aItr != entries.end() && bItr != rhs.entries.end()) {
22-
if (auto a = dynamic_cast<Group*>((*aItr).get())) {
23-
// if the first one is an Entry the second one must also be
24-
if (auto b = dynamic_cast<Group*>((*bItr).get())) {
25-
// if both are entries compare them as such
26-
if (*a != *b)
27-
return false;
28-
} else
29-
return false;
30-
}
31-
32-
if (auto a = dynamic_cast<Comment*>((*aItr).get())) {
33-
// if the first one is an Comment the second one must also be
34-
if (auto b = dynamic_cast<Comment*>((*bItr).get())) {
35-
// if both are comments compare them as such
36-
if (*a != *b)
37-
return false;
38-
} else
39-
return false;
40-
}
28+
if (*aItr->get() != *bItr->get())
29+
return false;
4130

4231
++aItr, ++bItr;
4332
}
@@ -64,6 +53,26 @@ namespace XdgUtils {
6453
output << std::endl;
6554
}
6655
}
56+
57+
AST::AST(const AST& other) {
58+
setEntries(other.entries);
59+
}
60+
61+
AST& AST::operator=(const AST& other) {
62+
setEntries(other.entries);
63+
return *this;
64+
}
65+
66+
AST::AST(AST&& other) noexcept {
67+
entries = std::move(other.entries);
68+
}
69+
70+
AST& AST::operator=(AST&& other) noexcept {
71+
entries = std::move(other.entries);
72+
return *this;
73+
}
74+
75+
AST::AST() = default;
6776
}
6877
}
6978
}

src/DesktopEntry/AST/AST.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ namespace XdgUtils {
1919
*/
2020
class AST {
2121
public:
22+
AST();
23+
24+
// Copy constructor
25+
AST(const AST& other);
26+
27+
// Copy assignment
28+
AST& operator=(const AST& other);
29+
30+
// Move constructor
31+
AST(AST&& other) noexcept;
32+
33+
// Move assignment
34+
AST& operator=(AST&& other) noexcept;
35+
36+
37+
std::vector<std::shared_ptr<Node>>& getEntries();
38+
2239
const std::vector<std::shared_ptr<Node>>& getEntries() const;
2340

2441
void setEntries(const std::vector<std::shared_ptr<Node>>& entries);

src/DesktopEntry/AST/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ add_library(
44
Group.cpp
55
Entry.cpp
66
Comment.cpp
7-
)
7+
Node.cpp)

src/DesktopEntry/AST/Comment.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ namespace XdgUtils {
4141
comment.write(os);
4242
return os;
4343
}
44+
45+
Node* Comment::clone() const {
46+
return new Comment(*this);
47+
}
4448
}
4549
}
4650
}

src/DesktopEntry/AST/Comment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace XdgUtils {
2020

2121
void write(std::ostream& output) const override;
2222

23+
Node* clone() const override;
24+
2325
bool operator==(const Comment& rhs) const;
2426

2527
bool operator!=(const Comment& rhs) const;

src/DesktopEntry/AST/Entry.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ namespace XdgUtils {
1919
: keyRaw(keyRaw), keyValue(keyValue), localeRaw(localeRaw),
2020
localeValue(localeValue), valueRaw(valueRaw), valueValue(valueValue) {}
2121

22+
Entry::Entry(const std::string& key, const std::string& locale, const std::string& value) :
23+
keyRaw(key), keyValue(key) {
24+
25+
if (!locale.empty()) {
26+
localeRaw = ('[' + locale + ']');
27+
localeValue = (locale);
28+
}
29+
30+
if (!value.empty()) {
31+
valueValue = value;
32+
valueRaw = '=' + value;
33+
}
34+
}
35+
2236
std::string Entry::getValue() const {
2337
return valueValue;
2438
}
@@ -51,6 +65,10 @@ namespace XdgUtils {
5165
entry.write(os);
5266
return os;
5367
}
68+
69+
Node* Entry::clone() const {
70+
return new Entry(*this);
71+
}
5472
}
5573
}
5674
}

src/DesktopEntry/AST/Entry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace XdgUtils {
1616
*/
1717
class Entry : public Node {
1818
public:
19+
Entry(const std::string &key, const std::string &locale, const std::string &value);
20+
1921
Entry(const std::string& keyRaw, const std::string& keyValue, const std::string& localeRaw,
2022
const std::string& localeValue, const std::string& valueRaw, const std::string& valueValue);
2123

@@ -29,6 +31,8 @@ namespace XdgUtils {
2931

3032
void write(std::ostream& output) const override;
3133

34+
Node* clone() const override;
35+
3236
/**
3337
* Compare two Entries according to they fields values. Raw values will be ignored
3438
* @param rhs

src/DesktopEntry/AST/Group.cpp

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ namespace XdgUtils {
3838
}
3939
}
4040

41+
std::vector<std::shared_ptr<Node>>& Group::getEntries() {
42+
return entries;;
43+
}
44+
4145
const std::vector<std::shared_ptr<Node>>& Group::getEntries() const {
4246
return entries;
4347
}
4448

4549
void Group::setEntries(const std::vector<std::shared_ptr<Node>>& entries) {
46-
Group::entries = entries;
50+
this->entries.clear();
51+
for (const auto& entry: entries)
52+
this->entries.emplace_back(entry->clone());
4753
}
4854

4955
bool Group::operator==(const Group& rhs) const {
@@ -54,25 +60,8 @@ namespace XdgUtils {
5460
auto bItr = rhs.entries.begin();
5561

5662
while (aItr != entries.end() && bItr != rhs.entries.end()) {
57-
if (auto a = dynamic_cast<Entry*>((*aItr).get())) {
58-
// if the first one is an Entry the second one must also be
59-
if (auto b = dynamic_cast<Entry*>((*bItr).get())) {
60-
// if both are entries compare them as such
61-
if (*a != *b)
62-
return false;
63-
} else
64-
return false;
65-
}
66-
67-
if (auto a = dynamic_cast<Comment*>((*aItr).get())) {
68-
// if the first one is an Comment the second one must also be
69-
if (auto b = dynamic_cast<Comment*>((*bItr).get())) {
70-
// if both are comments compare them as such
71-
if (*a != *b)
72-
return false;
73-
} else
74-
return false;
75-
}
63+
if (*aItr->get() != *bItr->get())
64+
return false;
7665

7766
++aItr, ++bItr;
7867
}
@@ -81,6 +70,7 @@ namespace XdgUtils {
8170
return (aItr == entries.end() && bItr == rhs.entries.end());
8271
}
8372

73+
8474
bool Group::operator!=(const Group& rhs) const {
8575
return !(rhs == *this);
8676
}
@@ -89,6 +79,38 @@ namespace XdgUtils {
8979
group.write(os);
9080
return os;
9181
}
82+
83+
Node* Group::clone() const {
84+
return new Group(*this);
85+
}
86+
87+
Group::Group(const Group& other) : headerValue(other.headerValue), headerRawValue(other.headerRawValue) {
88+
setEntries(other.entries);
89+
}
90+
91+
Group& Group::operator=(const Group& other) {
92+
headerValue = other.headerValue;
93+
headerRawValue = other.headerRawValue;
94+
95+
setEntries(other.entries);
96+
return *this;
97+
}
98+
99+
Group::Group(Group&& other) noexcept {
100+
headerValue = std::move(other.headerValue);
101+
headerRawValue = std::move(other.headerRawValue);
102+
entries = std::move(other.entries);
103+
}
104+
105+
Group& Group::operator=(Group&& other) noexcept {
106+
headerValue = std::move(other.headerValue);
107+
headerRawValue = std::move(other.headerRawValue);
108+
entries = std::move(other.entries);
109+
110+
return *this;
111+
}
112+
113+
Group::~Group() = default;
92114
}
93115
}
94116
}

src/DesktopEntry/AST/Group.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ namespace XdgUtils {
2222
*/
2323
Group(const std::string& headerRawValue, const std::string& headerValue);
2424

25+
// Copy constructor
26+
Group(const Group& other);
27+
28+
// Copy assigment
29+
Group& operator=(const Group& other);
30+
31+
// Move constructor
32+
Group(Group&& other) noexcept;
33+
34+
// Move assigment
35+
Group& operator=(Group&& other) noexcept;
36+
37+
~Group() override;
38+
2539
std::string getValue() const override;
2640

2741
/**
@@ -32,6 +46,10 @@ namespace XdgUtils {
3246

3347
void write(std::ostream& output) const override;
3448

49+
Node* clone() const override;
50+
51+
std::vector<std::shared_ptr<Node>>& getEntries();
52+
3553
const std::vector<std::shared_ptr<Node>>& getEntries() const;
3654

3755
void setEntries(const std::vector<std::shared_ptr<Node>>& entries);

0 commit comments

Comments
 (0)