Set up map searching by templated key

This commit is contained in:
beder
2011-09-07 03:21:24 -05:00
parent 7a3f425720
commit 2d75a631e2
6 changed files with 37 additions and 31 deletions

View File

@@ -15,13 +15,13 @@ namespace YAML
{ {
// indexing // indexing
template<typename Key> template<typename Key>
inline shared_node node_data::operator[](const Key& key) const inline shared_node node_data::get(const Key& key, shared_memory_holder pMemory) const
{ {
if(m_type != ValueType::Map) if(m_type != ValueType::Map)
return shared_node(new node); return shared_node(new node);
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) { for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
if(it->first->equals(key)) if(equals(it->first, key, pMemory))
return it->second; return it->second;
} }
@@ -29,12 +29,12 @@ namespace YAML
} }
template<typename Key> template<typename Key>
inline shared_node node_data::operator[](const Key& key) inline shared_node node_data::get(const Key& key, shared_memory_holder pMemory)
{ {
} }
template<typename Key> template<typename Key>
inline bool node_data::remove(const Key& key) inline bool node_data::remove(const Key& key, shared_memory_holder pMemory)
{ {
} }
} }

View File

@@ -9,6 +9,7 @@
#include "yaml-cpp/dll.h" #include "yaml-cpp/dll.h"
#include "yaml-cpp/value/type.h" #include "yaml-cpp/value/type.h"
#include "yaml-cpp/value/ptr.h" #include "yaml-cpp/value/ptr.h"
#include "yaml-cpp/value/value.h"
#include "yaml-cpp/value/detail/node_data.h" #include "yaml-cpp/value/detail/node_data.h"
namespace YAML namespace YAML
@@ -27,26 +28,19 @@ namespace YAML
void set_type(ValueType::value type) { m_pData->set_type(type); } void set_type(ValueType::value type) { m_pData->set_type(type); }
void set_null() { m_pData->set_null(); } void set_null() { m_pData->set_null(); }
void set_scalar(const std::string& scalar) { m_pData->set_scalar(scalar); } void set_scalar(const std::string& scalar) { m_pData->set_scalar(scalar); }
template<typename T> bool equals(const T& rhs) const;
// indexing // indexing
template<typename Key> shared_node operator[](const Key& key) const { return (static_cast<const node_data&>(*m_pData))[key]; } template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory) const { return static_cast<const node_data&>(*m_pData).get(key, pMemory); }
template<typename Key> shared_node operator[](const Key& key) { return (*m_pData)[key]; } template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory) { return m_pData->get(key, pMemory); }
template<typename Key> bool remove(const Key& key) { return m_pData->remove(key); } template<typename Key> bool remove(const Key& key, shared_memory_holder pMemory) { return m_pData->remove(key, pMemory); }
shared_node operator[](shared_node pKey) const { return (static_cast<const node_data&>(*m_pData))[pKey]; } shared_node get(shared_node pKey) const { return static_cast<const node_data&>(*m_pData).get(pKey); }
shared_node operator[](shared_node pKey) { return (*m_pData)[pKey]; } shared_node get(shared_node pKey) { return m_pData->get(pKey); }
bool remove(shared_node pKey) { return m_pData->remove(pKey); } bool remove(shared_node pKey) { return m_pData->remove(pKey); }
private: private:
shared_node_data m_pData; shared_node_data m_pData;
}; };
template<typename T>
inline bool node::equals(const T& rhs) const
{
}
} }
} }

View File

@@ -30,12 +30,12 @@ namespace YAML
const std::string scalar() const { return m_scalar; } const std::string scalar() const { return m_scalar; }
// indexing // indexing
template<typename Key> shared_node operator[](const Key& key) const; template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory) const;
template<typename Key> shared_node operator[](const Key& key); template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory);
template<typename Key> bool remove(const Key& key); template<typename Key> bool remove(const Key& key, shared_memory_holder pMemory);
shared_node operator[](shared_node pKey) const; shared_node get(shared_node pKey) const;
shared_node operator[](shared_node pKey); shared_node get(shared_node pKey);
bool remove(shared_node pKey); bool remove(shared_node pKey);
private: private:

View File

@@ -136,38 +136,38 @@ namespace YAML
template<typename Key> template<typename Key>
inline const Value Value::operator[](const Key& key) const inline const Value Value::operator[](const Key& key) const
{ {
detail::shared_node pValue = (static_cast<const detail::node&>(*m_pNode))[key]; detail::shared_node pValue = static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
return Value(pValue, m_pMemory); return Value(pValue, m_pMemory);
} }
template<typename Key> template<typename Key>
inline Value Value::operator[](const Key& key) inline Value Value::operator[](const Key& key)
{ {
detail::shared_node pValue = (*m_pNode)[key]; detail::shared_node pValue = m_pNode->get(key, m_pMemory);
return Value(pValue, m_pMemory); return Value(pValue, m_pMemory);
} }
template<typename Key> template<typename Key>
inline bool Value::remove(const Key& key) inline bool Value::remove(const Key& key)
{ {
return m_pNode->remove(key); return m_pNode->remove(key, m_pMemory);
} }
inline const Value Value::operator[](const Value& key) const inline const Value Value::operator[](const Value& key) const
{ {
detail::shared_node pValue = (static_cast<const detail::node&>(*m_pNode))[*key.m_pNode]; detail::shared_node pValue = static_cast<const detail::node&>(*m_pNode).get(key.m_pNode);
return Value(pValue, m_pMemory); return Value(pValue, m_pMemory);
} }
inline Value Value::operator[](const Value& key) inline Value Value::operator[](const Value& key)
{ {
detail::shared_node pValue = (*m_pNode)[*key.m_pNode]; detail::shared_node pValue = m_pNode->get(key.m_pNode);
return Value(pValue, m_pMemory); return Value(pValue, m_pMemory);
} }
inline bool Value::remove(const Value& key) inline bool Value::remove(const Value& key)
{ {
return m_pNode->remove(*key.m_pNode); return m_pNode->remove(key.m_pNode);
} }
inline const Value Value::operator[](const char *key) const inline const Value Value::operator[](const char *key) const
@@ -182,7 +182,7 @@ namespace YAML
inline bool Value::remove(const char *key) inline bool Value::remove(const char *key)
{ {
return m_pNode->remove(std::string(key)); return remove(std::string(key));
} }
inline const Value Value::operator[](char *key) const inline const Value Value::operator[](char *key) const
@@ -215,6 +215,15 @@ namespace YAML
{ {
return false; return false;
} }
template<typename T>
inline bool equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory)
{
T lhs;
if(convert(Value(pNode, pMemory), lhs))
return lhs == rhs;
return false;
}
} }
#endif // VALUE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #endif // VALUE_IMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -61,6 +61,9 @@ namespace YAML
private: private:
explicit Value(detail::shared_node pNode, detail::shared_memory_holder pMemory); explicit Value(detail::shared_node pNode, detail::shared_memory_holder pMemory);
template<typename T>
friend bool equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory);
template<typename T> void Assign(const T& rhs); template<typename T> void Assign(const T& rhs);
void Assign(const char *rhs); void Assign(const char *rhs);
void Assign(char *rhs); void Assign(char *rhs);

View File

@@ -57,7 +57,7 @@ namespace YAML
} }
// indexing // indexing
shared_node node_data::operator[](shared_node pKey) const shared_node node_data::get(shared_node pKey) const
{ {
if(m_type != ValueType::Map) if(m_type != ValueType::Map)
return shared_node(new node); return shared_node(new node);
@@ -70,7 +70,7 @@ namespace YAML
return shared_node(new node); return shared_node(new node);
} }
shared_node node_data::operator[](shared_node pKey) shared_node node_data::get(shared_node pKey)
{ {
switch(m_type) { switch(m_type) {
case ValueType::Undefined: case ValueType::Undefined: