1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/uuid/uuid.hpp

286 lines
8.7 KiB
C++
Raw Normal View History

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)
=============================================================================*/
#ifndef SPROUT_UUID_UUID_HPP
#define SPROUT_UUID_UUID_HPP
#include <cstddef>
#include <utility>
#include <stdexcept>
#include <type_traits>
#include <sprout/config.hpp>
#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>
#include <sprout/algorithm/cxx14/fill_n.hpp>
#include <sprout/algorithm/cxx14/swap_ranges.hpp>
#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) {
return first == last ? true
: !*first && sprout::uuids::detail::is_nil(sprout::next(first), last)
;
}
} // namespace detail
//
// uuid
//
class uuid {
public:
typedef std::uint8_t value_type;
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
typedef sprout::index_iterator<uuid&, true> iterator;
typedef sprout::index_iterator<uuid const&, true> const_iterator;
#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:
SPROUT_CXX14_CONSTEXPR void fill(const_reference value) {
sprout::fill_n(begin(), size(), value);
}
SPROUT_CXX14_CONSTEXPR void swap(uuid& other) SPROUT_NOEXCEPT {
sprout::swap_ranges(other.begin(), other.end(), begin());
}
// iterators:
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
SPROUT_CXX14_CONSTEXPR iterator begin() SPROUT_NOEXCEPT {
return iterator(*this, 0);
}
SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT {
return const_iterator(*this, 0);
}
SPROUT_CXX14_CONSTEXPR iterator end() SPROUT_NOEXCEPT {
return iterator(*this, size());
}
SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT {
return const_iterator(*this, size());
}
#else
SPROUT_CXX14_CONSTEXPR iterator begin() SPROUT_NOEXCEPT {
return &elems[0];
}
SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT {
return &elems[0];
}
SPROUT_CXX14_CONSTEXPR iterator end() SPROUT_NOEXCEPT {
return &elems[0] + size();
}
SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT {
return &elems[0] + size();
}
#endif
SPROUT_CXX14_CONSTEXPR reverse_iterator rbegin() SPROUT_NOEXCEPT {
return reverse_iterator(end());
}
SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT {
return const_reverse_iterator(end());
}
SPROUT_CXX14_CONSTEXPR reverse_iterator rend() SPROUT_NOEXCEPT {
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:
SPROUT_CXX14_CONSTEXPR reference operator[](size_type i) {
return elems[i];
}
SPROUT_CONSTEXPR const_reference operator[](size_type i) const {
return elems[i];
}
SPROUT_CXX14_CONSTEXPR reference at(size_type i) {
return i < size()
? elems[i]
: (throw std::out_of_range("uuid: index out of range"), elems[i])
;
}
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])
;
}
SPROUT_CXX14_CONSTEXPR reference front() {
return elems[0];
}
SPROUT_CONSTEXPR const_reference front() const {
return elems[0];
}
SPROUT_CXX14_CONSTEXPR reference back() {
return elems[size() - 1];
}
SPROUT_CONSTEXPR const_reference back() const {
return elems[size() - 1];
}
SPROUT_CXX14_CONSTEXPR pointer data() SPROUT_NOEXCEPT {
return &elems[0];
}
SPROUT_CONSTEXPR const_pointer data() const SPROUT_NOEXCEPT {
return &elems[0];
}
// others:
SPROUT_CXX14_CONSTEXPR pointer c_array() SPROUT_NOEXCEPT {
return &elems[0];
}
SPROUT_CONSTEXPR const_pointer c_array() SPROUT_NOEXCEPT {
return &elems[0];
}
SPROUT_CXX14_CONSTEXPR void assign(const_reference value) {
fill(value);
}
SPROUT_CXX14_CONSTEXPR void rangecheck(size_type i) const {
if (i >= size()) {
throw std::out_of_range("uuid: index out of range");
}
}
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);
}
//
// swap
//
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);
}
} // namespace uuids
using sprout::uuids::uuid;
} // namespace sprout
#endif // #ifndef SPROUT_UUID_UUID_HPP