format: allow implicit conversion from formats to strings

This commit is contained in:
Danny Robson 2018-11-29 13:10:02 +11:00
parent c7cc0dbf4c
commit ce3e3d83d3

View File

@ -104,17 +104,32 @@ namespace cruft::format {
/// lifetime of the return value.
template <typename ...Args>
bound<Args...>
operator () (const Args &...args) &;
operator() (const Args &...args) &;
/// records a complete collection of parameters for rendering in the
/// future. takes ownership of the specifiers so there is no lifetime
/// requirement.
template <typename ...Args>
stored<Args...>
operator () (const Args &...args) &&;
operator() (const Args &...args) &&;
};
template <typename... ValueT>
class bound;
template <typename ...Args>
std::string
to_string (const bound<Args...>&);
template <typename... ValueT>
class stored;
template <typename ...Args>
std::string
to_string (const bound<Args...>&);
/// parameter collection for a non-owning sequence of specifiers
template <typename ...ValueT>
class bound {
@ -124,13 +139,21 @@ namespace cruft::format {
m_values {args...}
{ ; }
auto specifiers (void) const
{ return view (m_parsed.m_specifiers); }
template <size_t Index>
auto
get (void) const& { return std::get<Index> (m_values); }
operator ::std::string () const
{
return to_string (*this);
}
private:
const parsed &m_parsed;
std::tuple<const ValueT&...> m_values;
@ -146,16 +169,24 @@ namespace cruft::format {
m_values {args...}
{ ; }
auto
specifiers (void) const&
{
return view {m_specifiers};
}
template <size_t Index>
const auto&
get (void) const& { return std::get<Index> (m_values); }
operator ::std::string () const
{
return to_string (*this);
}
private:
std::vector<specifier> m_specifiers;
std::tuple<const ValueT&...> m_values;
@ -164,13 +195,13 @@ namespace cruft::format {
template <typename ...Args>
bound<Args...>
parsed::operator () (const Args &...args) &
parsed::operator() (const Args &...args) &
{ return bound { *this, args... }; }
template <typename ...Args>
stored<Args...>
parsed::operator () (const Args &...args) &&
parsed::operator() (const Args &...args) &&
{
return stored { std::move (m_specifiers), args... };
}