mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Refactored the traits a bit, and added displaying the key to string and numeric key not found errors
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
|
||||
#include "null.h"
|
||||
#include "traits.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
@@ -18,34 +19,8 @@ namespace YAML
|
||||
bool Convert(const std::string& input, bool& output);
|
||||
bool Convert(const std::string& input, _Null& output);
|
||||
|
||||
template <typename>
|
||||
struct is_numeric { enum { value = false }; };
|
||||
|
||||
template <> struct is_numeric <char> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned char> { enum { value = true }; };
|
||||
template <> struct is_numeric <int> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned int> { enum { value = true }; };
|
||||
template <> struct is_numeric <long int> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned long int> { enum { value = true }; };
|
||||
template <> struct is_numeric <short int> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned short int> { enum { value = true }; };
|
||||
template <> struct is_numeric <long long> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned long long> { enum { value = true }; };
|
||||
template <> struct is_numeric <float> { enum { value = true }; };
|
||||
template <> struct is_numeric <double> { enum { value = true }; };
|
||||
template <> struct is_numeric <long double> { enum { value = true }; };
|
||||
|
||||
|
||||
template <typename T, typename, bool = T::value>
|
||||
struct enable_if;
|
||||
|
||||
template <typename T, typename R>
|
||||
struct enable_if <T, R, true> {
|
||||
typedef R type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline bool Convert(const std::string& input, T& output, typename enable_if<is_numeric<T>, T>::type * = 0) {
|
||||
inline bool Convert(const std::string& input, T& output, typename enable_if<is_numeric<T> >::type * = 0) {
|
||||
std::stringstream stream(input);
|
||||
stream.unsetf(std::ios::dec);
|
||||
return stream >> output;
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
|
||||
#include "mark.h"
|
||||
#include "traits.h"
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
@@ -67,6 +68,22 @@ namespace YAML
|
||||
const std::string EXPECTED_VALUE_TOKEN = "expected value token";
|
||||
const std::string UNEXPECTED_KEY_TOKEN = "unexpected key token";
|
||||
const std::string UNEXPECTED_VALUE_TOKEN = "unexpected value token";
|
||||
|
||||
template <typename T>
|
||||
inline const std::string KEY_NOT_FOUND_WITH_KEY(const T&, typename disable_if<is_numeric<T> >::type * = 0) {
|
||||
return KEY_NOT_FOUND;
|
||||
}
|
||||
|
||||
inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) {
|
||||
return KEY_NOT_FOUND + ": " + key;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const std::string KEY_NOT_FOUND_WITH_KEY(const T& key, typename enable_if<is_numeric<T> >::type * = 0) {
|
||||
std::stringstream stream;
|
||||
stream << KEY_NOT_FOUND << ": " << key;
|
||||
return stream.str();
|
||||
}
|
||||
}
|
||||
|
||||
class Exception: public std::exception {
|
||||
@@ -108,22 +125,23 @@ namespace YAML
|
||||
|
||||
class KeyNotFound: public RepresentationException {
|
||||
public:
|
||||
KeyNotFound(const Mark& mark_)
|
||||
: RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND) {}
|
||||
template <typename T>
|
||||
KeyNotFound(const Mark& mark_, const T& key_)
|
||||
: RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND_WITH_KEY(key_)) {}
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
class TypedKeyNotFound: public KeyNotFound {
|
||||
public:
|
||||
TypedKeyNotFound(const Mark& mark_, const T& key_)
|
||||
: KeyNotFound(mark_), key(key_) {}
|
||||
~TypedKeyNotFound() throw() {}
|
||||
: KeyNotFound(mark_, key_), key(key_) {}
|
||||
virtual ~TypedKeyNotFound() throw() {}
|
||||
|
||||
T key;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
TypedKeyNotFound <T> MakeTypedKeyNotFound(const Mark& mark, const T& key) {
|
||||
inline TypedKeyNotFound <T> MakeTypedKeyNotFound(const Mark& mark, const T& key) {
|
||||
return TypedKeyNotFound <T> (mark, key);
|
||||
}
|
||||
|
||||
|
50
include/traits.h
Normal file
50
include/traits.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
template <typename>
|
||||
struct is_numeric { enum { value = false }; };
|
||||
|
||||
template <> struct is_numeric <char> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned char> { enum { value = true }; };
|
||||
template <> struct is_numeric <int> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned int> { enum { value = true }; };
|
||||
template <> struct is_numeric <long int> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned long int> { enum { value = true }; };
|
||||
template <> struct is_numeric <short int> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned short int> { enum { value = true }; };
|
||||
template <> struct is_numeric <long long> { enum { value = true }; };
|
||||
template <> struct is_numeric <unsigned long long> { enum { value = true }; };
|
||||
template <> struct is_numeric <float> { enum { value = true }; };
|
||||
template <> struct is_numeric <double> { enum { value = true }; };
|
||||
template <> struct is_numeric <long double> { enum { value = true }; };
|
||||
|
||||
template <bool, class T = void>
|
||||
struct enable_if_c {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct enable_if_c<false, T> {};
|
||||
|
||||
template <class Cond, class T = void>
|
||||
struct enable_if : public enable_if_c<Cond::value, T> {};
|
||||
|
||||
template <bool, class T = void>
|
||||
struct disable_if_c {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct disable_if_c<true, T> {};
|
||||
|
||||
template <class Cond, class T = void>
|
||||
struct disable_if : public disable_if_c<Cond::value, T> {};
|
||||
}
|
||||
|
||||
#endif // TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
Reference in New Issue
Block a user