Skip to content

Commit

Permalink
Add pImpl to libdnf5::module::ModuleProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
kontura committed Feb 28, 2024
1 parent 7bf1633 commit 740652f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 31 deletions.
7 changes: 4 additions & 3 deletions include/libdnf5/module/module_profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class ModuleProfile {
// @replaces libdnf:module:modulemd/ModuleProfile.hpp:ctor:ModuleProfile.ModuleProfile()
ModuleProfile(const ModuleProfile & src);
ModuleProfile & operator=(const ModuleProfile & src);
ModuleProfile(ModuleProfile && src) noexcept;
ModuleProfile & operator=(ModuleProfile && src) noexcept;
~ModuleProfile();

private:
Expand All @@ -69,9 +71,8 @@ class ModuleProfile {
// @replaces libdnf:module:modulemd/ModuleProfile.hpp:ctor:ModuleProfile.ModuleProfile(ModulemdProfile * profile)
ModuleProfile(_ModulemdProfile * profile, const bool is_default);

// @replaces libdnf:module:modulemd/ModuleProfile.hpp:attribute:ModuleProfile.profile
_ModulemdProfile * profile{nullptr};
bool is_default_profile = false;
class Impl;
std::unique_ptr<Impl> p_impl;
};


Expand Down
106 changes: 78 additions & 28 deletions libdnf5/module/module_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,89 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5::module {

class ModuleProfile::Impl {
public:
Impl(_ModulemdProfile * profile, const bool is_default) : profile(profile), is_default_profile(is_default) {
g_object_ref(profile);
}

~Impl();
Impl(const Impl & src);
Impl & operator=(const Impl & src);
Impl(Impl && src) noexcept;
Impl & operator=(Impl && src) noexcept;

private:
friend ModuleProfile;

// @replaces libdnf:module:modulemd/ModuleProfile.hpp:attribute:ModuleProfile.profile
_ModulemdProfile * profile{nullptr};
bool is_default_profile = false;
};

ModuleProfile::Impl::~Impl() {
if (profile != nullptr) {
g_object_unref(profile);
}
}

ModuleProfile::Impl::Impl(const Impl & src) : profile(src.profile), is_default_profile(src.is_default_profile) {
if (profile != nullptr) {
g_object_ref(profile);
}
}

ModuleProfile::Impl & ModuleProfile::Impl::operator=(const Impl & src) {
if (this != &src) {
if (profile != nullptr) {
g_object_unref(profile);
}
profile = src.profile;
is_default_profile = src.is_default_profile;
if (profile != nullptr) {
g_object_ref(profile);
}
}
return *this;
}

ModuleProfile::Impl::Impl(Impl && src) noexcept : profile(src.profile), is_default_profile(src.is_default_profile) {
src.profile = nullptr;
}

ModuleProfile::Impl & ModuleProfile::Impl::operator=(Impl && src) noexcept {
if (this != &src) {
if (profile != nullptr) {
g_object_unref(profile);
}
profile = src.profile;
is_default_profile = src.is_default_profile;
src.profile = nullptr;
}
return *this;
}

std::string ModuleProfile::get_name() const {
if (!profile) {
if (!p_impl->profile) {
return {};
}
return libdnf5::utils::string::c_to_str(modulemd_profile_get_name(profile));
return libdnf5::utils::string::c_to_str(modulemd_profile_get_name(p_impl->profile));
}


std::string ModuleProfile::get_description() const {
if (!profile) {
if (!p_impl->profile) {
return {};
}
return libdnf5::utils::string::c_to_str(modulemd_profile_get_description(profile, NULL));
return libdnf5::utils::string::c_to_str(modulemd_profile_get_description(p_impl->profile, NULL));
}


std::vector<std::string> ModuleProfile::get_rpms() const {
if (!profile) {
if (!p_impl->profile) {
return {};
}
gchar ** c_rpms = modulemd_profile_get_rpms_as_strv(profile);
gchar ** c_rpms = modulemd_profile_get_rpms_as_strv(p_impl->profile);

std::vector<std::string> rpms;
for (gchar ** item = c_rpms; *item; ++item) {
Expand All @@ -61,44 +122,33 @@ std::vector<std::string> ModuleProfile::get_rpms() const {


bool ModuleProfile::is_default() const {
return is_default_profile;
return p_impl->is_default_profile;
}


ModuleProfile::ModuleProfile(ModulemdProfile * profile, const bool is_default)
: profile(profile),
is_default_profile(is_default) {
g_object_ref(profile);
}
: p_impl(std::make_unique<Impl>(profile, is_default)) {}


ModuleProfile::ModuleProfile(const ModuleProfile & src)
: profile(src.profile),
is_default_profile(src.is_default_profile) {
if (profile != nullptr) {
g_object_ref(profile);
}
}
ModuleProfile::ModuleProfile(const ModuleProfile & src) : p_impl(new Impl(*src.p_impl)) {}
ModuleProfile::ModuleProfile(ModuleProfile && src) noexcept = default;


ModuleProfile & ModuleProfile::operator=(const ModuleProfile & src) {
if (this != &src) {
g_object_unref(profile);
profile = src.profile;
is_default_profile = src.is_default_profile;
if (profile != nullptr) {
g_object_ref(profile);
if (p_impl) {
*p_impl = *src.p_impl;
} else {
p_impl = std::make_unique<Impl>(*src.p_impl);
}
}

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


ModuleProfile::~ModuleProfile() {
if (profile != nullptr) {
g_object_unref(profile);
}
}
ModuleProfile::~ModuleProfile() = default;


} // namespace libdnf5::module

0 comments on commit 740652f

Please sign in to comment.