mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-18 19:48:44 +02:00
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:
@@ -38,6 +38,8 @@
|
|||||||
#ifndef _MongoDB_Array_included
|
#ifndef _MongoDB_Array_included
|
||||||
#define _MongoDB_Array_included
|
#define _MongoDB_Array_included
|
||||||
|
|
||||||
|
#include "Poco/NumberFormatter.h"
|
||||||
|
|
||||||
#include "Poco/MongoDB/MongoDB.h"
|
#include "Poco/MongoDB/MongoDB.h"
|
||||||
#include "Poco/MongoDB/Document.h"
|
#include "Poco/MongoDB/Document.h"
|
||||||
|
|
||||||
@@ -59,6 +61,40 @@ public:
|
|||||||
/// Destructor
|
/// 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;
|
std::string toString(int indent = 0) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -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::string Array::toString(int indent) const
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
Reference in New Issue
Block a user