From ee360d307c16405b882b72b0d7751df4fba0abc6 Mon Sep 17 00:00:00 2001 From: fbraem Date: Wed, 27 Feb 2013 19:51:33 +0100 Subject: [PATCH] Add methods that turns a numeric index into a string for getting the element (an array is actually a document) --- MongoDB/include/Poco/MongoDB/Array.h | 36 ++++++++++++++++++++++++++++ MongoDB/src/Array.cpp | 7 ++++++ 2 files changed, 43 insertions(+) diff --git a/MongoDB/include/Poco/MongoDB/Array.h b/MongoDB/include/Poco/MongoDB/Array.h index 0a5f417bc..8c42e95fc 100644 --- a/MongoDB/include/Poco/MongoDB/Array.h +++ b/MongoDB/include/Poco/MongoDB/Array.h @@ -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 + 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(Poco::NumberFormatter::format(pos)); + } + + + template + 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(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 + bool isType(int pos) + /// Returns true when the type of the element equals the TypeId of ElementTrait + { + return Document::isType(Poco::NumberFormatter::format(pos)); + } + + std::string toString(int indent = 0) const; }; diff --git a/MongoDB/src/Array.cpp b/MongoDB/src/Array.cpp index 191ee8d43..64528151b 100644 --- a/MongoDB/src/Array.cpp +++ b/MongoDB/src/Array.cpp @@ -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;