Add methods that turns a numeric index into a string for getting the element (an array is actually a document)

This commit is contained in:
fbraem
2013-02-27 19:51:33 +01:00
parent d1b318b806
commit ee360d307c
2 changed files with 43 additions and 0 deletions

View File

@@ -38,6 +38,8 @@
#ifndef _MongoDB_Array_included
#define _MongoDB_Array_included
#include "Poco/NumberFormatter.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Document.h"
@@ -59,6 +61,40 @@ public:
/// Destructor
template<typename T>
T get(int pos) const
/// Returns the element on the given index and tries to convert
/// it to the template type. When the element is not found, a
/// NotFoundException will be thrown. When the element can't be
/// converted a BadCastException will be thrown.
{
return Document::get<T>(Poco::NumberFormatter::format(pos));
}
template<typename T>
T get(int pos, const T& def) const
/// Returns the element on the given index and tries to convert
/// it to the template type. When the element is not found, or
/// has the wrong type, the def argument will be returned.
{
return Document::get<T>(Poco::NumberFormatter::format(pos), def);
}
Element::Ptr get(int pos) const;
/// Returns the element on the given index.
/// An empty element will be returned when the element is not found.
template<typename T>
bool isType(int pos)
/// Returns true when the type of the element equals the TypeId of ElementTrait
{
return Document::isType<T>(Poco::NumberFormatter::format(pos));
}
std::string toString(int indent = 0) const;
};

View File

@@ -51,6 +51,13 @@ Array::~Array()
}
Element::Ptr Array::get(int pos) const
{
std::string name = Poco::NumberFormatter::format(pos);
return Document::get(name);
}
std::string Array::toString(int indent) const
{
std::ostringstream oss;