pragma once replaced

added constexpr
This commit is contained in:
Marius Bancila 2022-02-23 18:24:00 +02:00
parent 5c538cca02
commit cdebc5b78d
2 changed files with 28 additions and 45 deletions

View File

@ -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: 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. * Added detailed explanation of the algorithms for generating uuids.
* `from_string()` and non-member `swap()` declared with `noexcept`. * `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. * 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 an exposition-only member to hint and the possible internal representation of the uuid data.
* Added `uuid` constructors from arrays. * Added `uuid` constructors from arrays.
@ -506,21 +507,11 @@ namespace std {
constexpr std::strong_ordering operator<=>(uuid const&) const noexcept = default; constexpr std::strong_ordering operator<=>(uuid const&) const noexcept = default;
template<class CharT = char> template<typename StringType>
static bool is_valid_uuid(CharT const * str) noexcept; constexpr static bool is_valid_uuid(StringType const & str) noexcept;
template<class CharT = char, template<typename StringType>
class Traits = std::char_traits<CharT>, constexpr static uuid from_string(StringType const & str) noexcept;
class Allocator = std::allocator<CharT>>
static bool is_valid_uuid(std::basic_string<CharT, Traits, Allocator> const & str) noexcept;
template<class CharT = char>
static uuid from_string(CharT const * str) noexcept;
template<class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
static uuid from_string(std::basic_string<CharT, Traits, Allocator> const & str) noexcept;
private: private:
template <class Elem, class Traits> template <class Elem, class Traits>
friend std::basic_ostream<Elem, Traits> & operator<<(std::basic_ostream<Elem, Traits> &s, uuid const & id); friend std::basic_ostream<Elem, Traits> & operator<<(std::basic_ostream<Elem, Traits> &s, uuid const & id);
@ -598,13 +589,8 @@ namespace std {
public: public:
explicit uuid_name_generator(uuid const& namespace_uuid) noexcept; explicit uuid_name_generator(uuid const& namespace_uuid) noexcept;
template<class CharT = char> template<typename StringType>
uuid operator()(CharT const * name); uuid operator()(StringType const & name);
template<class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
uuid operator()(std::basic_string<CharT, Traits, Allocator> const & name);
}; };
} }
``` ```
@ -714,21 +700,11 @@ namespace std {
constexpr std::span<std::byte const, 16> as_bytes() const; constexpr std::span<std::byte const, 16> as_bytes() const;
template<class CharT = char> template<typename StringType>
static bool is_valid_uuid(CharT const * str) noexcept; constexpr static bool is_valid_uuid(StringType const & str) noexcept;
template<class CharT = char, template<typename StringType>
class Traits = std::char_traits<CharT>, constexpr static uuid from_string(StringType const & str) noexcept;
class Allocator = std::allocator<CharT>>
static bool is_valid_uuid(std::basic_string<CharT, Traits, Allocator> const & str) noexcept;
template<class CharT = char>
static uuid from_string(CharT const * str) noexcept;
template<class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
static uuid from_string(std::basic_string<CharT, Traits, Allocator> const & str) noexcept;
private: private:
template <class Elem, class Traits> template <class Elem, class Traits>
friend std::basic_ostream<Elem, Traits> & operator<<(std::basic_ostream<Elem, Traits> &s, uuid const & id); friend std::basic_ostream<Elem, Traits> & operator<<(std::basic_ostream<Elem, Traits> &s, uuid const & id);
@ -841,6 +817,12 @@ auto id1 = uuid::from_string<char, std::char_traits<char>>(
std::string str{ "47183823-2574-4bfd-b411-99ed177d3e43" }; std::string str{ "47183823-2574-4bfd-b411-99ed177d3e43" };
auto id2 = uuid::from_string<char, std::char_traits<char>>(str); // [2] auto id2 = uuid::from_string<char, std::char_traits<char>>(str); // [2]
``` ```
However, to simplify the definition, these two overloads have been replaced with a single one:
```
template <typename StringType>
constexpr static std::optional<uuid> 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. #### Need more explanations about the choices of ordering and how the RFC works in that regard.

View File

@ -1,4 +1,5 @@
#pragma once #ifndef STDUUID_H
#define STDUUID_H
#include <cstring> #include <cstring>
#include <string> #include <string>
@ -62,7 +63,7 @@ namespace uuids
namespace detail namespace detail
{ {
template <typename TChar> template <typename TChar>
constexpr inline unsigned char hex2char(TChar const ch) constexpr inline unsigned char hex2char(TChar const ch) noexcept
{ {
if (ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9')) if (ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9'))
return static_cast<unsigned char>(ch - static_cast<TChar>('0')); return static_cast<unsigned char>(ch - static_cast<TChar>('0'));
@ -74,7 +75,7 @@ namespace uuids
} }
template <typename TChar> template <typename TChar>
constexpr inline bool is_hex(TChar const ch) constexpr inline bool is_hex(TChar const ch) noexcept
{ {
return return
(ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9')) || (ch >= static_cast<TChar>('0') && ch <= static_cast<TChar>('9')) ||
@ -83,17 +84,15 @@ namespace uuids
} }
template <typename TChar> template <typename TChar>
constexpr std::basic_string_view<TChar> to_string_view(TChar const * str) constexpr std::basic_string_view<TChar> to_string_view(TChar const * str) noexcept
{ {
if (str) return str; if (str) return str;
return {}; return {};
} }
template <typename StringType> template <typename StringType>
constexpr std::basic_string_view< constexpr std::basic_string_view<typename StringType::value_type, typename StringType::traits_type>
typename StringType::value_type, to_string_view(StringType const & str) noexcept
typename StringType::traits_type>
to_string_view(StringType const & str)
{ {
return str; return str;
} }
@ -947,3 +946,5 @@ namespace std
} }
}; };
} }
#endif /* STDUUID_H */