Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
Settings updates
Browse files Browse the repository at this point in the history
- push_back -> emplace_back
- settings::at
- vsettings parse callback support
  • Loading branch information
jrmadsen committed May 30, 2024
1 parent 915bfa4 commit e3c9ecf
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
12 changes: 6 additions & 6 deletions source/timemory/settings/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
INIT, std::string{ #FUNC }, std::string{ ENV_VAR }, \
std::string{ DESC }, CATEGORIES)) \
.second) \
m_order.push_back(ENV_VAR);
m_order.emplace_back(ENV_VAR);
#endif
//
//--------------------------------------------------------------------------------------//
Expand All @@ -186,7 +186,7 @@
INIT, std::string{}, std::string{ ENV_VAR }, \
std::string{ DESC }, CATEGORIES)) \
.second) \
m_order.push_back(ENV_VAR);
m_order.emplace_back(ENV_VAR);
#endif
//
//--------------------------------------------------------------------------------------//
Expand All @@ -199,7 +199,7 @@
INIT, std::string{ #FUNC }, std::string{ ENV_VAR }, \
std::string{ DESC }, __VA_ARGS__)) \
.second) \
m_order.push_back(ENV_VAR);
m_order.emplace_back(ENV_VAR);
#endif
//
//--------------------------------------------------------------------------------------//
Expand All @@ -212,7 +212,7 @@
INIT, std::string{}, std::string{ ENV_VAR }, \
std::string{ DESC }, __VA_ARGS__)) \
.second) \
m_order.push_back(ENV_VAR);
m_order.emplace_back(ENV_VAR);
#endif
//
//--------------------------------------------------------------------------------------//
Expand All @@ -226,7 +226,7 @@
INIT, std::string{ #FUNC }, std::string{ ENV_VAR }, \
std::string{ DESC }, CATEGORIES)) \
.second) \
m_order.push_back(ENV_VAR);
m_order.emplace_back(ENV_VAR);
#endif
//
//--------------------------------------------------------------------------------------//
Expand All @@ -238,7 +238,7 @@
INIT, std::string{ #FUNC }, std::string{ ENV_VAR }, \
std::string{ DESC }, __VA_ARGS__)) \
.second) \
m_order.push_back(ENV_VAR);
m_order.emplace_back(ENV_VAR);
#endif
//
//--------------------------------------------------------------------------------------//
Expand Down
2 changes: 1 addition & 1 deletion source/timemory/settings/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,7 @@ settings::read(std::istream& ifs, std::string inp)
return std::tie(key, val) ==
std::tie(itr.first, itr.second);
}))
m_unknown_configs.emplace_back(strpair_t{ key, val });
m_unknown_configs.emplace_back(key, val);
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions source/timemory/settings/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ struct TIMEMORY_VISIBILITY("default") settings
template <typename Sp = std::string>
auto find(Sp&& _key, bool _exact = true, const std::string& _category = {});

template <typename Sp = std::string>
decltype(auto) at(Sp&& _key);

template <typename Tp, typename Sp = std::string>
Tp get(Sp&& _key, bool _exact = true);

Expand Down Expand Up @@ -1010,6 +1013,16 @@ settings::find(Sp&& _key, bool _exact, const std::string& _category)
//
//--------------------------------------------------------------------------------------//
//
template <typename Sp>
inline decltype(auto)
settings::at(Sp&& _key)
{
// exact match to map key
return m_data.at(std::forward<Sp>(_key));
}
//
//--------------------------------------------------------------------------------------//
//
template <typename Tp, typename Sp>
Tp
settings::get(Sp&& _key, bool _exact)
Expand Down
7 changes: 5 additions & 2 deletions source/timemory/settings/tsettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,12 @@ tsettings<Tp, Vp>::parse()
//
template <typename Tp, typename Vp>
bool
tsettings<Tp, Vp>::parse(const std::string& v, update_type _upd)
tsettings<Tp, Vp>::parse(const std::string& val, update_type _upd)
{
return set(std::move(get_value<decay_t<Tp>>(v)), _upd);
auto ret = set(get_value<decay_t<Tp>>(val), _upd);
if(ret && m_callback)
m_callback(this, val, _upd);
return ret;
}
//
template <typename Tp, typename Vp>
Expand Down
16 changes: 16 additions & 0 deletions source/timemory/settings/vsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ vsettings::vsettings(std::string _name, std::string _env_name, std::string _desc
{}
//
TIMEMORY_SETTINGS_INLINE
vsettings::vsettings(std::string _name, std::string _env_name, std::string _descript,
parse_callback_t&& _callback, std::set<std::string> _categories,
std::vector<std::string> _cmdline, int32_t _count,
int32_t _max_count, std::vector<std::string> _choices)
: m_count{ _count }
, m_max_count{ _max_count }
, m_name{ std::move(_name) }
, m_env_name{ std::move(_env_name) }
, m_description{ std::move(_descript) }
, m_cmdline{ std::move(_cmdline) }
, m_choices{ std::move(_choices) }
, m_categories{ std::move(_categories) }
, m_callback{ std::move(_callback) }
{}
//
TIMEMORY_SETTINGS_INLINE
vsettings::vsettings(std::string _name, std::string _env_name, std::string _descript,
std::set<std::string> _categories, std::vector<std::string> _cmdline,
int32_t _count, int32_t _max_count,
Expand Down
15 changes: 15 additions & 0 deletions source/timemory/settings/vsettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct vsettings
using display_map_t = std::map<std::string, std::string>;
using shared_pointer_t = std::shared_ptr<vsettings>;
using update_type = setting_update_type;
using parse_callback_t =
std::function<void(vsettings*, std::string_view, update_type)>;

struct noparse
{};
Expand All @@ -80,6 +82,11 @@ struct vsettings
int32_t _max_count = -1, std::vector<std::string> _choices = {},
std::set<std::string> _categories = {});

vsettings(std::string _name, std::string _env_name, std::string _descript,
parse_callback_t&& _callback, std::set<std::string> _categories,
std::vector<std::string> _cmdline = {}, int32_t _count = -1,
int32_t _max_count = -1, std::vector<std::string> _choices = {});

virtual ~vsettings() = default;

vsettings(const vsettings&) = default;
Expand Down Expand Up @@ -125,6 +132,7 @@ struct vsettings
auto get_type_index() const { return m_type_index; }
auto get_value_index() const { return m_value_index; }
auto get_updated() const { return (m_updated != update_type::default_value); }
auto get_updated_type() const { return m_updated; }
auto get_user_updated() const { return (m_updated == update_type::user); }
auto get_config_updated() const { return (m_updated == update_type::config); }
auto get_environ_updated() const { return (m_updated == update_type::env); }
Expand Down Expand Up @@ -167,6 +175,12 @@ struct vsettings
return static_cast<const tsettings<decay_t<Tp>, Tp>*>(_val.get());
}

template <typename FuncT>
void set_parse_callback(FuncT&& _func)
{
m_callback = std::forward<FuncT>(_func);
}

protected:
friend struct settings;

Expand Down Expand Up @@ -199,6 +213,7 @@ struct vsettings
std::vector<std::string> m_cmdline = {};
std::vector<std::string> m_choices = {};
std::set<std::string> m_categories = {};
parse_callback_t m_callback = {};
};
//
template <typename Tp>
Expand Down

0 comments on commit e3c9ecf

Please sign in to comment.