Skip to content
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

Merged
merged 1 commit into from
Dec 1, 2021
Merged

Conversation

Marmare314
Copy link
Contributor

Since I am new to the Xournal++ code I thought I could get started by doing some refactoring :)

  • ClipboardHandler
    • replaced GList by std::set because it is sorted
    • added some static_cast's to make clang-tidy happy
  • RegionSelect
    • replaced GList by std::vector since only push_back is needed
    • reserve has not been called

@Marmare314
Copy link
Contributor Author

Unfortunately I can't seem to figure out how to install clang-format-12 on Linux Mint 20.
And apparently clang-format-10 with 'AlignOperands: true' yields a different result.

@rolandlo
Copy link
Member

Unfortunately I can't seem to figure out how to install clang-format-12 on Linux Mint 20.
And apparently clang-format-10 with 'AlignOperands: true' yields a different result.

I fixed it. Do you have clang-format-11 on Linux Mint 20? That may be enough.

@Marmare314
Copy link
Contributor Author

It does! clang-format-11 seems to work fine while git-clang-format-11 complains about the AlignOperators Argument.
But I guess that's good enough.

@Febbe
Copy link
Collaborator

Febbe commented Apr 18, 2021

you can install the newest (not-stable) version, which brings meta-packages from here: https://apt.llvm.org/

git-clang-format-11 complains about the AlignOperators Argument

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.

@Febbe Febbe added this to the Future major version milestone Apr 18, 2021
@Marmare314
Copy link
Contributor Author

I got clang-format-12 running now, however I had no luck with the meta-package.
But git-clang-format --binary clang-format-12 works well.

Also: If I do some similar refactoring, should I create a new pull request, or push it to this one?

@rolandlo
Copy link
Member

I got clang-format-12 running now, however I had no luck with the meta-package.
But git-clang-format --binary clang-format-12 works well.

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.

@Marmare314
Copy link
Contributor Author

I think that should be everything worth replacing.

Copy link
Collaborator

@Febbe Febbe left a 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

Comment on lines 63 to 68
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;
}
Copy link
Collaborator

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);
}

@@ -137,23 +138,22 @@ auto ClipboardHandler::copy() -> bool {
// prepare text contents
/////////////////////////////////////////////////////////////////

GList* textElements = nullptr;
std::multiset<Element*, decltype(&ElementCompareFunc)> textElements(ElementCompareFunc);
Copy link
Collaborator

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:

Suggested change
std::multiset<Element*, decltype(&ElementCompareFunc)> textElements(ElementCompareFunc);
std::multiset<Text*, decltype(&ElementCompareFunc)> textElements(ElementCompareFunc);

Comment on lines 150 to 151
for (Element* l: textElements) {
Text* e = dynamic_cast<Text*>(l);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (Element* l: textElements) {
Text* e = dynamic_cast<Text*>(l);
for (Text* e: textElements) {


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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
textElements.insert(e);
textElements.insert(static_cast<Text*>(e));

Comment on lines 169 to 170
if (ax > p.x) {
ax = p.x;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (ax > p.x) {
ax = p.x;
ax = std::min(ax, p.x);

Comment on lines 28 to 29
XmlNode(const XmlNode& node) = delete;
void operator=(const XmlNode& node) = delete;
Copy link
Collaborator

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.

Comment on lines 24 to 25
XmlPointNode(const XmlPointNode& node) = delete;
void operator=(const XmlPointNode& node) = delete;
Copy link
Collaborator

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.

Comment on lines 17 to 18
for (auto pointIter = points.begin(); pointIter != points.end(); pointIter++) {
if (pointIter != points.begin()) {
Copy link
Collaborator

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.

}

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); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void XmlPointNode::addPoint(const Point* point) { points.emplace_back(*point); }
void XmlPointNode::addPoint(Point point) { points.emplace_back(std::move(point)); }


auto tmpfn = (fs::path(filepath) += ".") += img->getFilepath();
if (!gdk_pixbuf_save(img->getPixbuf(), tmpfn.u8string().c_str(), "png", nullptr, nullptr)) {
for (BackgroundImage& img: backgroundImages) {
Copy link
Collaborator

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&:

Suggested change
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Contributor Author

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();
Copy link
Collaborator

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&

Copy link
Contributor Author

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.

};
// TODO: replace with <=> operator
static int cmp(const PageLayerPosEntry<T>& a, const PageLayerPosEntry<T>& b) { return a.pos - b.pos; }
};
Copy link
Collaborator

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 ;)

Comment on lines 25 to 26
// TODO: replace with <=> operator
static int cmp(const PageLayerPosEntry<T>& a, const PageLayerPosEntry<T>& b) { return a.pos - b.pos; }
Copy link
Collaborator

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

Comment on lines 18 to 19
// TODO: constructor could be removed with C++20
PageLayerPosEntry<T>(Layer* layer, T* element, int pos): layer(layer), element(element), pos(pos) {}
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

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
Copy link
Collaborator

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 :)

Comment on lines 35 to 36
std::multiset<PageLayerPosEntry<Element>, decltype(&PageLayerPosEntry<Element>::cmp)> elements{
PageLayerPosEntry<Element>::cmp};
Copy link
Collaborator

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)

Suggested change
std::multiset<PageLayerPosEntry<Element>, decltype(&PageLayerPosEntry<Element>::cmp)> elements{
PageLayerPosEntry<Element>::cmp};
std::multiset<PageLayerPosEntry<Element>> elements{};

Comment on lines 35 to 36
std::multiset<PageLayerPosEntry<Element>, decltype(&PageLayerPosEntry<Element>::cmp)> elements{
PageLayerPosEntry<Element>::cmp};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

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>()));
Copy link
Collaborator

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.

Suggested change
auto& newStroke = strokeList.emplace_back(std::move(std::make_unique<Stroke>()));
auto& newStroke = strokeList.emplace_back(std::make_unique<Stroke>());

GList* points = p->getPoints();
if (g_list_length(points) < 2) {
for (EraseableStrokePart& part: parts) {
std::vector<Point>& points = part.getPoints();
Copy link
Collaborator

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 &

Suggested change
std::vector<Point>& points = part.getPoints();
auto const& points = part.getPoints();

@Febbe
Copy link
Collaborator

Febbe commented Apr 30, 2021

remove po/xournalpp.pot from the commit history

@arvryna
Copy link

arvryna commented Jun 8, 2021

@Marmare314 : There are some files you did not change for example:

PopplerGlibPage.cpp,
ToolbarCustomizeDialog.cpp,
MainWindow.cpp,
RecentManager.cpp
DeviceListHelper.cpp

In these place, is there any specific reason why you avoided it ? or is it not worth changing there ?
If you don't want to work on them, maybe i will it up.

@Marmare314
Copy link
Contributor Author

@Marmare314 : There are some files you did not change for example:

PopplerGlibPage.cpp,
ToolbarCustomizeDialog.cpp,
MainWindow.cpp,
RecentManager.cpp
DeviceListHelper.cpp

In these place, is there any specific reason why you avoided it ? or is it not worth changing there ?
If you don't want to work on them, maybe i will it up.

  • PopplerGlibPage: its not worth replacing it as poppler_page_find_text returns a glist an it is only iterated over once
  • MainWindow, DeviceListHelper, ToolbarCustomizeDialog: same thing here
  • RecentManager: i forgot about this one

It will be a while before I will have time to replace it, so feel free to do it.

@Febbe
Copy link
Collaborator

Febbe commented Jun 9, 2021

With c++20, we will get std::ranges and Sentinels, this will make it simple, to just create a
template <GListLike C> GListView;, without wrapping it. Then we can also get rid of some copies.

@rolandlo rolandlo mentioned this pull request Jun 26, 2021
30 tasks
@Febbe Febbe modified the milestones: Future major version, 1.2 Jul 19, 2021
@rolandlo
Copy link
Member

This PR should be rebased on current master, while solving the merge conflicts.

Febbe pushed a commit to Marmare314/xournalpp that referenced this pull request Dec 1, 2021
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
@Febbe
Copy link
Collaborator

Febbe commented Dec 1, 2021

Rebased on current master

Febbe pushed a commit to Febbe/xournalpp that referenced this pull request Dec 1, 2021
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
@Febbe Febbe merged commit 0156eb6 into xournalpp:master Dec 1, 2021
@Febbe
Copy link
Collaborator

Febbe commented Dec 1, 2021

Thank you for the PR. I finally got time to merge it.

Febbe pushed a commit to Febbe/xournalpp that referenced this pull request Jan 22, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants