From cdebc5b78de532dbd6bc7c865991665b226f688f Mon Sep 17 00:00:00 2001 From: Marius Bancila Date: Wed, 23 Feb 2022 18:24:00 +0200 Subject: [PATCH] pragma once replaced added constexpr --- P0959.md | 56 +++++++++++++++++--------------------------------- include/uuid.h | 17 +++++++-------- 2 files changed, 28 insertions(+), 45 deletions(-) diff --git a/P0959.md b/P0959.md index a1c0c42..e36faea 100644 --- a/P0959.md +++ b/P0959.md @@ -105,6 +105,7 @@ P0959R2 was discussed by LEWGI in Kona with the following feedback: Based on this feedback and further considerations, the following changes have been done in this version: * Added detailed explanation of the algorithms for generating uuids. * `from_string()` and non-member `swap()` declared with `noexcept`. +* `from_string()` and `is_valid_uuid()` declared with `constexpr`. * The `uuid` type is now defined as a class. * Added an exposition-only member to hint and the possible internal representation of the uuid data. * Added `uuid` constructors from arrays. @@ -506,21 +507,11 @@ namespace std { constexpr std::strong_ordering operator<=>(uuid const&) const noexcept = default; - template - static bool is_valid_uuid(CharT const * str) noexcept; - - template, - class Allocator = std::allocator> - static bool is_valid_uuid(std::basic_string const & str) noexcept; - - template - static uuid from_string(CharT const * str) noexcept; - - template, - class Allocator = std::allocator> - static uuid from_string(std::basic_string const & str) noexcept; + template + constexpr static bool is_valid_uuid(StringType const & str) noexcept; + + template + constexpr static uuid from_string(StringType const & str) noexcept; private: template friend std::basic_ostream & operator<<(std::basic_ostream &s, uuid const & id); @@ -598,13 +589,8 @@ namespace std { public: explicit uuid_name_generator(uuid const& namespace_uuid) noexcept; - template - uuid operator()(CharT const * name); - - template, - class Allocator = std::allocator> - uuid operator()(std::basic_string const & name); + template + uuid operator()(StringType const & name); }; } ``` @@ -714,21 +700,11 @@ namespace std { constexpr std::span as_bytes() const; - template - static bool is_valid_uuid(CharT const * str) noexcept; - - template, - class Allocator = std::allocator> - static bool is_valid_uuid(std::basic_string const & str) noexcept; - - template - static uuid from_string(CharT const * str) noexcept; - - template, - class Allocator = std::allocator> - static uuid from_string(std::basic_string const & str) noexcept; + template + constexpr static bool is_valid_uuid(StringType const & str) noexcept; + + template + constexpr static uuid from_string(StringType const & str) noexcept; private: template friend std::basic_ostream & operator<<(std::basic_ostream &s, uuid const & id); @@ -841,6 +817,12 @@ auto id1 = uuid::from_string>( std::string str{ "47183823-2574-4bfd-b411-99ed177d3e43" }; auto id2 = uuid::from_string>(str); // [2] ``` +However, to simplify the definition, these two overloads have been replaced with a single one: +``` +template +constexpr static std::optional from_string(StringType const & str); +``` +The `is_valid_uuid()` function and the `uuid_name_generator`'s call operator are now defined in the same manner. #### Need more explanations about the choices of ordering and how the RFC works in that regard. diff --git a/include/uuid.h b/include/uuid.h index 600846f..670acb0 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef STDUUID_H +#define STDUUID_H #include #include @@ -62,7 +63,7 @@ namespace uuids namespace detail { template - constexpr inline unsigned char hex2char(TChar const ch) + constexpr inline unsigned char hex2char(TChar const ch) noexcept { if (ch >= static_cast('0') && ch <= static_cast('9')) return static_cast(ch - static_cast('0')); @@ -74,7 +75,7 @@ namespace uuids } template - constexpr inline bool is_hex(TChar const ch) + constexpr inline bool is_hex(TChar const ch) noexcept { return (ch >= static_cast('0') && ch <= static_cast('9')) || @@ -83,17 +84,15 @@ namespace uuids } template - constexpr std::basic_string_view to_string_view(TChar const * str) + constexpr std::basic_string_view to_string_view(TChar const * str) noexcept { if (str) return str; return {}; } template - constexpr std::basic_string_view< - typename StringType::value_type, - typename StringType::traits_type> - to_string_view(StringType const & str) + constexpr std::basic_string_view + to_string_view(StringType const & str) noexcept { return str; } @@ -947,3 +946,5 @@ namespace std } }; } + +#endif /* STDUUID_H */ \ No newline at end of file