From 3955daf23b34eeaa021120bf74211e4e29d04cf2 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Wed, 8 Aug 2012 22:27:06 +0900 Subject: [PATCH] add uuid/md5_name_generator, sha1_name_generator --- sprout/checksum/md5.hpp | 1 - sprout/endian_traits.hpp | 88 ++++++++++++++++++++--------- sprout/iterator/bytes_iterator.hpp | 2 +- sprout/uuid/md5_name_generator.hpp | 72 +++++++++++++++++++++++ sprout/uuid/name_generator.hpp | 61 +------------------- sprout/uuid/sha1_name_generator.hpp | 72 +++++++++++++++++++++++ 6 files changed, 208 insertions(+), 88 deletions(-) create mode 100644 sprout/uuid/md5_name_generator.hpp create mode 100644 sprout/uuid/sha1_name_generator.hpp diff --git a/sprout/checksum/md5.hpp b/sprout/checksum/md5.hpp index f5c0a704..142a4fb0 100644 --- a/sprout/checksum/md5.hpp +++ b/sprout/checksum/md5.hpp @@ -12,7 +12,6 @@ #include #include #include -#include namespace sprout { static_assert(CHAR_BIT == 8, "CHAR_BIT == 8"); diff --git a/sprout/endian_traits.hpp b/sprout/endian_traits.hpp index 13839d43..2610b0c8 100644 --- a/sprout/endian_traits.hpp +++ b/sprout/endian_traits.hpp @@ -3,45 +3,77 @@ #include #include +#include #include namespace sprout { + namespace detail { + template + class default_big_endian_traits; + template + class default_big_endian_traits< + T, + typename std::enable_if::value>::type + > { + public: + typedef T type; + public: + static SPROUT_CONSTEXPR std::size_t size() { + return sizeof(type); + } + static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) { + return static_cast( + (t & (UCHAR_MAX << CHAR_BIT * ((size() - 1) - i))) + >> CHAR_BIT * ((size() - 1) - i) + ); + } + }; + + template + class default_little_endian_traits; + template + class default_little_endian_traits< + T, + typename std::enable_if::value>::type + > { + public: + typedef T type; + public: + static SPROUT_CONSTEXPR std::size_t size() { + return sizeof(type); + } + static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) { + return static_cast( + (t & (UCHAR_MAX << CHAR_BIT * i)) + >> CHAR_BIT * i + ); + } + }; + } // namespace detail + // // big_endian_traits // template - class big_endian_traits { - public: - typedef T type; - public: - static SPROUT_CONSTEXPR std::size_t size() { - return sizeof(type); - } - static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) { - return static_cast( - (t & (UCHAR_MAX << CHAR_BIT * ((size() - 1) - i))) - >> CHAR_BIT * ((size() - 1) - i) - ); - } - }; + class big_endian_traits + : public sprout::detail::default_big_endian_traits + {}; + // // little_endian_traits // template - class little_endian_traits { - public: - typedef T type; - public: - static SPROUT_CONSTEXPR std::size_t size() { - return sizeof(type); - } - static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) { - return static_cast( - (t & (UCHAR_MAX << CHAR_BIT * i)) - >> CHAR_BIT * i - ); - } - }; + class little_endian_traits + : public sprout::detail::default_little_endian_traits + {}; + + // + // endian_traits + // + template + class endian_traits + : public sprout::big_endian_traits + {}; } // namespace sprout #endif // #ifndef SPROUT_ENDIAN_TRAITS_HPP diff --git a/sprout/iterator/bytes_iterator.hpp b/sprout/iterator/bytes_iterator.hpp index 4fcaf506..4192e3c4 100644 --- a/sprout/iterator/bytes_iterator.hpp +++ b/sprout/iterator/bytes_iterator.hpp @@ -15,7 +15,7 @@ namespace sprout { // template< typename Iterator, - typename Traits = sprout::big_endian_traits::value_type> + typename Traits = sprout::endian_traits::value_type> > class bytes_iterator : public std::iterator< diff --git a/sprout/uuid/md5_name_generator.hpp b/sprout/uuid/md5_name_generator.hpp new file mode 100644 index 00000000..1f5c7413 --- /dev/null +++ b/sprout/uuid/md5_name_generator.hpp @@ -0,0 +1,72 @@ +#ifndef SPROUT_UUID_MD5_NAME_GENERATOR_HPP +#define SPROUT_UUID_MD5_NAME_GENERATOR_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace uuids { + // + // md5_name_generator + // + class md5_name_generator { + public: + typedef sprout::uuids::uuid result_type; + private: + typedef typename result_type::value_type value_type; + private: + sprout::md5 sum_; + private: + SPROUT_CONSTEXPR result_type sha_to_uuid_1(sprout::md5::value_type const& value) const { + return result_type{{ + value[0], + value[1], + value[2], + value[3], + value[4], + value[5], + static_cast((value[6] & 0x5F) | 0x50), + value[7], + static_cast((value[8] & 0xBF) | 0x80), + value[9], + value[10], + value[11], + value[12], + value[13], + value[14], + value[15] + }}; + } + SPROUT_CONSTEXPR result_type sha_to_uuid(sprout::md5 const& sha) const { + return sha_to_uuid_1(sha.checksum()); + } + public: + SPROUT_CONSTEXPR md5_name_generator() + : sum_(sprout::md5::const_type().process_range(sprout::uuids::uuid{{0}})) + {} + explicit SPROUT_CONSTEXPR md5_name_generator(sprout::uuids::uuid const& namespace_uuid) + : sum_(sprout::md5::const_type().process_range(namespace_uuid)) + {} + template + SPROUT_CONSTEXPR result_type operator()(sprout::basic_string const& name) const { + return sha_to_uuid(sum_.process_range(name)); + } + SPROUT_CONSTEXPR result_type operator()(char const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + SPROUT_CONSTEXPR result_type operator()(wchar_t const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + SPROUT_CONSTEXPR result_type operator()(char16_t const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + SPROUT_CONSTEXPR result_type operator()(char32_t const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + }; + } // namespace uuids +} // namespace sprout + +#endif // #ifndef SPROUT_UUID_MD5_NAME_GENERATOR_HPP diff --git a/sprout/uuid/name_generator.hpp b/sprout/uuid/name_generator.hpp index 0eaac54f..c828fc96 100644 --- a/sprout/uuid/name_generator.hpp +++ b/sprout/uuid/name_generator.hpp @@ -2,70 +2,15 @@ #define SPROUT_UUID_NAME_GENERATOR_HPP #include -#include -#include -#include +#include +#include namespace sprout { namespace uuids { // // name_generator // - class name_generator { - public: - typedef sprout::uuids::uuid result_type; - private: - typedef typename result_type::value_type value_type; - private: - sprout::sha1 sha_; - private: - SPROUT_CONSTEXPR result_type sha_to_uuid_1(sprout::sha1::value_type const& value) const { - return result_type{{ - value[0], - value[1], - value[2], - value[3], - value[4], - value[5], - static_cast((value[6] & 0x5F) | 0x50), - value[7], - static_cast((value[8] & 0xBF) | 0x80), - value[9], - value[10], - value[11], - value[12], - value[13], - value[14], - value[15] - }}; - } - SPROUT_CONSTEXPR result_type sha_to_uuid(sprout::sha1 const& sha) const { - return sha_to_uuid_1(sha.checksum()); - } - public: - SPROUT_CONSTEXPR name_generator() - : sha_(sprout::sha1::const_type().process_range(sprout::uuids::uuid{{0}})) - {} - explicit SPROUT_CONSTEXPR name_generator(sprout::uuids::uuid const& namespace_uuid) - : sha_(sprout::sha1::const_type().process_range(namespace_uuid)) - {} - template - SPROUT_CONSTEXPR result_type operator()(sprout::basic_string const& name) const { - return sha_to_uuid(sha_.process_range(name)); - } - SPROUT_CONSTEXPR result_type operator()(char const* name) const { - return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits::length(name))); - } - SPROUT_CONSTEXPR result_type operator()(wchar_t const* name) const { - return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits::length(name))); - } - SPROUT_CONSTEXPR result_type operator()(char16_t const* name) const { - return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits::length(name))); - } - SPROUT_CONSTEXPR result_type operator()(char32_t const* name) const { - return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits::length(name))); - } - }; + typedef sprout::uuids::sha1_name_generator name_generator; } // namespace uuids } // namespace sprout diff --git a/sprout/uuid/sha1_name_generator.hpp b/sprout/uuid/sha1_name_generator.hpp new file mode 100644 index 00000000..6d1e375d --- /dev/null +++ b/sprout/uuid/sha1_name_generator.hpp @@ -0,0 +1,72 @@ +#ifndef SPROUT_UUID_SHA1_NAME_GENERATOR_HPP +#define SPROUT_UUID_SHA1_NAME_GENERATOR_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace uuids { + // + // sha1_name_generator + // + class sha1_name_generator { + public: + typedef sprout::uuids::uuid result_type; + private: + typedef typename result_type::value_type value_type; + private: + sprout::sha1 sum_; + private: + SPROUT_CONSTEXPR result_type sha_to_uuid_1(sprout::sha1::value_type const& value) const { + return result_type{{ + value[0], + value[1], + value[2], + value[3], + value[4], + value[5], + static_cast((value[6] & 0x5F) | 0x50), + value[7], + static_cast((value[8] & 0xBF) | 0x80), + value[9], + value[10], + value[11], + value[12], + value[13], + value[14], + value[15] + }}; + } + SPROUT_CONSTEXPR result_type sha_to_uuid(sprout::sha1 const& sha) const { + return sha_to_uuid_1(sha.checksum()); + } + public: + SPROUT_CONSTEXPR sha1_name_generator() + : sum_(sprout::sha1::const_type().process_range(sprout::uuids::uuid{{0}})) + {} + explicit SPROUT_CONSTEXPR sha1_name_generator(sprout::uuids::uuid const& namespace_uuid) + : sum_(sprout::sha1::const_type().process_range(namespace_uuid)) + {} + template + SPROUT_CONSTEXPR result_type operator()(sprout::basic_string const& name) const { + return sha_to_uuid(sum_.process_range(name)); + } + SPROUT_CONSTEXPR result_type operator()(char const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + SPROUT_CONSTEXPR result_type operator()(wchar_t const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + SPROUT_CONSTEXPR result_type operator()(char16_t const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + SPROUT_CONSTEXPR result_type operator()(char32_t const* name) const { + return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits::length(name))); + } + }; + } // namespace uuids +} // namespace sprout + +#endif // #ifndef SPROUT_UUID_SHA1_NAME_GENERATOR_HPP