uuid.hpp 2.53 KB
Newer Older
Robert Kimball's avatar
Robert Kimball committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------

#pragma once

#include <array>
18
#include <cstring>
Robert Kimball's avatar
Robert Kimball committed
19 20
#include <iomanip>
#include <iostream>
21
#include <random>
Robert Kimball's avatar
Robert Kimball committed
22 23 24

static std::mt19937_64 random_generator;

25 26 27 28 29 30
namespace ngraph
{
    class uuid_type;
}

class ngraph::uuid_type
Robert Kimball's avatar
Robert Kimball committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
{
public:
    uuid_type()
    {
        m_data[0]  = random_generator();
        m_data[1]  = random_generator();
        uint8_t* p = (uint8_t*)m_data;
        p[6]       = (p[6] & 0x0F) | 0x40;
        p[8]       = (p[8] & 0x3F) | 0x80;
    }

    std::string to_string() const
    {
        std::stringstream ss;
        uint8_t*          p = (uint8_t*)m_data;
        for (int i = 0; i < 4; i++)
            ss << std::hex << std::setw(2) << std::setfill('0') << (int)*p++;
        ss << "-";
        for (int i = 0; i < 2; i++)
            ss << std::hex << std::setw(2) << std::setfill('0') << (int)*p++;
        ss << "-";
        for (int i = 0; i < 2; i++)
            ss << std::hex << std::setw(2) << std::setfill('0') << (int)*p++;
        ss << "-";
        for (int i = 0; i < 2; i++)
            ss << std::hex << std::setw(2) << std::setfill('0') << (int)*p++;
        ss << "-";
        for (int i = 0; i < 6; i++)
            ss << std::hex << std::setw(2) << std::setfill('0') << (int)*p++;
        return ss.str();
    }

    static uuid_type zero()
    {
        uuid_type rc;
        rc.m_data[0] = 0;
        rc.m_data[1] = 0;
        return rc;
    }

    bool operator==(const uuid_type& other) const
    {
        return memcmp((char*)m_data, (char*)other.m_data, 16) == 0;
    }

76
    bool operator!=(const uuid_type& other) const { return !(*this == other); }
Robert Kimball's avatar
Robert Kimball committed
77 78 79 80 81 82 83 84 85
    friend std::ostream& operator<<(std::ostream& out, const uuid_type& id)
    {
        out << id.to_string();
        return out;
    }

private:
    uint64_t m_data[2];
};