MXS-2329 Make duration misuse harder

Now the desired type must be specified when getting a duration.
The type also dictates how durations without suffixes should be
interpreted.

That removes the need for remembering that to convert a returned
millisecond duration to a second duration.
This commit is contained in:
Johan Wikman
2019-04-29 10:00:44 +03:00
parent 8bf0e00b1c
commit 8a250a8b13
4 changed files with 43 additions and 10 deletions

View File

@ -339,8 +339,26 @@ public:
*
* @return Duration in milliseconds; 0 if the parameter is not found.
*/
std::chrono::milliseconds get_duration(const std::string& key,
mxs::config::DurationInterpretation interpretation) const;
std::chrono::milliseconds get_duration_in_ms(const std::string& key,
mxs::config::DurationInterpretation interpretation) const;
/**
* @brief Get a duration in a specific unit.
*
* @param key Parameter name
*
* @return The duration in the desired unit.
*
* @note The type the function is specialized with dictates how values without a
* suffix should be interpreted; if @c std::chrono::seconds, they will be
* interpreted as seconds, if @c std::chrono::milliseconds, they will be
* interpreted as milliseconds.
*
* @note There is no default implementation, but only specializations for
* @c std::chrono::seconds and @c std::chrono::milliseconds.
*/
template<class T>
T get_duration(const std::string& key) const = delete;
/**
* @brief Get a service value
@ -441,6 +459,22 @@ private:
ContainerType m_contents;
};
template<>
inline std::chrono::milliseconds
MXS_CONFIG_PARAMETER::get_duration<std::chrono::milliseconds>(const std::string& key) const
{
return get_duration_in_ms(key, mxs::config::INTERPRET_AS_MILLISECONDS);
}
template<>
inline std::chrono::seconds
MXS_CONFIG_PARAMETER::get_duration<std::chrono::seconds>(const std::string& key) const
{
std::chrono::milliseconds ms = get_duration_in_ms(key, mxs::config::INTERPRET_AS_SECONDS);
return std::chrono::duration_cast<std::chrono::seconds>(ms);
}
/**
* The config context structure, used to build the configuration
* data during the parse process