Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(MongoDB): Document::get returns value by const reference #4563

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MongoDB/include/Poco/MongoDB/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MongoDB_API Array: public Document
Array();
/// Creates an empty Array.

virtual ~Array();
~Array() override;
/// Destroys the Array.

// Document template functions available for backward compatibility
Expand Down Expand Up @@ -93,7 +93,7 @@ class MongoDB_API Array: public Document
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:
Expand Down
8 changes: 2 additions & 6 deletions MongoDB/include/Poco/MongoDB/Binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()};
}


Expand Down Expand Up @@ -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());
}


Expand Down
1 change: 0 additions & 1 deletion MongoDB/include/Poco/MongoDB/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions MongoDB/include/Poco/MongoDB/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class MongoDB_API Document
/// 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
Expand All @@ -123,7 +123,7 @@ class MongoDB_API Document
{
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();
Expand All @@ -134,7 +134,7 @@ class MongoDB_API Document
}

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.
Expand All @@ -147,7 +147,7 @@ class MongoDB_API Document

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();
Expand Down Expand Up @@ -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());
}
}

Expand Down
25 changes: 11 additions & 14 deletions MongoDB/include/Poco/MongoDB/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 << "\\\"";
Expand All @@ -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;
}
Expand Down Expand Up @@ -360,34 +359,32 @@ class ConcreteElement: public Element
{
}

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);
}
Expand Down
2 changes: 1 addition & 1 deletion MongoDB/src/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
4 changes: 4 additions & 0 deletions MongoDB/src/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions MongoDB/src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions MongoDB/src/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
{
Expand Down
10 changes: 5 additions & 5 deletions MongoDB/testsuite/src/MongoDBTestOpMsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
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");
Expand Down Expand Up @@ -194,14 +194,14 @@

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");
Dismissed Show dismissed Hide dismissed
assertTrue (doc->isType<NullValue>("unknown"));
bool active = doc->get<bool>("active");
assertEquals (false, active);
Expand Down
Loading