mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Simplified Node::operator[] interface by removing the C-string overloads, using a helper to_value
This commit is contained in:
@@ -123,7 +123,7 @@ namespace YAML
|
|||||||
return node.Scalar();
|
return node.Scalar();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// access functions
|
// access functions
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline const T Node::as() const
|
inline const T Node::as() const
|
||||||
@@ -267,12 +267,57 @@ namespace YAML
|
|||||||
m_pMemory->merge(*rhs.m_pMemory);
|
m_pMemory->merge(*rhs.m_pMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helpers for indexing
|
||||||
|
namespace detail {
|
||||||
|
template<typename T>
|
||||||
|
struct to_value_t {
|
||||||
|
explicit to_value_t(const T& t_): t(t_) {}
|
||||||
|
const T& t;
|
||||||
|
typedef const T& return_type;
|
||||||
|
|
||||||
|
const T& operator()() const { return t; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct to_value_t<const char*> {
|
||||||
|
explicit to_value_t(const char *t_): t(t_) {}
|
||||||
|
const char *t;
|
||||||
|
typedef std::string return_type;
|
||||||
|
|
||||||
|
const std::string operator()() const { return t; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct to_value_t<char*> {
|
||||||
|
explicit to_value_t(char *t_): t(t_) {}
|
||||||
|
const char *t;
|
||||||
|
typedef std::string return_type;
|
||||||
|
|
||||||
|
const std::string operator()() const { return t; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<std::size_t N>
|
||||||
|
struct to_value_t<char [N]> {
|
||||||
|
explicit to_value_t(const char *t_): t(t_) {}
|
||||||
|
const char *t;
|
||||||
|
typedef std::string return_type;
|
||||||
|
|
||||||
|
const std::string operator()() const { return t; }
|
||||||
|
};
|
||||||
|
|
||||||
|
// converts C-strings to std::strings so they can be copied
|
||||||
|
template<typename T>
|
||||||
|
inline typename to_value_t<T>::return_type to_value(const T& t) {
|
||||||
|
return to_value_t<T>(t)();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// indexing
|
// indexing
|
||||||
template<typename Key>
|
template<typename Key>
|
||||||
inline const Node Node::operator[](const Key& key) const
|
inline const Node Node::operator[](const Key& key) const
|
||||||
{
|
{
|
||||||
EnsureNodeExists();
|
EnsureNodeExists();
|
||||||
detail::node& value = static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
|
detail::node& value = static_cast<const detail::node&>(*m_pNode).get(detail::to_value(key), m_pMemory);
|
||||||
return Node(value, m_pMemory);
|
return Node(value, m_pMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +325,7 @@ namespace YAML
|
|||||||
inline Node Node::operator[](const Key& key)
|
inline Node Node::operator[](const Key& key)
|
||||||
{
|
{
|
||||||
EnsureNodeExists();
|
EnsureNodeExists();
|
||||||
detail::node& value = m_pNode->get(key, m_pMemory);
|
detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory);
|
||||||
return Node(value, m_pMemory);
|
return Node(value, m_pMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,7 +333,7 @@ namespace YAML
|
|||||||
inline bool Node::remove(const Key& key)
|
inline bool Node::remove(const Key& key)
|
||||||
{
|
{
|
||||||
EnsureNodeExists();
|
EnsureNodeExists();
|
||||||
return m_pNode->remove(key, m_pMemory);
|
return m_pNode->remove(detail::to_value(key), m_pMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const Node Node::operator[](const Node& key) const
|
inline const Node Node::operator[](const Node& key) const
|
||||||
@@ -313,36 +358,6 @@ namespace YAML
|
|||||||
key.EnsureNodeExists();
|
key.EnsureNodeExists();
|
||||||
return m_pNode->remove(*key.m_pNode, m_pMemory);
|
return m_pNode->remove(*key.m_pNode, m_pMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const Node Node::operator[](const char *key) const
|
|
||||||
{
|
|
||||||
return operator[](std::string(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Node Node::operator[](const char *key)
|
|
||||||
{
|
|
||||||
return operator[](std::string(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Node::remove(const char *key)
|
|
||||||
{
|
|
||||||
return remove(std::string(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const Node Node::operator[](char *key) const
|
|
||||||
{
|
|
||||||
return operator[](static_cast<const char *>(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Node Node::operator[](char *key)
|
|
||||||
{
|
|
||||||
return operator[](static_cast<const char *>(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Node::remove(char *key)
|
|
||||||
{
|
|
||||||
return remove(static_cast<const char *>(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
// free functions
|
// free functions
|
||||||
|
|
||||||
|
@@ -78,15 +78,7 @@ namespace YAML
|
|||||||
const Node operator[](const Node& key) const;
|
const Node operator[](const Node& key) const;
|
||||||
Node operator[](const Node& key);
|
Node operator[](const Node& key);
|
||||||
bool remove(const Node& key);
|
bool remove(const Node& key);
|
||||||
|
|
||||||
const Node operator[](const char *key) const;
|
|
||||||
Node operator[](const char *key);
|
|
||||||
bool remove(const char *key);
|
|
||||||
|
|
||||||
const Node operator[](char *key) const;
|
|
||||||
Node operator[](char *key);
|
|
||||||
bool remove(char *key);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit Node(detail::node& node, detail::shared_memory_holder pMemory);
|
explicit Node(detail::node& node, detail::shared_memory_holder pMemory);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user