mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-23 08:31:43 +02:00
ObjectId constructor translates a hex string (24 characters) into a 12 byte object id
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/MongoDB/ObjectId.h"
|
||||
#include "Poco/Format.h"
|
||||
|
||||
@@ -23,21 +22,26 @@
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
ObjectId::ObjectId()
|
||||
{
|
||||
memset(_id, 0, sizeof(_id));
|
||||
}
|
||||
|
||||
ObjectId::ObjectId(const std::string& id)
|
||||
{
|
||||
if (id.size() == 12)
|
||||
{
|
||||
std::string::const_iterator it = id.begin();
|
||||
std::string::const_iterator end = id.end();
|
||||
for (int i = 0; i < 12; ++it, ++i) _id[i] = *it;
|
||||
}
|
||||
else if (id.size())
|
||||
{
|
||||
throw Poco::InvalidArgumentException("ID must be 12 characters long.");
|
||||
poco_assert_dbg(id.size() == 24);
|
||||
|
||||
const char *p = id.c_str();
|
||||
for (std::size_t i = 0; i < 12; ++i) {
|
||||
_id[i] = fromHex(p);
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
|
||||
ObjectId::ObjectId(const ObjectId& copy)
|
||||
{
|
||||
memcpy(_id, copy._id, sizeof(_id));
|
||||
}
|
||||
|
||||
ObjectId::~ObjectId()
|
||||
{
|
||||
|
Reference in New Issue
Block a user