Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed May 1, 2024
1 parent 7677167 commit 835a658
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 33 deletions.
1 change: 1 addition & 0 deletions doc/uuid/changes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
* Added `uuid_clock`, a `<chrono>`-compatible clock with an epoch and a resolution as specified in RFC 4122.
* Accessors for the timestamp, the time point, the clock sequence, and the node identifier have been added to `uuid`.
* Improved the `what()` strings of the `std::runtime_error` exceptions thrown by `string_generator`.
* Added an overload of `to_chars` taking `wchar_t* first, wchar_t* last`.
137 changes: 110 additions & 27 deletions doc/uuid/uuid_io.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,81 +10,164 @@
namespace boost {
namespace uuids {
template <typename ch, typename char_traits>
std::basic_ostream<ch, char_traits>&
operator<<( std::basic_ostream<ch, char_traits> &os, uuid const& u );
// stream insertion
template <typename ch, typename char_traits>
std::basic_istream<ch, char_traits>&
operator>>( std::basic_istream<ch, char_traits> &is, uuid& u );
template<class Ch, class Traits>
std::basic_ostream<Ch, Traits>&
operator<<( std::basic_ostream<Ch, Traits>& os, uuid const& u );
// stream extraction
template<class Ch, class Traits>
std::basic_istream<Ch, Traits>&
operator>>( std::basic_istream<Ch, Traits>& is, uuid& u );
// to_chars
template<class OutputIterator>
OutputIterator to_chars( uuid const& u, OutputIterator out );
bool to_chars( uuid const& u, char* first, char* last );
bool to_chars( uuid const& u, char* first, char* last ) noexcept;
bool to_chars( uuid const& u, wchar_t* first, wchar_t* last ) noexcept;
// to_string
std::string to_string( uuid const& u );
std::wstring to_wstring( uuid const& u );
}} // namespace boost::uuids
----

=== Stream Operators
=== Stream Insertion

```
template<class Ch, class Traits>
std::basic_ostream<Ch, Traits>&
operator<<( std::basic_ostream<Ch, Traits>& os, uuid const& u );
```

Requires: :: `Ch` must be either `char` or `wchar_t`.

Effects: :: Inserts the string representation of `u` into the output stream `os`.
+
The string representation of a `uuid` is `hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh`, where `h` is a lowercase hexadecimal digit.

The standard input and output stream operators `<<` and `>>` are provided by including `boost/uuid/uuid_io.hpp`.
The string representation of a `uuid` is `hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh` where `h` is a hexadecimal digit.
NOTE: This operator also enables the use of
https://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast/synopsis.html[boost::lexical_cast]
to convert a `uuid` to a string.

```c++
Example: ::
+
```
using namespace boost::uuids;

uuid u1 = random_generator()();

std::cout << u1 << std::endl;

std::string s1 = boost::lexical_cast<std::string>( u1 );

std::cout << s1 << std::endl;
```

=== Stream Extraction

```
template<class Ch, class Traits>
std::basic_istream<Ch, Traits>&
operator>>( std::basic_istream<Ch, Traits>& is, uuid& u );
```

Requires: :: `Ch` must be either `char` or `wchar_t`.

Effects: :: Parses a `uuid` string representation from `is` and stores the result into `u`.

NOTE: This operator also enables the use of
https://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast/synopsis.html[boost::lexical_cast]
to convert a string to a `uuid`.

Example: ::
+
```
using namespace boost::uuids;

uuid u1 = random_generator()();

std::stringstream ss;
ss << u1;

uuid u2;
ss >> u2;
uuid u2 = boost::lexical_cast<uuid>( ss.str() );

assert( u1 == u2 );

uuid u3;
ss >> u3;

assert( u1 == u3 );
```

One can also use https://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast/synopsis.html[boost::lexical_cast].
=== to_chars

```
template<class OutputIterator>
OutputIterator to_chars( uuid const& u, OutputIterator out );
```

```c++
Effects: :: Outputs the string representation of `u` (exactly 36 characters of type `char`) to the output iterator `out`.

Example: ::
+
```
using namespace boost::uuids;

uuid u1 = random_generator()();

std::string s = boost::lexical_cast<std::string>( u1 );
uuid u2 = boost::lexical_cast<uuid>( s );
std::vector<char> v;

assert( u1 == u2 );
to_chars( u1, std::back_inserter( v ) );
```

=== to_chars
```
bool to_chars( uuid const& u, char* first, char* last ) noexcept;
```
```
bool to_chars( uuid const& u, wchar_t* first, wchar_t* last ) noexcept;
```

The function `to_chars` is a fast non-allocating (and non-throwing if the iterator does not throw) function to write a `uuid` to a string buffer.
It writes exactly 36 characters to the iterator on success (not null-terminated).
Effects: :: If `last - first >= 36`, writes the string representation of `u` (exactly 36 characters, not null terminated) into the buffer starting at `first` and returns `true`. Otherwise, returns `false`.

```c++
Example: ::
+
```
using namespace boost::uuids;

uuid u = random_generator()();

char buf[ 36 ];

to_chars( u, buf );
// OR
bool ret = to_chars( u, std::begin(buf), std::end(buf) );
bool ret = to_chars( u, std::begin( buf ), std::end( buf ) );
assert( ret );

std::cout << std::string( buf, 36 ) << std::endl;
```

=== to_string

The functions `to_string` and `to_wstring` are provided as a convenience to convert a `uuid` to a string.
They are also likely faster than the stream operators or using https://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast/synopsis.html[boost::lexical_cast].
They are likely to be more efficient than https://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast/synopsis.html[boost::lexical_cast].

```c++
```
std::string to_string( uuid const& u );
```
```
std::wstring to_wstring( uuid const& u );
```

Returns: :: A string containing the string representation of `u`.

Example: ::
+
```
using namespace boost::uuids;

uuid u = random_generator()();
Expand Down
13 changes: 7 additions & 6 deletions include/boost/uuid/uuid_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <istream>
#include <locale>
#include <algorithm>
#include <string>

#if defined(_MSC_VER)
#pragma warning(push) // Save warning settings.
Expand All @@ -24,7 +25,7 @@ namespace uuids {

namespace detail {

inline char to_char( unsigned i )
inline char to_char( unsigned i ) noexcept
{
if( i <= 9 )
{
Expand All @@ -37,7 +38,7 @@ inline char to_char( unsigned i )
}
}

inline wchar_t to_wchar( unsigned i )
inline wchar_t to_wchar( unsigned i ) noexcept
{
if( i <= 9 )
{
Expand All @@ -49,7 +50,7 @@ inline wchar_t to_wchar( unsigned i )
}
}

inline char* to_chars( uuid const& u, char* out )
inline char* to_chars( uuid const& u, char* out ) noexcept
{
std::size_t i = 0;

Expand All @@ -70,7 +71,7 @@ inline char* to_chars( uuid const& u, char* out )
return out;
}

inline wchar_t* to_chars( uuid const& u, wchar_t* out )
inline wchar_t* to_chars( uuid const& u, wchar_t* out ) noexcept
{
std::size_t i = 0;

Expand Down Expand Up @@ -102,7 +103,7 @@ OutputIterator to_chars( uuid const& u, OutputIterator out )
return std::copy_n( tmp, 36, out );
}

inline bool to_chars( uuid const& u, char* first, char* last )
inline bool to_chars( uuid const& u, char* first, char* last ) noexcept
{
if( last - first < 36 )
{
Expand All @@ -113,7 +114,7 @@ inline bool to_chars( uuid const& u, char* first, char* last )
return true;
}

inline bool to_chars( uuid const& u, wchar_t* first, wchar_t* last )
inline bool to_chars( uuid const& u, wchar_t* first, wchar_t* last ) noexcept
{
if( last - first < 36 )
{
Expand Down

0 comments on commit 835a658

Please sign in to comment.