2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-10-25 09:16:27 +00:00
|
|
|
#ifndef SPROUT_UUID_UUID_HPP
|
|
|
|
#define SPROUT_UUID_UUID_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <utility>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2012-09-21 06:43:30 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
|
|
|
#include <sprout/iterator/reverse_iterator.hpp>
|
2013-01-11 19:08:44 +00:00
|
|
|
#include <sprout/algorithm/equal.hpp>
|
|
|
|
#include <sprout/algorithm/lexicographical_compare.hpp>
|
2013-10-31 09:57:41 +00:00
|
|
|
#include <sprout/algorithm/cxx14/fill_n.hpp>
|
|
|
|
#include <sprout/algorithm/cxx14/swap_ranges.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
|
|
|
|
# include <sprout/iterator/index_iterator.hpp>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace uuids {
|
|
|
|
namespace detail {
|
|
|
|
template<typename InputIterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_nil(InputIterator first, InputIterator last) {
|
2011-10-25 09:16:27 +00:00
|
|
|
return first == last ? true
|
|
|
|
: !*first && sprout::uuids::detail::is_nil(sprout::next(first), last)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// uuid
|
|
|
|
//
|
|
|
|
class uuid {
|
|
|
|
public:
|
2011-10-26 06:57:31 +00:00
|
|
|
typedef std::uint8_t value_type;
|
2011-10-25 09:16:27 +00:00
|
|
|
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
|
2013-02-18 17:49:10 +00:00
|
|
|
typedef sprout::index_iterator<uuid&, true> iterator;
|
|
|
|
typedef sprout::index_iterator<uuid const&, true> const_iterator;
|
2011-10-25 09:16:27 +00:00
|
|
|
#else
|
|
|
|
typedef std::uint8_t* iterator;
|
|
|
|
typedef std::uint8_t const* const_iterator;
|
|
|
|
#endif
|
|
|
|
typedef std::uint8_t& reference;
|
|
|
|
typedef std::uint8_t const& const_reference;
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
typedef std::uint8_t* pointer;
|
|
|
|
typedef std::uint8_t const* const_pointer;
|
|
|
|
typedef sprout::reverse_iterator<iterator> reverse_iterator;
|
|
|
|
typedef sprout::reverse_iterator<const_iterator> const_reverse_iterator;
|
|
|
|
public:
|
|
|
|
//
|
|
|
|
// variant_type
|
|
|
|
//
|
|
|
|
enum variant_type {
|
|
|
|
variant_ncs,
|
|
|
|
variant_rfc_4122,
|
|
|
|
variant_microsoft,
|
|
|
|
variant_future
|
|
|
|
};
|
|
|
|
//
|
|
|
|
// version_type
|
|
|
|
//
|
|
|
|
enum version_type {
|
|
|
|
version_unknown = -1,
|
|
|
|
version_time_based = 1,
|
|
|
|
version_dce_security = 2,
|
|
|
|
version_name_based_md5 = 3,
|
|
|
|
version_random_number_based = 4,
|
|
|
|
version_name_based_sha1 = 5
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
SPROUT_STATIC_CONSTEXPR size_type static_size = 16;
|
|
|
|
public:
|
|
|
|
value_type elems[static_size];
|
|
|
|
public:
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void fill(const_reference value) {
|
2013-10-31 09:57:41 +00:00
|
|
|
sprout::fill_n(begin(), size(), value);
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void swap(uuid& other) SPROUT_NOEXCEPT {
|
2013-10-31 09:57:41 +00:00
|
|
|
sprout::swap_ranges(other.begin(), other.end(), begin());
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
|
|
|
// iterators:
|
|
|
|
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR iterator begin() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return iterator(*this, 0);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT {
|
|
|
|
return const_iterator(*this, 0);
|
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR iterator end() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return iterator(*this, size());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT {
|
|
|
|
return const_iterator(*this, size());
|
|
|
|
}
|
|
|
|
#else
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR iterator begin() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return &elems[0];
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT {
|
|
|
|
return &elems[0];
|
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR iterator end() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return &elems[0] + size();
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT {
|
|
|
|
return &elems[0] + size();
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator rbegin() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return reverse_iterator(end());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT {
|
|
|
|
return const_reverse_iterator(end());
|
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator rend() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return reverse_iterator(begin());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_reverse_iterator rend() const SPROUT_NOEXCEPT {
|
|
|
|
return const_reverse_iterator(begin());
|
|
|
|
}
|
|
|
|
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
|
|
|
|
SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT {
|
|
|
|
return const_iterator(*this, 0);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
|
|
|
|
return const_iterator(*this, size());
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT {
|
|
|
|
return &elems[0];
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
|
|
|
|
return &elems[0] + size();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT {
|
|
|
|
return const_reverse_iterator(end());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT {
|
|
|
|
return const_reverse_iterator(begin());
|
|
|
|
}
|
|
|
|
// capacity:
|
|
|
|
SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT {
|
|
|
|
return static_size;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT {
|
|
|
|
return size();
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
// element access:
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reference operator[](size_type i) {
|
2011-10-25 09:16:27 +00:00
|
|
|
return elems[i];
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_reference operator[](size_type i) const {
|
|
|
|
return elems[i];
|
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reference at(size_type i) {
|
2011-10-25 13:04:54 +00:00
|
|
|
return i < size()
|
|
|
|
? elems[i]
|
|
|
|
: (throw std::out_of_range("uuid: index out of range"), elems[i])
|
|
|
|
;
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2011-10-25 13:04:54 +00:00
|
|
|
SPROUT_CONSTEXPR const_reference at(size_type i) const {
|
|
|
|
return i < size()
|
|
|
|
? elems[i]
|
|
|
|
: (throw std::out_of_range("uuid: index out of range"), elems[i])
|
|
|
|
;
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reference front() {
|
2011-10-25 09:16:27 +00:00
|
|
|
return elems[0];
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_reference front() const {
|
|
|
|
return elems[0];
|
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reference back() {
|
2011-10-25 09:16:27 +00:00
|
|
|
return elems[size() - 1];
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_reference back() const {
|
|
|
|
return elems[size() - 1];
|
|
|
|
}
|
|
|
|
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR pointer data() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return &elems[0];
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR const_pointer data() const SPROUT_NOEXCEPT {
|
|
|
|
return &elems[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// others:
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR pointer c_array() SPROUT_NOEXCEPT {
|
2011-10-25 09:16:27 +00:00
|
|
|
return &elems[0];
|
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CONSTEXPR const_pointer c_array() SPROUT_NOEXCEPT {
|
|
|
|
return &elems[0];
|
|
|
|
}
|
|
|
|
SPROUT_CXX14_CONSTEXPR void assign(const_reference value) {
|
2011-10-25 09:16:27 +00:00
|
|
|
fill(value);
|
|
|
|
}
|
2013-10-30 09:15:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void rangecheck(size_type i) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
if (i >= size()) {
|
2011-10-25 13:04:54 +00:00
|
|
|
throw std::out_of_range("uuid: index out of range");
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SPROUT_CONSTEXPR bool is_nil() const {
|
|
|
|
return sprout::uuids::detail::is_nil(begin(), end());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR variant_type variant() const {
|
|
|
|
return (elems[8] & 0x80) == 0x00 ? variant_ncs
|
|
|
|
: (elems[8] & 0xC0) == 0x80 ? variant_rfc_4122
|
|
|
|
: (elems[8] & 0xE0) == 0xC0 ? variant_microsoft
|
|
|
|
: variant_future
|
|
|
|
;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR version_type version() const {
|
|
|
|
return (elems[6] & 0xF0) == 0x10 ? version_time_based
|
|
|
|
: (elems[6] & 0xF0) == 0x20 ? version_dce_security
|
|
|
|
: (elems[6] & 0xF0) == 0x30 ? version_name_based_md5
|
|
|
|
: (elems[6] & 0xF0) == 0x40 ? version_random_number_based
|
|
|
|
: (elems[6] & 0xF0) == 0x50 ? version_name_based_sha1
|
|
|
|
: version_unknown
|
|
|
|
;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// operator==
|
|
|
|
// operator!=
|
|
|
|
// operator<
|
|
|
|
// operator>
|
|
|
|
// operator<=
|
|
|
|
// operator>=
|
|
|
|
//
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator==(sprout::uuids::uuid const& lhs, sprout::uuids::uuid const& rhs) {
|
2013-01-11 19:08:44 +00:00
|
|
|
return sprout::equal(lhs.begin(), lhs.end(), rhs.begin());
|
2012-10-06 04:53:07 +00:00
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator!=(sprout::uuids::uuid const& lhs, sprout::uuids::uuid const& rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<(sprout::uuids::uuid const& lhs, sprout::uuids::uuid const& rhs) {
|
2013-01-11 19:08:44 +00:00
|
|
|
return sprout::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
2012-10-06 04:53:07 +00:00
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>(sprout::uuids::uuid const& lhs, sprout::uuids::uuid const& rhs) {
|
|
|
|
return rhs < lhs;
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<=(sprout::uuids::uuid const& lhs, sprout::uuids::uuid const& rhs) {
|
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>=(sprout::uuids::uuid const& lhs, sprout::uuids::uuid const& rhs) {
|
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
2011-10-25 09:16:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// swap
|
|
|
|
//
|
2013-10-30 09:15:57 +00:00
|
|
|
inline SPROUT_CXX14_CONSTEXPR void
|
2012-10-06 04:53:07 +00:00
|
|
|
swap(sprout::uuids::uuid& lhs, sprout::uuids::uuid& rhs)
|
|
|
|
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
|
|
|
|
{
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
2011-10-25 09:16:27 +00:00
|
|
|
} // namespace uuids
|
|
|
|
|
|
|
|
using sprout::uuids::uuid;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_UUID_UUID_HPP
|