format: add invalid_specifier exception
This commit is contained in:
parent
61ba8e5186
commit
df9d0ce432
@ -17,10 +17,3 @@
|
||||
#include "format.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
//std::string
|
||||
//util::format (std::string &&fmt)
|
||||
//{
|
||||
// return std::move (fmt);
|
||||
//}
|
||||
|
15
format.hpp
15
format.hpp
@ -11,7 +11,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2015-2016 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_FORMAT_HPP
|
||||
@ -37,6 +37,19 @@ namespace util {
|
||||
class format_error : public error
|
||||
{ using error::error; };
|
||||
|
||||
template <typename ValueT>
|
||||
class invalid_specifier : public format_error {
|
||||
public:
|
||||
using value_type = ValueT;
|
||||
|
||||
invalid_specifier (char specifier);
|
||||
|
||||
char specifier (void) const;
|
||||
|
||||
private:
|
||||
char m_specifier;
|
||||
};
|
||||
|
||||
// missing format specifier
|
||||
class missing_error : public error
|
||||
{ using error::error; };
|
||||
|
23
format.ipp
23
format.ipp
@ -165,7 +165,7 @@ namespace util {
|
||||
throw util::format::format_error ("missing format specifier");
|
||||
|
||||
if (!is_valid_specifier<typename std::decay<ValueT>::type> (&*spec))
|
||||
throw util::format::format_error ("invalid/unhandled format specifier");
|
||||
throw util::format::invalid_specifier<ValueT> (*spec);
|
||||
|
||||
if (*spec == 'x') {
|
||||
dest << std::hex << val << std::dec;
|
||||
@ -194,4 +194,25 @@ namespace util {
|
||||
return out.str ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// TODO: we'd like to use typeid here for type naming, but we don't allow
|
||||
// RTTI. revisit this when introspection is more advanced.
|
||||
template <typename ValueT>
|
||||
format::invalid_specifier<ValueT>::invalid_specifier (char _specifier):
|
||||
format_error (
|
||||
format::render ("invalid specifier '%c' for type '%s'",
|
||||
_specifier,
|
||||
"unimplemented")
|
||||
),
|
||||
m_specifier (_specifier)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename ValueT>
|
||||
char
|
||||
format::invalid_specifier<ValueT>::specifier (void) const
|
||||
{ return m_specifier; }
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ main (void)
|
||||
util::format::render ("%s");
|
||||
}, "missing value");
|
||||
|
||||
tap.expect_throw<util::format::format_error> ([] (void) {
|
||||
tap.expect_throw<util::format::invalid_specifier<int>> ([] (void) {
|
||||
util::format::render ("%<", 42);
|
||||
}, "invalid specifier");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user