| 1 | // |
|---|---|
| 2 | // ObjectId.cpp |
| 3 | // |
| 4 | // Library: MongoDB |
| 5 | // Package: MongoDB |
| 6 | // Module: ObjectId |
| 7 | // |
| 8 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/MongoDB/ObjectId.h" |
| 16 | #include "Poco/Format.h" |
| 17 | #include <cstring> |
| 18 | |
| 19 | |
| 20 | namespace Poco { |
| 21 | namespace MongoDB { |
| 22 | |
| 23 | |
| 24 | ObjectId::ObjectId() |
| 25 | { |
| 26 | std::memset(_id, 0, sizeof(_id)); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | ObjectId::ObjectId(const std::string& id) |
| 31 | { |
| 32 | poco_assert_dbg(id.size() == 24); |
| 33 | |
| 34 | const char* p = id.c_str(); |
| 35 | for (std::size_t i = 0; i < 12; ++i) |
| 36 | { |
| 37 | _id[i] = fromHex(p); |
| 38 | p += 2; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | |
| 43 | ObjectId::ObjectId(const ObjectId& copy) |
| 44 | { |
| 45 | std::memcpy(_id, copy._id, sizeof(_id)); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | ObjectId::~ObjectId() |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | |
| 54 | std::string ObjectId::toString(const std::string& fmt) const |
| 55 | { |
| 56 | std::string s; |
| 57 | |
| 58 | for (int i = 0; i < 12; ++i) |
| 59 | { |
| 60 | s += format(fmt, (unsigned int) _id[i]); |
| 61 | } |
| 62 | return s; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | } } // namespace Poco::MongoDB |
| 67 |