Skip to content

Commit

Permalink
Add pImpl to libdnf5::comps::GroupQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
kontura committed Feb 28, 2024
1 parent 484ba32 commit 2eddd55
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 29 deletions.
44 changes: 17 additions & 27 deletions include/libdnf5/comps/group/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "libdnf5/base/base_weak.hpp"
#include "libdnf5/common/sack/query.hpp"
#include "libdnf5/common/weak_ptr.hpp"
#include "libdnf5/comps/group/group.hpp"

#include <string>
Expand All @@ -32,24 +31,25 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
namespace libdnf5::comps {



class GroupQuery : public libdnf5::sack::Query<Group> {
public:
// Create new query with newly composed groups (using only solvables from currently enabled repositories)
explicit GroupQuery(const libdnf5::BaseWeakPtr & base, bool empty = false);
explicit GroupQuery(libdnf5::Base & base, bool empty = false);

void filter_groupid(const std::string & pattern, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ) {
filter(F::groupid, pattern, cmp);
}
~GroupQuery();

GroupQuery(const GroupQuery & src);
GroupQuery & operator=(const GroupQuery & src);

GroupQuery(GroupQuery && src) noexcept;
GroupQuery & operator=(GroupQuery && src) noexcept;

void filter_groupid(const std::vector<std::string> & patterns, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ) {
filter(F::groupid, patterns, cmp);
}
void filter_groupid(const std::string & pattern, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ);

void filter_name(const std::string & pattern, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ) {
filter(F::name, pattern, cmp);
}
void filter_groupid(const std::vector<std::string> & patterns, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ);

void filter_name(const std::string & pattern, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ);

/// Filter groups by packages they contain. Keep only groups that contain packages with given names.
///
Expand All @@ -59,26 +59,16 @@ class GroupQuery : public libdnf5::sack::Query<Group> {
void filter_package_name(
const std::vector<std::string> & patterns, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ);

void filter_name(const std::vector<std::string> & patterns, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ) {
filter(F::name, patterns, cmp);
}
void filter_name(const std::vector<std::string> & patterns, sack::QueryCmp cmp = libdnf5::sack::QueryCmp::EQ);

void filter_uservisible(bool value) { filter(F::is_uservisible, value, sack::QueryCmp::EQ); }
void filter_default(bool value) { filter(F::is_default, value, sack::QueryCmp::EQ); }
void filter_installed(bool value) { filter(F::is_installed, value, sack::QueryCmp::EQ); }
void filter_uservisible(bool value);
void filter_default(bool value);
void filter_installed(bool value);

private:
struct F {
static std::string groupid(const Group & obj) { return obj.get_groupid(); }
static std::string name(const Group & obj) { return obj.get_name(); }
static bool is_uservisible(const Group & obj) { return obj.get_uservisible(); }
static bool is_default(const Group & obj) { return obj.get_default(); }
static bool is_installed(const Group & obj) { return obj.get_installed(); }
};

libdnf5::BaseWeakPtr base;

friend Group;
class Impl;
std::unique_ptr<Impl> p_impl;
};


Expand Down
65 changes: 63 additions & 2 deletions libdnf5/comps/group/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "solv/pool.hpp"

#include "libdnf5/base/base.hpp"
#include "libdnf5/common/sack/match_string.hpp"
#include "libdnf5/comps/group/group.hpp"

extern "C" {
Expand All @@ -38,8 +37,25 @@ extern "C" {

namespace libdnf5::comps {

class GroupQuery::Impl {
public:
explicit Impl(const libdnf5::BaseWeakPtr & base) : base(base) {}

GroupQuery::GroupQuery(const BaseWeakPtr & base, bool empty) : base(base) {
private:
friend GroupQuery;

struct F {
static std::string groupid(const Group & obj) { return obj.get_groupid(); }
static std::string name(const Group & obj) { return obj.get_name(); }
static bool is_uservisible(const Group & obj) { return obj.get_uservisible(); }
static bool is_default(const Group & obj) { return obj.get_default(); }
static bool is_installed(const Group & obj) { return obj.get_installed(); }
};

libdnf5::BaseWeakPtr base;
};

GroupQuery::GroupQuery(const BaseWeakPtr & base, bool empty) : p_impl(std::make_unique<Impl>(base)) {
if (empty) {
return;
}
Expand Down Expand Up @@ -101,6 +117,25 @@ GroupQuery::GroupQuery(const BaseWeakPtr & base, bool empty) : base(base) {

GroupQuery::GroupQuery(libdnf5::Base & base, bool empty) : GroupQuery(base.get_weak_ptr(), empty) {}

GroupQuery::~GroupQuery() = default;

GroupQuery::GroupQuery(const GroupQuery & src) : libdnf5::sack::Query<Group>(src), p_impl(new Impl(*src.p_impl)) {}
GroupQuery::GroupQuery(GroupQuery && src) noexcept = default;

GroupQuery & GroupQuery::operator=(const GroupQuery & src) {
libdnf5::sack::Query<Group>::operator=(src);
if (this != &src) {
if (p_impl) {
*p_impl = *src.p_impl;
} else {
p_impl = std::make_unique<Impl>(*src.p_impl);
}
}

return *this;
}
GroupQuery & GroupQuery::operator=(GroupQuery && src) noexcept = default;

void GroupQuery::filter_package_name(const std::vector<std::string> & patterns, sack::QueryCmp cmp) {
for (auto it = get_data().begin(); it != get_data().end();) {
// Copy group so we can call `get_packages()`, this is needed because `it` is from a std::set and thus const
Expand All @@ -116,4 +151,30 @@ void GroupQuery::filter_package_name(const std::vector<std::string> & patterns,
}
}

void GroupQuery::filter_groupid(const std::string & pattern, sack::QueryCmp cmp) {
filter(Impl::F::groupid, pattern, cmp);
}

void GroupQuery::filter_groupid(const std::vector<std::string> & patterns, sack::QueryCmp cmp) {
filter(Impl::F::groupid, patterns, cmp);
}

void GroupQuery::filter_name(const std::string & pattern, sack::QueryCmp cmp) {
filter(Impl::F::name, pattern, cmp);
}

void GroupQuery::filter_name(const std::vector<std::string> & patterns, sack::QueryCmp cmp) {
filter(Impl::F::name, patterns, cmp);
}

void GroupQuery::filter_uservisible(bool value) {
filter(Impl::F::is_uservisible, value, sack::QueryCmp::EQ);
}
void GroupQuery::filter_default(bool value) {
filter(Impl::F::is_default, value, sack::QueryCmp::EQ);
}
void GroupQuery::filter_installed(bool value) {
filter(Impl::F::is_installed, value, sack::QueryCmp::EQ);
}

} // namespace libdnf5::comps

0 comments on commit 2eddd55

Please sign in to comment.