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:
@@ -267,12 +267,57 @@ namespace YAML
|
||||
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
|
||||
template<typename Key>
|
||||
inline const Node Node::operator[](const Key& key) const
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -280,7 +325,7 @@ namespace YAML
|
||||
inline Node Node::operator[](const Key& key)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -288,7 +333,7 @@ namespace YAML
|
||||
inline bool Node::remove(const Key& key)
|
||||
{
|
||||
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
|
||||
@@ -314,36 +359,6 @@ namespace YAML
|
||||
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
|
||||
|
||||
inline bool operator==(const Node& lhs, const Node& rhs)
|
||||
|
@@ -79,14 +79,6 @@ namespace YAML
|
||||
Node operator[](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:
|
||||
explicit Node(detail::node& node, detail::shared_memory_holder pMemory);
|
||||
|
||||
|
Reference in New Issue
Block a user