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

Add pImpl to the remaining various classes #1377

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions include/libdnf5/conf/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ namespace libdnf5 {
/// Base class for configurations objects
class Config {
public:
OptionBinds & opt_binds() noexcept { return binds; }
OptionBinds & opt_binds() noexcept;

virtual ~Config() = default;
Config();
virtual ~Config();

virtual void load_from_parser(
const ConfigParser & parser,
Expand All @@ -45,7 +46,8 @@ class Config {
Option::Priority priority);

private:
OptionBinds binds;
class Impl;
ImplPtr<Impl> p_impl;
};


Expand Down
35 changes: 20 additions & 15 deletions include/libdnf5/conf/option_binds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class OptionBinds {
using NewStringFunc = std::function<void(Option::Priority, const std::string &)>;
using GetValueStringFunc = std::function<const std::string &()>;

~Item();
Option::Priority get_priority() const;
void new_string(Option::Priority priority, const std::string & value);
std::string get_value_string() const;
Expand All @@ -67,16 +68,19 @@ class OptionBinds {

Item(Option & option, NewStringFunc new_string_func, GetValueStringFunc get_value_string_func, bool add_value);
explicit Item(Option & option);
Option * option;
NewStringFunc new_str_func;
GetValueStringFunc get_value_str_func;
bool is_append_option{false}; // hint that new value be added/appended

class Impl;
ImplPtr<Impl> p_impl;
};

using Container = std::map<std::string, Item>;
using iterator = Container::iterator;
using const_iterator = Container::const_iterator;

OptionBinds();
OptionBinds(const OptionBinds & src);
~OptionBinds();

Item & add(
const std::string & id,
Option & option,
Expand All @@ -86,19 +90,20 @@ class OptionBinds {
Item & add(const std::string & id, Option & option);
Item & at(const std::string & id);
const Item & at(const std::string & id) const;
bool empty() const noexcept { return items.empty(); }
std::size_t size() const noexcept { return items.size(); }
iterator begin() noexcept { return items.begin(); }
const_iterator begin() const noexcept { return items.begin(); }
const_iterator cbegin() const noexcept { return items.cbegin(); }
iterator end() noexcept { return items.end(); }
const_iterator end() const noexcept { return items.end(); }
const_iterator cend() const noexcept { return items.cend(); }
iterator find(const std::string & id) { return items.find(id); }
const_iterator find(const std::string & id) const { return items.find(id); }
bool empty() const noexcept;
std::size_t size() const noexcept;
iterator begin() noexcept;
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
const_iterator cend() const noexcept;
iterator find(const std::string & id);
const_iterator find(const std::string & id) const;

private:
Container items;
class Impl;
ImplPtr<Impl> p_impl;
};

} // namespace libdnf5
Expand Down
19 changes: 12 additions & 7 deletions include/libdnf5/logger/log_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "logger.hpp"

#include "libdnf5/common/impl_ptr.hpp"

#include <memory>
#include <vector>

Expand All @@ -33,26 +35,28 @@ namespace libdnf5 {
class LogRouter : public Logger {
public:
/// Constructs a new LogRouter instance with an empty set of destination loggers.
LogRouter() = default;
LogRouter();

~LogRouter();

/// Constructs a new LogRouter instance and sets the destination loggers.
LogRouter(std::vector<std::unique_ptr<Logger>> && loggers) : loggers(std::move(loggers)) {}
LogRouter(std::vector<std::unique_ptr<Logger>> && loggers);

/// Moves (registers) the "logger" into the log router. It gets next free index number.
void add_logger(std::unique_ptr<Logger> && logger) { loggers.push_back(std::move(logger)); }
void add_logger(std::unique_ptr<Logger> && logger);

/// Returns pointer to the logger at the "index" position.
Logger * get_logger(size_t index) { return loggers.at(index).get(); }
Logger * get_logger(size_t index);

/// Removes logger at the "index" position from LogRouter.
/// The array of the loggers is squeezed. Index of the loggers behind removed logger is decreased by one.
std::unique_ptr<Logger> release_logger(size_t index);

/// Swaps the logger at the "index" position with another "logger".
void swap_logger(std::unique_ptr<Logger> & logger, size_t index) { loggers.at(index).swap(logger); }
void swap_logger(std::unique_ptr<Logger> & logger, size_t index);

/// Returns number of loggers registered in LogRouter.
size_t get_loggers_count() const noexcept { return loggers.size(); }
size_t get_loggers_count() const noexcept;

void log_line(Level level, const std::string & message) noexcept override;

Expand All @@ -63,7 +67,8 @@ class LogRouter : public Logger {
const std::string & message) noexcept override;

private:
std::vector<std::unique_ptr<Logger>> loggers;
class Impl;
ImplPtr<Impl> p_impl;
};

} // namespace libdnf5
Expand Down
13 changes: 5 additions & 8 deletions include/libdnf5/logger/memory_buffer_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "logger.hpp"

#include <mutex>
#include <vector>

#include "libdnf5/common/impl_ptr.hpp"

namespace libdnf5 {

Expand All @@ -40,23 +38,22 @@ class MemoryBufferLogger : public Logger {
};

explicit MemoryBufferLogger(std::size_t max_items_to_keep, std::size_t reserve = 0);
~MemoryBufferLogger();

void write(
const std::chrono::time_point<std::chrono::system_clock> & time,
pid_t pid,
Level level,
const std::string & message) noexcept override;

std::size_t get_items_count() const { return items.size(); }
std::size_t get_items_count() const;
const Item & get_item(std::size_t item_idx) const;
void clear() noexcept;
void write_to_logger(Logger & logger);

private:
mutable std::mutex items_mutex;
std::size_t max_items; // rotation, oldest messages are replaced
std::size_t first_item_idx;
std::vector<Item> items;
class Impl;
ImplPtr<Impl> p_impl;
};

} // namespace libdnf5
Expand Down
18 changes: 16 additions & 2 deletions libdnf5/conf/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5 {

class Config::Impl {
private:
friend Config;

OptionBinds binds;
};

OptionBinds & Config::opt_binds() noexcept {
return p_impl->binds;
}

Config::~Config() = default;
Config::Config() : p_impl(new Impl()) {}

void Config::load_from_parser(
const ConfigParser & parser,
const std::string & section,
Expand All @@ -31,8 +45,8 @@ void Config::load_from_parser(
auto cfg_parser_data_iter = parser.get_data().find(section);
if (cfg_parser_data_iter != parser.get_data().end()) {
for (const auto & opt : cfg_parser_data_iter->second) {
auto opt_binds_iter = binds.find(opt.first);
if (opt_binds_iter != binds.end()) {
auto opt_binds_iter = p_impl->binds.find(opt.first);
if (opt_binds_iter != p_impl->binds.end()) {
try {
opt_binds_iter->second.new_string(priority, vars.substitute(opt.second));
} catch (const OptionError & ex) {
Expand Down
104 changes: 81 additions & 23 deletions libdnf5/conf/option_binds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,79 @@ OptionBindsOptionAlreadyExistsError::OptionBindsOptionAlreadyExistsError(const s
: OptionBindsError(M_("Option \"{}\" already exists"), id) {}

// ========== OptionBinds::Item class ===============
class OptionBinds::Item::Impl {
public:
Impl(Option & option, NewStringFunc && new_string_func, GetValueStringFunc && get_value_string_func, bool add_value)
: option(&option),
new_str_func(std::move(new_string_func)),
get_value_str_func(std::move(get_value_string_func)),
is_append_option(add_value) {}

Impl(Option & option) : option(&option){};

private:
friend OptionBinds::Item;
Option * option;
NewStringFunc new_str_func;
GetValueStringFunc get_value_str_func;
bool is_append_option{false}; // hint that new value be added/appended
};

OptionBinds::Item::Item(
Option & option, NewStringFunc new_string_func, GetValueStringFunc get_value_string_func, bool add_value)
: option(&option),
new_str_func(std::move(new_string_func)),
get_value_str_func(std::move(get_value_string_func)),
is_append_option(add_value) {}
: p_impl(new Impl(option, std::move(new_string_func), std::move(get_value_string_func), add_value)) {}

OptionBinds::Item::Item(Option & option) : option(&option) {}
OptionBinds::Item::Item(Option & option) : p_impl(new Impl(option)) {}

OptionBinds::Item::~Item() = default;

Option::Priority OptionBinds::Item::get_priority() const {
return option->get_priority();
return p_impl->option->get_priority();
}

void OptionBinds::Item::new_string(Option::Priority priority, const std::string & value) {
if (new_str_func) {
new_str_func(priority, value);
if (p_impl->new_str_func) {
p_impl->new_str_func(priority, value);
} else {
option->set(priority, value);
p_impl->option->set(priority, value);
}
}

std::string OptionBinds::Item::get_value_string() const {
if (get_value_str_func) {
return get_value_str_func();
if (p_impl->get_value_str_func) {
return p_impl->get_value_str_func();
}
return option->get_value_string();
return p_impl->option->get_value_string();
}

bool OptionBinds::Item::get_is_append_option() const {
return is_append_option;
return p_impl->is_append_option;
}


// =========== OptionBinds class ===============
class OptionBinds::Impl {
private:
friend OptionBinds;

Container items;
};

OptionBinds::OptionBinds() : p_impl(new Impl()) {}
OptionBinds::OptionBinds(const OptionBinds & src) = default;
OptionBinds::~OptionBinds() = default;

OptionBinds::Item & OptionBinds::at(const std::string & id) {
auto item = items.find(id);
if (item == items.end()) {
auto item = p_impl->items.find(id);
if (item == p_impl->items.end()) {
throw OptionBindsOptionNotFoundError(id);
}
return item->second;
}

const OptionBinds::Item & OptionBinds::at(const std::string & id) const {
auto item = items.find(id);
if (item == items.end()) {
auto item = p_impl->items.find(id);
if (item == p_impl->items.end()) {
throw OptionBindsOptionNotFoundError(id);
}
return item->second;
Expand All @@ -90,21 +116,53 @@ OptionBinds::Item & OptionBinds::add(
Item::NewStringFunc new_string_func,
Item::GetValueStringFunc get_value_string_func,
bool add_value) {
auto item = items.find(id);
if (item != items.end()) {
auto item = p_impl->items.find(id);
if (item != p_impl->items.end()) {
throw OptionBindsOptionAlreadyExistsError(id);
}
auto res = items.emplace(id, Item(option, std::move(new_string_func), std::move(get_value_string_func), add_value));
auto res = p_impl->items.emplace(
id, Item(option, std::move(new_string_func), std::move(get_value_string_func), add_value));
return res.first->second;
}

OptionBinds::Item & OptionBinds::add(const std::string & id, Option & option) {
auto item = items.find(id);
if (item != items.end()) {
auto item = p_impl->items.find(id);
if (item != p_impl->items.end()) {
throw OptionBindsOptionAlreadyExistsError(id);
}
auto res = items.emplace(id, Item(option));
auto res = p_impl->items.emplace(id, Item(option));
return res.first->second;
}

bool OptionBinds::empty() const noexcept {
return p_impl->items.empty();
}
std::size_t OptionBinds::size() const noexcept {
return p_impl->items.size();
}
OptionBinds::iterator OptionBinds::begin() noexcept {
return p_impl->items.begin();
}
OptionBinds::const_iterator OptionBinds::begin() const noexcept {
return p_impl->items.begin();
}
OptionBinds::const_iterator OptionBinds::cbegin() const noexcept {
return p_impl->items.cbegin();
}
OptionBinds::iterator OptionBinds::end() noexcept {
return p_impl->items.end();
}
OptionBinds::const_iterator OptionBinds::end() const noexcept {
return p_impl->items.end();
}
OptionBinds::const_iterator OptionBinds::cend() const noexcept {
return p_impl->items.cend();
}
OptionBinds::iterator OptionBinds::find(const std::string & id) {
return p_impl->items.find(id);
}
OptionBinds::const_iterator OptionBinds::find(const std::string & id) const {
return p_impl->items.find(id);
}

} // namespace libdnf5
Loading
Loading