Simplified Node::operator[] interface by removing the C-string overloads, using a helper to_value

This commit is contained in:
Jesse Beder 2012-10-31 18:55:07 -05:00
parent d770a7dc97
commit a645866ffa
2 changed files with 49 additions and 42 deletions

View File

@ -123,7 +123,7 @@ namespace YAML
return node.Scalar();
}
};
// access functions
template<typename T>
inline const T Node::as() const
@ -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
@ -313,36 +358,6 @@ namespace YAML
key.EnsureNodeExists();
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

View File

@ -78,15 +78,7 @@ namespace YAML
const Node operator[](const Node& key) const;
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);