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 new version of AddSetting that doesn't require pre-initialized variable #316

Closed
wants to merge 7 commits into from

Conversation

emilydolson
Copy link
Collaborator

@emilydolson emilydolson commented Jun 25, 2020

As discussed in #315 . This also fixes the SettingConfig.cc and SettingCombo.cc examples, which were previously not compiling.

@codecov
Copy link

codecov bot commented Jun 25, 2020

Codecov Report

Merging #316 into master will decrease coverage by 0.64%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #316      +/-   ##
==========================================
- Coverage   84.99%   84.35%   -0.65%     
==========================================
  Files         196      198       +2     
  Lines       26846    27082     +236     
==========================================
+ Hits        22818    22845      +27     
- Misses       4028     4237     +209     
Impacted Files Coverage Δ
source/config/SettingConfig.h 7.89% <100.00%> (ø)
tests/config/SettingCombos.cc 100.00% <100.00%> (ø)
tests/config/SettingConfig.cc 100.00% <100.00%> (ø)
source/Evolve/World_select.h 89.92% <0.00%> (-0.38%) ⬇️
source/config/SettingCombos.h 1.71% <0.00%> (+1.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 07537ce...9871245. Read the comment docs.

Copy link
Member

@mmore500 mmore500 left a comment

Choose a reason for hiding this comment

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

This PR adds a streamlined version of an existing API and makes miscellaneous positive cosmetic fixes in related code

@@ -17,7 +17,7 @@ int main()
config_set.AddSetting<int>("int1") = { 1, 2, 3, 4 };
config_set.AddSetting<std::string>("string") = { "a", "b", "cde" };
config_set.AddSetting<int>("int2") = { 5 };
config_set.AddSetting<double>("double", "A double value!", "-d") = { 1.1, 2.2 };
config_set.AddSetting<double>("double", "A double value!", '-d') = { 1.1, 2.2 };
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... what does this change do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes it actually compile! The 3rd argument is a char, not a string, so it was previously failing to compile.

source/config/SettingConfig.h Show resolved Hide resolved
TEST_CASE("Test SettingConfig", "[config]")
{
emp::SettingConfig config_set;
config_set.AddSetting<int>("test_non_combo") = {5};
Copy link
Member

Choose a reason for hiding this comment

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

does the 5 have to be in {}’s ... ? I might just not quite be understanding what’s going on here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does not have to be in {}s, but they also don't hurt anything

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I had assumed they were there b/c they had to be there for some obscure reason. Might be worth considering removing just to make the code easier to read the next time someone digs thru it (and they don’t assume there’s an obscure reason they have to be there)

template <typename T>
T & AddSetting(const std::string & name,
const std::string & desc="",
const char option_flag='\0')
Copy link
Member

Choose a reason for hiding this comment

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

Should we add optional arg args_label (or remove it from the fun above?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was just trying to keep things consistent between AddSetting and AddComboSetting. I was assuming AddComboSetting is set up this way to avoid ambiguity in which version is being called (but I haven't confirmed that)

const std::string & desc="",
const char option_flag='\0')
{
emp_assert(!emp::Has(setting_map, name));
Copy link
Member

Choose a reason for hiding this comment

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

Could we default construct a T t; and then just call the fun above so we just have one impl?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The issue with that is that then we end up with dynamically allocated memory in some cases but not all. We could take care of that with a flag, but since this file is deprecated anyway I don't know if it's worth bothering

Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure we would still always end up with dynamically allocated memory? The function we're delegating to always news

auto new_ptr = emp::NewPtr<SettingInfo<T>>(name, desc, option_flag, args_label, &var);

What I'm thinking of would look like


    template <typename T>
    T & AddSetting(const std::string & name,
                   const std::string & desc="",
                   const char option_flag='\0')
    {
      T t;
      return AddSetting(name, desc, option_flag, t);
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, I didn't explain that clearly. We can't default construct a T because it's passed by reference and if we construct it here it will immediately go out of scope. The way to get around that is to dynamically allocate memory for the value inside the SettingInfo constructor. That's the additional dynamically allocated memory I was referring to.

@mmore500
Copy link
Member

@emilydolson approved and left some comments that might be worth addressing before merge

@emilydolson
Copy link
Collaborator Author

These fixes are superseded by #414

@emilydolson emilydolson closed this Mar 3, 2021
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