mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Converted indexing to std::size_t, and fixed the Node templated overloads to properly index any index type (determining what is an index type is a bit of a hack - it should be is_convertible<T, std::size_t> (I think), but I just explicitly wrote down a list)
This commit is contained in:
@@ -40,7 +40,7 @@ namespace YAML
|
||||
// accessors
|
||||
Iterator begin() const;
|
||||
Iterator end() const;
|
||||
unsigned size() const;
|
||||
std::size_t size() const;
|
||||
|
||||
// extraction of scalars
|
||||
bool GetScalar(std::string& s) const;
|
||||
@@ -55,19 +55,17 @@ namespace YAML
|
||||
template <typename T>
|
||||
friend void operator >> (const Node& node, T& value);
|
||||
|
||||
// just for maps
|
||||
// retrieval for maps and sequences
|
||||
template <typename T>
|
||||
const Node *FindValue(const T& key) const;
|
||||
const Node *FindValue(const char *key) const;
|
||||
|
||||
|
||||
template <typename T>
|
||||
const Node& operator [] (const T& key) const;
|
||||
|
||||
// specific to maps
|
||||
const Node *FindValue(const char *key) const;
|
||||
const Node& operator [] (const char *key) const;
|
||||
|
||||
// just for sequences
|
||||
const Node& operator [] (unsigned u) const;
|
||||
const Node& operator [] (int i) const;
|
||||
|
||||
// for anchors/aliases
|
||||
const Node *Identity() const { return m_pIdentity; }
|
||||
bool IsAlias() const { return m_alias; }
|
||||
@@ -81,10 +79,17 @@ namespace YAML
|
||||
friend bool operator < (const Node& n1, const Node& n2);
|
||||
|
||||
private:
|
||||
// helper for sequences
|
||||
template <typename, bool> friend struct _FindFromNodeAtIndex;
|
||||
const Node *FindAtIndex(std::size_t i) const;
|
||||
|
||||
// helper for maps
|
||||
template <typename T>
|
||||
const Node& GetValue(const T& key) const;
|
||||
|
||||
|
||||
template <typename T>
|
||||
const Node *FindValueForKey(const T& key) const;
|
||||
|
||||
// helpers for parsing
|
||||
void ParseHeader(Scanner *pScanner, const ParserState& state);
|
||||
void ParseTag(Scanner *pScanner, const ParserState& state);
|
||||
|
@@ -4,6 +4,8 @@
|
||||
#define NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "nodeutil.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// implementation of templated things
|
||||
@@ -31,9 +33,18 @@ namespace YAML
|
||||
|
||||
template <typename T>
|
||||
inline const Node *Node::FindValue(const T& key) const {
|
||||
if(!m_pContent)
|
||||
return 0;
|
||||
|
||||
switch(GetType()) {
|
||||
case CT_MAP:
|
||||
return FindValueForKey(key);
|
||||
case CT_SEQUENCE:
|
||||
return FindFromNodeAtIndex(*this, key);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const Node *Node::FindValueForKey(const T& key) const {
|
||||
for(Iterator it=begin();it!=end();++it) {
|
||||
T t;
|
||||
if(it.first().Read(t)) {
|
||||
@@ -45,24 +56,16 @@ namespace YAML
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline const Node *Node::FindValue(const char *key) const {
|
||||
return FindValue(std::string(key));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const Node& Node::GetValue(const T& key) const {
|
||||
if(!m_pContent)
|
||||
throw BadDereference();
|
||||
|
||||
for(Iterator it=begin();it!=end();++it) {
|
||||
T t;
|
||||
if(it.first().Read(t)) {
|
||||
if(key == t)
|
||||
return it.second();
|
||||
}
|
||||
}
|
||||
|
||||
throw MakeTypedKeyNotFound(m_mark, key);
|
||||
const Node *pValue = FindValue(key);
|
||||
if(!pValue)
|
||||
throw MakeTypedKeyNotFound(m_mark, key);
|
||||
|
||||
return *pValue;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -70,6 +73,10 @@ namespace YAML
|
||||
return GetValue(key);
|
||||
}
|
||||
|
||||
inline const Node *Node::FindValue(const char *key) const {
|
||||
return FindValue(std::string(key));
|
||||
}
|
||||
|
||||
inline const Node& Node::operator [] (const char *key) const {
|
||||
return GetValue(std::string(key));
|
||||
}
|
||||
|
60
include/nodeutil.h
Normal file
60
include/nodeutil.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef NODEUTIL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define NODEUTIL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
template <typename T, typename U>
|
||||
struct is_same_type {
|
||||
enum { value = false };
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_same_type<T, T> {
|
||||
enum { value = true };
|
||||
};
|
||||
|
||||
template <typename T, bool check>
|
||||
struct is_index_type_with_check {
|
||||
enum { value = false };
|
||||
};
|
||||
|
||||
template <> struct is_index_type_with_check<std::size_t, false> { enum { value = true }; };
|
||||
|
||||
#define MAKE_INDEX_TYPE(Type) \
|
||||
template <> struct is_index_type_with_check<Type, is_same_type<Type, std::size_t>::value> { enum { value = true }; }
|
||||
|
||||
MAKE_INDEX_TYPE(int);
|
||||
MAKE_INDEX_TYPE(unsigned);
|
||||
MAKE_INDEX_TYPE(short);
|
||||
MAKE_INDEX_TYPE(unsigned short);
|
||||
MAKE_INDEX_TYPE(long);
|
||||
MAKE_INDEX_TYPE(unsigned long);
|
||||
|
||||
#undef MAKE_INDEX_TYPE;
|
||||
|
||||
template <typename T>
|
||||
struct is_index_type: public is_index_type_with_check<T, false> {};
|
||||
|
||||
// messing around with template stuff to get the right overload for operator [] for a sequence
|
||||
template <typename T, bool b>
|
||||
struct _FindFromNodeAtIndex {
|
||||
const Node *pRet;
|
||||
_FindFromNodeAtIndex(const Node&, const T&): pRet(0) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct _FindFromNodeAtIndex<T, true> {
|
||||
const Node *pRet;
|
||||
_FindFromNodeAtIndex(const Node& node, const T& key): pRet(node.FindAtIndex(static_cast<std::size_t>(key))) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline const Node *FindFromNodeAtIndex(const Node& node, const T& key) {
|
||||
return _FindFromNodeAtIndex<T, is_index_type<T>::value>(node, key).pRet;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // NODEUTIL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
Reference in New Issue
Block a user