1//
2// Array.h
3//
4// Library: MongoDB
5// Package: MongoDB
6// Module: Array
7//
8// Definition of the Array class.
9//
10// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef MongoDB_Array_INCLUDED
18#define MongoDB_Array_INCLUDED
19
20
21#include "Poco/NumberFormatter.h"
22#include "Poco/MongoDB/MongoDB.h"
23#include "Poco/MongoDB/Document.h"
24
25
26namespace Poco {
27namespace MongoDB {
28
29
30class MongoDB_API Array: public Document
31 /// This class represents a BSON Array.
32{
33public:
34 typedef SharedPtr<Array> Ptr;
35
36 Array();
37 /// Creates an empty Array.
38
39 virtual ~Array();
40 /// Destroys the Array.
41
42 template<typename T>
43 T get(int pos) const
44 /// Returns the element at the given index and tries to convert
45 /// it to the template type. If the element is not found, a
46 /// Poco::NotFoundException will be thrown. If the element cannot be
47 /// converted a BadCastException will be thrown.
48 {
49 return Document::get<T>(Poco::NumberFormatter::format(pos));
50 }
51
52 template<typename T>
53 T get(int pos, const T& deflt) const
54 /// Returns the element at the given index and tries to convert
55 /// it to the template type. If the element is not found, or
56 /// has the wrong type, the deflt argument will be returned.
57 {
58 return Document::get<T>(Poco::NumberFormatter::format(pos), deflt);
59 }
60
61 Element::Ptr get(int pos) const;
62 /// Returns the element at the given index.
63 /// An empty element will be returned if the element is not found.
64
65 Int64 getInteger(int pos) const
66 /// Returns an integer. Useful when MongoDB returns Int32, Int64
67 /// or double for a number (count for example). This method will always
68 /// return an Int64. When the element is not found, a
69 /// Poco::NotFoundException will be thrown.
70 {
71 return Document::getInteger(Poco::NumberFormatter::format(pos));
72 }
73
74 template<typename T>
75 bool isType(int pos) const
76 /// Returns true if the type of the element equals the TypeId of ElementTrait,
77 /// otherwise false.
78 {
79 return Document::isType<T>(Poco::NumberFormatter::format(pos));
80 }
81
82 std::string toString(int indent = 0) const;
83 /// Returns a string representation of the Array.
84};
85
86
87// BSON Embedded Array
88// spec: document
89template<>
90struct ElementTraits<Array::Ptr>
91{
92 enum { TypeId = 0x04 };
93
94 static std::string toString(const Array::Ptr& value, int indent = 0)
95 {
96 //TODO:
97 return value.isNull() ? "null" : value->toString(indent);
98 }
99};
100
101
102template<>
103inline void BSONReader::read<Array::Ptr>(Array::Ptr& to)
104{
105 to->read(_reader);
106}
107
108
109template<>
110inline void BSONWriter::write<Array::Ptr>(Array::Ptr& from)
111{
112 from->write(_writer);
113}
114
115
116} } // namespace Poco::MongoDB
117
118
119#endif // MongoDB_Array_INCLUDED
120