Skip to content

Add iterator to pini #4

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

Merged
merged 3 commits into from
Jul 12, 2021
Merged

Add iterator to pini #4

merged 3 commits into from
Jul 12, 2021

Conversation

NikolaJelic
Copy link
Collaborator

Added:

  • iterator to the pini class that uses the iterator built into std::unordered_map
  • wrapper functions for std::unordered_map's empty(), size() clear() and erase()

@NikolaJelic NikolaJelic self-assigned this Jul 10, 2021
@@ -16,6 +17,13 @@ class pini {
bool load_file(std::filesystem::path const& filename);
bool load_text(std::string_view text);

// iteration

using const_iterator = std::unordered_map<std::string, std::string>::const_iterator;
Copy link
Member

Choose a reason for hiding this comment

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

Just curious: any reason you don't want to alias the map type itself, and instead write down that long type name every time?

Copy link
Member

Choose a reason for hiding this comment

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

I see that you do have the alias as map_type, so why not use that here?!

@@ -35,6 +43,13 @@ class pini {
static void default_callback(std::string_view msg, severity level);
inline static on_msg on_msg_t = &default_callback;

bool is_empty();
Copy link
Member

Choose a reason for hiding this comment

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

Missing const correctness

@@ -35,6 +43,13 @@ class pini {
static void default_callback(std::string_view msg, severity level);
inline static on_msg on_msg_t = &default_callback;

bool is_empty();
std::size_t size();
Copy link
Member

Choose a reason for hiding this comment

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

Missing const correctness

src/pini.cpp Outdated
@@ -89,4 +89,11 @@ void pini::default_callback(std::string_view msg, severity level) {
out << msg << '\n';
}

bool pini::is_empty() { return key_value_pairs.empty(); }
std::size_t pini::size() { return key_value_pairs.size(); }
Copy link
Member

Choose a reason for hiding this comment

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

You may want to inline is_empty() and size() (implement in header / in class definition)

if (!pin.load_file(filename)) { return 1; }
for (auto& pair : pin) { std::cout << pair.first << " " << pair.second << '\n'; }
std::cout << pin.size() << " | " << pin.is_empty();
pin.erase("def");
Copy link
Member

Choose a reason for hiding this comment

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

This needs more failure cases. Like I said before, you aren't going to see any output in automated runs.

- moved util to pn namespace,
- used callbacks instead of cerr for data converters,
- inlined size() and empty()
@NikolaJelic NikolaJelic merged commit 2113ab8 into main Jul 12, 2021
@NikolaJelic NikolaJelic deleted the lib branch July 13, 2021 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants