mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-13 10:32:57 +01:00
enh(MongoDB): Document::get returns value by const reference instead of creating a copy and few other minor improvements.
This commit is contained in:
parent
4552df2f2e
commit
cf1a29908a
@ -36,7 +36,7 @@ public:
|
||||
Array();
|
||||
/// Creates an empty Array.
|
||||
|
||||
virtual ~Array();
|
||||
~Array() override;
|
||||
/// Destroys the Array.
|
||||
|
||||
// Document template functions available for backward compatibility
|
||||
@ -93,7 +93,7 @@ public:
|
||||
return Document::isType<T>(Poco::NumberFormatter::format(pos));
|
||||
}
|
||||
|
||||
std::string toString(int indent = 0) const;
|
||||
std::string toString(int indent = 0) const override;
|
||||
/// Returns a string representation of the Array.
|
||||
|
||||
private:
|
||||
|
@ -20,12 +20,8 @@
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Element.h"
|
||||
#include "Poco/Base64Encoder.h"
|
||||
#include "Poco/Buffer.h"
|
||||
#include "Poco/StreamCopier.h"
|
||||
#include "Poco/MemoryStream.h"
|
||||
#include "Poco/UUID.h"
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -106,7 +102,7 @@ inline Buffer<unsigned char>& Binary::buffer()
|
||||
|
||||
inline std::string Binary::toRawString() const
|
||||
{
|
||||
return std::string(reinterpret_cast<const char*>(_buffer.begin()), _buffer.size());
|
||||
return {reinterpret_cast<const char*>(_buffer.begin()), _buffer.size()};
|
||||
}
|
||||
|
||||
|
||||
@ -145,7 +141,7 @@ inline void BSONWriter::write<Binary::Ptr>(Binary::Ptr& from)
|
||||
{
|
||||
_writer << (Poco::Int32) from->buffer().size();
|
||||
_writer << from->subtype();
|
||||
_writer.writeRaw((char*) from->buffer().begin(), from->buffer().size());
|
||||
_writer.writeRaw(reinterpret_cast<char*>(from->buffer().begin()), from->buffer().size());
|
||||
}
|
||||
|
||||
|
||||
|
@ -20,7 +20,6 @@
|
||||
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/Net/StreamSocket.h"
|
||||
#include "Poco/Mutex.h"
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
#include "Poco/MongoDB/ResponseMessage.h"
|
||||
#include "Poco/MongoDB/OpMsgMessage.h"
|
||||
|
@ -108,7 +108,7 @@ public:
|
||||
/// Returns true if the document has an element with the given name.
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& name) const
|
||||
const T& get(const std::string& name) const
|
||||
/// Returns the element with the given name 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
|
||||
@ -123,7 +123,7 @@ public:
|
||||
{
|
||||
if (ElementTraits<T>::TypeId == element->type())
|
||||
{
|
||||
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
|
||||
auto* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
|
||||
if (concrete != 0)
|
||||
{
|
||||
return concrete->value();
|
||||
@ -134,7 +134,7 @@ public:
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& name, const T& def) const
|
||||
const T& get(const std::string& name, const T& def) const
|
||||
/// Returns the element with the given name 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.
|
||||
@ -147,7 +147,7 @@ public:
|
||||
|
||||
if (ElementTraits<T>::TypeId == element->type())
|
||||
{
|
||||
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
|
||||
auto* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
|
||||
if (concrete != 0)
|
||||
{
|
||||
return concrete->value();
|
||||
@ -232,9 +232,9 @@ inline bool Document::empty() const
|
||||
|
||||
inline void Document::elementNames(std::vector<std::string>& keys) const
|
||||
{
|
||||
for (ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
for (const auto & _element : _elements)
|
||||
{
|
||||
keys.push_back((*it)->name());
|
||||
keys.push_back(_element->name());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
#include "Poco/Nullable.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
#include "Poco/DateTimeFormatter.h"
|
||||
#include "Poco/UTF8String.h"
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/BSONReader.h"
|
||||
#include "Poco/MongoDB/BSONWriter.h"
|
||||
@ -115,9 +114,9 @@ struct ElementTraits<std::string>
|
||||
|
||||
oss << '"';
|
||||
|
||||
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
|
||||
for (char it : value)
|
||||
{
|
||||
switch (*it)
|
||||
switch (it)
|
||||
{
|
||||
case '"':
|
||||
oss << "\\\"";
|
||||
@ -142,13 +141,13 @@ struct ElementTraits<std::string>
|
||||
break;
|
||||
default:
|
||||
{
|
||||
if ( *it > 0 && *it <= 0x1F )
|
||||
if ( it > 0 && it <= 0x1F )
|
||||
{
|
||||
oss << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*it);
|
||||
oss << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << *it;
|
||||
oss << it;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -360,34 +359,32 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~ConcreteElement()
|
||||
{
|
||||
}
|
||||
~ConcreteElement() override = default;
|
||||
|
||||
|
||||
T value() const
|
||||
const T& value() const
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
|
||||
std::string toString(int indent = 0) const
|
||||
std::string toString(int indent = 0) const override
|
||||
{
|
||||
return ElementTraits<T>::toString(_value, indent);
|
||||
}
|
||||
|
||||
|
||||
int type() const
|
||||
int type() const override
|
||||
{
|
||||
return ElementTraits<T>::TypeId;
|
||||
}
|
||||
|
||||
void read(BinaryReader& reader)
|
||||
void read(BinaryReader& reader) override
|
||||
{
|
||||
BSONReader(reader).read(_value);
|
||||
}
|
||||
|
||||
void write(BinaryWriter& writer)
|
||||
void write(BinaryWriter& writer) override
|
||||
{
|
||||
BSONWriter(writer).write(_value);
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ std::string Array::toString(int indent) const
|
||||
|
||||
if (indent > 0) oss << std::endl;
|
||||
|
||||
for (ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
for (auto it = _elements.begin(), total = _elements.end(); it != total; ++it)
|
||||
{
|
||||
if (it != _elements.begin())
|
||||
{
|
||||
|
@ -13,6 +13,10 @@
|
||||
|
||||
|
||||
#include "Poco/MongoDB/Binary.h"
|
||||
#include "Poco/Base64Encoder.h"
|
||||
#include "Poco/StreamCopier.h"
|
||||
#include "Poco/MemoryStream.h"
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
|
||||
#include "Poco/MongoDB/Database.h"
|
||||
#include "Poco/Base64Encoder.h"
|
||||
#include "Poco/MongoDB/Binary.h"
|
||||
#include "Poco/MD5Engine.h"
|
||||
#include "Poco/SHA1Engine.h"
|
||||
|
@ -47,7 +47,7 @@ Element::Ptr Document::get(const std::string& name) const
|
||||
{
|
||||
Element::Ptr element;
|
||||
|
||||
ElementSet::const_iterator it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name));
|
||||
auto it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name));
|
||||
if (it != _elements.end())
|
||||
{
|
||||
return *it;
|
||||
@ -167,7 +167,7 @@ std::string Document::toString(int indent) const
|
||||
if (indent > 0) oss << std::endl;
|
||||
|
||||
|
||||
for (ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
for (auto it = _elements.begin(), total = _elements.end(); it != total; ++it)
|
||||
{
|
||||
if (it != _elements.begin())
|
||||
{
|
||||
|
@ -60,7 +60,7 @@ void MongoDBTest::testOpCmdUUID()
|
||||
Document::Ptr doc = response.documents()[0];
|
||||
try
|
||||
{
|
||||
std::string name = doc->get<std::string>("name");
|
||||
const auto& name = doc->get<std::string>("name");
|
||||
assertEquals ("Barcelona", name );
|
||||
|
||||
Binary::Ptr uuidBinary = doc->get<Binary::Ptr>("uuid");
|
||||
@ -194,14 +194,14 @@ void MongoDBTest::testOpCmdFind()
|
||||
|
||||
try
|
||||
{
|
||||
std::string lastname = doc->get<std::string>("lastname");
|
||||
const auto& lastname = doc->get<std::string>("lastname");
|
||||
assertEquals ("Braem", lastname);
|
||||
std::string firstname = doc->get<std::string>("firstname");
|
||||
const auto& firstname = doc->get<std::string>("firstname");
|
||||
assertEquals ("Franky", firstname);
|
||||
Poco::Timestamp birthDateTimestamp = doc->get<Poco::Timestamp>("birthdate");
|
||||
const auto& birthDateTimestamp = doc->get<Poco::Timestamp>("birthdate");
|
||||
Poco::DateTime birthDate(birthDateTimestamp);
|
||||
assertTrue (birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
|
||||
Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
|
||||
const auto& lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
|
||||
assertTrue (doc->isType<NullValue>("unknown"));
|
||||
bool active = doc->get<bool>("active");
|
||||
assertEquals (false, active);
|
||||
|
Loading…
Reference in New Issue
Block a user