Add method addNewDocument and return a reference of the active document in the add methods to allow chaining these methods ...

This commit is contained in:
fbraem
2013-02-21 16:48:21 +01:00
parent 7600eed502
commit 7328eace34
2 changed files with 32 additions and 12 deletions

View File

@@ -86,27 +86,36 @@ public:
/// Destructor
void addElement(Element::Ptr element);
/// Add an element to the document
Document& addElement(Element::Ptr element);
/// Add an element to the document.
/// The active document is returned to allow chaining of the add methods.
template<typename T>
void add(const std::string& name, T value)
/// Creates an element with the given name and value
Document& add(const std::string& name, T value)
/// Creates an element with the given name and value and
// adds it to the document.
/// The active document is returned to allow chaining of the add methods.
{
addElement(new ConcreteElement<T>(name, value));
return addElement(new ConcreteElement<T>(name, value));
}
void add(const std::string& name, const char* value)
/// Creates an element with the given name and value
Document& add(const std::string& name, const char* value)
/// Creates an element with the given name and value and
// adds it to the document.
/// The active document is returned to allow chaining of the add methods.
{
addElement(new ConcreteElement<std::string>(name, std::string(value)));
return addElement(new ConcreteElement<std::string>(name, std::string(value)));
}
Document& addNewDocument(const std::string& name);
/// Create a new document and add it to this document.
/// Unlike the other add methods, this method returns
/// a reference to the new document.
void clear();
/// Removes all elements from the document.
@@ -212,6 +221,21 @@ protected:
};
inline Document& Document::addElement(Element::Ptr element)
{
_elements.insert(element);
return *this;
}
inline Document& Document::addNewDocument(const std::string& name)
{
Document::Ptr newDoc = new Document();
add(name, newDoc);
return *newDoc;
}
inline void Document::clear()
{
_elements.clear();

View File

@@ -220,9 +220,5 @@ void Document::write(BinaryWriter& writer)
writer << '\0';
}
void Document::addElement(Element::Ptr element)
{
_elements.insert(element);
}
}} // Namespace Poco::MongoDB