-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace some GList with STL structs #3042
Conversation
Unfortunately I can't seem to figure out how to install clang-format-12 on Linux Mint 20. |
I fixed it. Do you have |
It does! clang-format-11 seems to work fine while git-clang-format-11 complains about the AlignOperators Argument. |
you can install the newest (not-stable) version, which brings meta-packages from here: https://apt.llvm.org/
There is a bug (in my opinion); git-clang-format-11 calls the meta-package clang-format, instead of clang-format-11. When you use the canonical provided meta package, you'll have clang-format-10 installed. so git-clang-format-11 will call clang-format which calls clang-format-10. Installing the llvm provided meta-package will fix everything. |
I got clang-format-12 running now, however I had no luck with the meta-package. Also: If I do some similar refactoring, should I create a new pull request, or push it to this one? |
I suggest you push it to this one. Just squash the commits which belong together and keep commits apart, if they belong to different refactoring steps. |
I think that should be everything worth replacing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, a bulk of change requests, thank you for the PR
src/control/ClipboardHandler.cpp
Outdated
auto ElementCompareFunc(Element* a, Element* b) -> bool { | ||
if (a->getY() == b->getY()) { | ||
return a->getX() - b->getX(); | ||
return (a->getX() - b->getX()) > 0; | ||
} | ||
return a->getY() - b->getY(); | ||
return (a->getY() - b->getY()) > 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either use the less operator here, or create a comparator object with an adequate name like
constexpr auto operator < (Element *lhs, Element *rhs) -> bool{
return std::tie(lhs->y, lhs->x) > std::tie(rhs->y, rhs->x);
}
src/control/ClipboardHandler.cpp
Outdated
@@ -137,23 +138,22 @@ auto ClipboardHandler::copy() -> bool { | |||
// prepare text contents | |||
///////////////////////////////////////////////////////////////// | |||
|
|||
GList* textElements = nullptr; | |||
std::multiset<Element*, decltype(&ElementCompareFunc)> textElements(ElementCompareFunc); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know, all objects of the set will be Text*
so make them one:
std::multiset<Element*, decltype(&ElementCompareFunc)> textElements(ElementCompareFunc); | |
std::multiset<Text*, decltype(&ElementCompareFunc)> textElements(ElementCompareFunc); |
src/control/ClipboardHandler.cpp
Outdated
for (Element* l: textElements) { | ||
Text* e = dynamic_cast<Text*>(l); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (Element* l: textElements) { | |
Text* e = dynamic_cast<Text*>(l); | |
for (Text* e: textElements) { |
src/control/ClipboardHandler.cpp
Outdated
|
||
for (Element* e: *this->selection->getElements()) { | ||
if (e->getType() == ELEMENT_TEXT) { | ||
textElements = g_list_insert_sorted(textElements, e, reinterpret_cast<GCompareFunc>(ElementCompareFunc)); | ||
textElements.insert(e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
textElements.insert(e); | |
textElements.insert(static_cast<Text*>(e)); |
src/control/tools/Selection.cpp
Outdated
if (ax > p.x) { | ||
ax = p.x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (ax > p.x) { | |
ax = p.x; | |
ax = std::min(ax, p.x); |
src/control/xml/XmlNode.h
Outdated
XmlNode(const XmlNode& node) = delete; | ||
void operator=(const XmlNode& node) = delete; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are using std::unique_ptr
those are deleted anyways. But defining them as deleted requires to fulfill the rule of 5. So best would be to remove them. This will implicitly allow to move the object.
src/control/xml/XmlPointNode.h
Outdated
XmlPointNode(const XmlPointNode& node) = delete; | ||
void operator=(const XmlPointNode& node) = delete; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, remove or fulfill the rule of 5.
src/control/xml/XmlPointNode.cpp
Outdated
for (auto pointIter = points.begin(); pointIter != points.end(); pointIter++) { | ||
if (pointIter != points.begin()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move the condition out of the loop, also prepend the ++ (increment) in c++, because val++ will create a copy of val before increment. And not every compiler is capable to remove the extra code.
src/control/xml/XmlPointNode.cpp
Outdated
} | ||
|
||
void XmlPointNode::addPoint(const Point* point) { this->points = g_list_append(this->points, new Point(*point)); } | ||
void XmlPointNode::addPoint(const Point* point) { points.emplace_back(*point); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void XmlPointNode::addPoint(const Point* point) { points.emplace_back(*point); } | |
void XmlPointNode::addPoint(Point point) { points.emplace_back(std::move(point)); } |
src/control/xojfile/SaveHandler.cpp
Outdated
|
||
auto tmpfn = (fs::path(filepath) += ".") += img->getFilepath(); | ||
if (!gdk_pixbuf_save(img->getPixbuf(), tmpfn.u8string().c_str(), "png", nullptr, nullptr)) { | ||
for (BackgroundImage& img: backgroundImages) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
img is only used as const&:
for (BackgroundImage& img: backgroundImages) { | |
for (BackgroundImage const& img: backgroundImages) { |
|
||
this->points = g_list_delete_link(this->points, link); | ||
} | ||
points.erase(points.begin() + 1, points.end() - 1); // deletes everything but endpoints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
points.erase(points.begin() + 1, points.end() - 1); // deletes everything but endpoints | |
assert(points.size() > 1); | |
points.erase(points.begin() + 1, points.end() - 1); // deletes everything but endpoints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should assert
from <cassert>
be used or g_assert
?
|
||
public: | ||
void addPoint(Point p); | ||
double getWidth() const; | ||
|
||
GList* getPoints(); | ||
std::vector<Point>& getPoints(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check, if this function can be const
and if you can return a const&
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In EraseableStroke::erasePart points are erased. A const
version could be added though.
src/undo/PageLayerPosEntry.h
Outdated
}; | ||
// TODO: replace with <=> operator | ||
static int cmp(const PageLayerPosEntry<T>& a, const PageLayerPosEntry<T>& b) { return a.pos - b.pos; } | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re add the line break ;)
src/undo/PageLayerPosEntry.h
Outdated
// TODO: replace with <=> operator | ||
static int cmp(const PageLayerPosEntry<T>& a, const PageLayerPosEntry<T>& b) { return a.pos - b.pos; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also looking forward to c++20 👀 .
I think this function can be constexpr.
Also, I would implement the less operator
src/undo/PageLayerPosEntry.h
Outdated
// TODO: constructor could be removed with C++20 | ||
PageLayerPosEntry<T>(Layer* layer, T* element, int pos): layer(layer), element(element), pos(pos) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure? I think this is already replaceable since c++14 or at least c++17.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least vector::emplace
does not support it yet.
But I think this is cleaner than dealing with it in the files using it.
src/undo/EraseUndoAction.cpp
Outdated
EraseableStroke* e = entryIter->element->getEraseable(); | ||
std::vector<std::unique_ptr<Stroke>> strokeList = e->getStroke(entryIter->element); | ||
for (auto& stroke: strokeList) { | ||
// TODO (Marmare314): should use unique_ptr in layer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the todo :); Yes that is a refactoring, which should go into another pr :)
src/undo/DeleteUndoAction.h
Outdated
std::multiset<PageLayerPosEntry<Element>, decltype(&PageLayerPosEntry<Element>::cmp)> elements{ | ||
PageLayerPosEntry<Element>::cmp}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like below this violates the Compare Requirement. (Reference below)
std::multiset<PageLayerPosEntry<Element>, decltype(&PageLayerPosEntry<Element>::cmp)> elements{ | |
PageLayerPosEntry<Element>::cmp}; | |
std::multiset<PageLayerPosEntry<Element>> elements{}; |
src/undo/AddUndoAction.h
Outdated
std::multiset<PageLayerPosEntry<Element>, decltype(&PageLayerPosEntry<Element>::cmp)> elements{ | ||
PageLayerPosEntry<Element>::cmp}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
src/model/eraser/EraseableStroke.cpp
Outdated
s->setLineStyle(original->getLineStyle()); | ||
s->setWidth(original->getWidth()); | ||
list = g_list_append(list, s); | ||
auto& newStroke = strokeList.emplace_back(std::move(std::make_unique<Stroke>())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::make_unique<Stroke>()
is already a rvalue-reference, no need to call move here.
auto& newStroke = strokeList.emplace_back(std::move(std::make_unique<Stroke>())); | |
auto& newStroke = strokeList.emplace_back(std::make_unique<Stroke>()); |
src/model/eraser/EraseableStroke.cpp
Outdated
GList* points = p->getPoints(); | ||
if (g_list_length(points) < 2) { | ||
for (EraseableStrokePart& part: parts) { | ||
std::vector<Point>& points = part.getPoints(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
points
is only used as const &
std::vector<Point>& points = part.getPoints(); | |
auto const& points = part.getPoints(); |
remove |
@Marmare314 : There are some files you did not change for example: PopplerGlibPage.cpp, In these place, is there any specific reason why you avoided it ? or is it not worth changing there ? |
It will be a while before I will have time to replace it, so feel free to do it. |
With c++20, we will get std::ranges and Sentinels, this will make it simple, to just create a |
This PR should be rebased on current master, while solving the merge conflicts. |
refactor ClipboardHandler, RegionSelect refactor XmlNode replace glist in SaveHandler replace glist in FormatDialog + clang-format refactor EraseableStrokePart refactor EraseableStroke refactor EraseableStroke::getStroke fix everything except clipboardhandler and commented requests
Rebased on current master |
refactor ClipboardHandler, RegionSelect refactor XmlNode replace glist in SaveHandler replace glist in FormatDialog + clang-format refactor EraseableStrokePart refactor EraseableStroke refactor EraseableStroke::getStroke fix everything except clipboardhandler and commented requests
refactor ClipboardHandler, RegionSelect refactor XmlNode replace glist in SaveHandler replace glist in FormatDialog + clang-format refactor EraseableStrokePart refactor EraseableStroke refactor EraseableStroke::getStroke fix everything except clipboardhandler and commented requests
Thank you for the PR. I finally got time to merge it. |
refactor ClipboardHandler, RegionSelect refactor XmlNode replace glist in SaveHandler replace glist in FormatDialog + clang-format refactor EraseableStrokePart refactor EraseableStroke refactor EraseableStroke::getStroke fix everything except clipboardhandler and commented requests
Since I am new to the Xournal++ code I thought I could get started by doing some refactoring :)