aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-crt-cpp/source/UUID.cpp
blob: c985ea27787e3d33cdc8b348ae6389f3af3a6335 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/crt/UUID.h>

namespace Aws
{
    namespace Crt
    {
        UUID::UUID() noexcept : m_good(false)
        {
            if (aws_uuid_init(&m_uuid) == AWS_OP_SUCCESS)
            {
                m_good = true;
            }
        }

        UUID::UUID(const String &str) noexcept : m_good(false)
        {
            auto strCur = aws_byte_cursor_from_c_str(str.c_str());
            if (aws_uuid_init_from_str(&m_uuid, &strCur) == AWS_OP_SUCCESS)
            {
                m_good = true;
            }
        }

        UUID &UUID::operator=(const String &str) noexcept
        {
            *this = UUID(str);
            return *this;
        }

        bool UUID::operator==(const UUID &other) noexcept { return aws_uuid_equals(&m_uuid, &other.m_uuid); }

        bool UUID::operator!=(const UUID &other) noexcept { return !aws_uuid_equals(&m_uuid, &other.m_uuid); }

        String UUID::ToString() const
        {
            String uuidStr;
            uuidStr.resize(AWS_UUID_STR_LEN);
            auto outBuf = ByteBufFromEmptyArray(reinterpret_cast<const uint8_t *>(uuidStr.data()), uuidStr.capacity());
            aws_uuid_to_str(&m_uuid, &outBuf);
            uuidStr.resize(outBuf.len);
            return uuidStr;
        }

        UUID::operator String() const { return ToString(); }

        UUID::operator ByteBuf() const noexcept { return ByteBufFromArray(m_uuid.uuid_data, sizeof(m_uuid.uuid_data)); }

        int UUID::GetLastError() const noexcept { return aws_last_error(); }
    } // namespace Crt
} // namespace Aws