UUID.hpp 1.19 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
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
#ifndef __HEPNOS_UUID_UTIL_HPP
#define __HEPNOS_UUID_UTIL_HPP

#include <string>
#include <cstring>
#include <thallium/serialization/stl/string.hpp>
#include <boost/serialization/split_member.hpp>

namespace hepnos {

struct UUID {

    unsigned char data[16];

    public:

    UUID();
    UUID(const UUID& other) = default;
    UUID(UUID&& other) = default;
    UUID& operator=(const UUID& other) = default;
    UUID& operator=(UUID&& other) = default;

    std::string to_string() const;

    BOOST_SERIALIZATION_SPLIT_MEMBER()

    template<typename Archive>
    void save(Archive& a, const unsigned int version) const {
        (void)version;
        a & std::string(data, 16);
    }

    template<typename Archive>
    void load(Archive& a, const unsigned int version) {
        (void)version;
        std::string s;
        a & s;
        std::memcpy(data, s.data(), 16);
    }

    template<typename T>
    friend T& operator<<(T& stream, const UUID& id);

    static UUID generate();

    void randomize();
47 48

    bool operator==(const UUID& other) const;
Matthieu Dorier's avatar
Matthieu Dorier committed
49 50

    uint64_t hash() const;
Matthieu Dorier's avatar
Matthieu Dorier committed
51 52 53 54
};

template<typename T>
T& operator<<(T& stream, const UUID& id) {
55
    stream << id.to_string();
Matthieu Dorier's avatar
Matthieu Dorier committed
56 57 58 59 60 61
    return stream;
}

}

#endif