Changeset 63941


Ignore:
Timestamp:
Jul 13, 2010, 12:37:17 AM (8 years ago)
Author:
Andy Tompkins
Message:

One can define BOOST_UUID_NO_TYPE_TRAITS to remove the dependency on Boost.TypeTraits?.
Stream operator<< handles left, internal, and right manipulators.
Added to_string, to_wstring
Fixed bug in 3 uuid string in documentation

Location:
branches/release
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/release/boost/uuid/uuid.hpp

    r58262 r63941  
    3838#include <algorithm>
    3939#include <boost/config.hpp> // for static assert
    40 #include <boost/mpl/bool.hpp>
     40#ifndef BOOST_UUID_NO_TYPE_TRAITS
    4141#include <boost/type_traits/is_pod.hpp>
     42#include <boost/type_traits/integral_constant.hpp>
     43#endif
    4244
    4345#if defined(_MSC_VER)
     
    203205}} //namespace boost::uuids
    204206
     207#ifndef BOOST_UUID_NO_TYPE_TRAITS
    205208// type traits specializations
    206209namespace boost {
    207210
    208211template <>
    209 struct is_pod<uuids::uuid> : mpl::true_
    210 {};
     212struct is_pod<uuids::uuid> : true_type {};
    211213
    212214} // namespace boost
     215#endif
    213216
    214217#if defined(_MSC_VER)
  • branches/release/boost/uuid/uuid_io.hpp

    r58017 r63941  
    3333{
    3434    io::ios_flags_saver flags_saver(os);
    35     io::ios_width_saver width_saver(os);
    3635    io::basic_ios_fill_saver<ch, char_traits> fill_saver(os);
    3736
    3837    const typename std::basic_ostream<ch, char_traits>::sentry ok(os);
    3938    if (ok) {
     39        const std::streamsize width = os.width(0);
     40        const std::streamsize uuid_width = 36;
     41        const std::ios_base::fmtflags flags = os.flags();
     42        const typename std::basic_ios<ch, char_traits>::char_type fill = os.fill();
     43        if (flags & (std::ios_base::right | std::ios_base::internal)) {
     44            for (std::streamsize i=uuid_width; i<width; i++) {
     45                os << fill;
     46            }
     47        }
     48
    4049        os << std::hex;
    4150        os.fill(os.widen('0'));
     
    4958            }
    5059        }
     60       
     61        if (flags & std::ios_base::left) {
     62            for (std::streamsize i=uuid_width; i<width; i++) {
     63                os << fill;
     64            }
     65        }
     66       
     67        os.width(0); //used the width so reset it
    5168    }
    5269    return os;
     
    111128}
    112129
     130namespace detail {
     131inline char to_char(size_t i) {
     132    if (i <= 9) {
     133        return static_cast<char>('0' + i);
     134    } else {
     135        return static_cast<char>('a' + (i-10));
     136    }
     137}
     138
     139inline wchar_t to_wchar(size_t i) {
     140    if (i <= 9) {
     141        return static_cast<wchar_t>(L'0' + i);
     142    } else {
     143        return static_cast<wchar_t>(L'a' + (i-10));
     144    }
     145}
     146
     147} // namespace detail
     148
     149inline std::string to_string(uuid const& u)
     150{
     151    std::string result;
     152    result.reserve(36);
     153
     154    std::size_t i=0;
     155    for (uuid::const_iterator it_data = u.begin(); it_data!=u.end(); ++it_data, ++i) {
     156        const size_t hi = ((*it_data) >> 4) & 0x0F;
     157        result += detail::to_char(hi);
     158
     159        const size_t lo = (*it_data) & 0x0F;
     160        result += detail::to_char(lo);
     161
     162        if (i == 3 || i == 5 || i == 7 || i == 9) {
     163            result += '-';
     164        }
     165    }
     166    return result;
     167}
     168
     169#ifndef BOOST_NO_STD_WSTRING
     170inline std::wstring to_wstring(uuid const& u)
     171{
     172    std::wstring result;
     173    result.reserve(36);
     174
     175    std::size_t i=0;
     176    for (uuid::const_iterator it_data = u.begin(); it_data!=u.end(); ++it_data, ++i) {
     177        const size_t hi = ((*it_data) >> 4) & 0x0F;
     178        result += detail::to_wchar(hi);
     179
     180        const size_t lo = (*it_data) & 0x0F;
     181        result += detail::to_wchar(lo);
     182
     183        if (i == 3 || i == 5 || i == 7 || i == 9) {
     184            result += L'-';
     185        }
     186    }
     187    return result;
     188}
     189
     190#endif
     191
    113192}} //namespace boost::uuids
    114193
  • branches/release/libs/uuid/test/Jamfile.v2

    r60927 r63941  
    99    # make sure each header file is self-contained
    1010    [ compile compile_uuid.cpp ]
     11    [ compile compile_uuid.cpp : <define>BOOST_UUID_NO_TYPE_TRAITS : compile_uuid_no_type_traits ]
    1112    [ compile compile_uuid_io.cpp ]
    1213    [ compile compile_uuid_serialize.cpp ]
     
    4142
    4243    # test serializing uuids
    43     [ run test_serialization.cpp ../../serialization/build//boost_serialization
    44     ]
     44    [ run test_serialization.cpp ../../serialization/build//boost_serialization ]
    4545    # TODO - This test fails to like with boost_wserialization
    4646    #[ run test_wserialization.cpp
  • branches/release/libs/uuid/test/compile_uuid.cpp

    r60927 r63941  
    88// http://www.boost.org/LICENSE_1_0.txt)
    99
    10 // Purpose to make sure that a translation unit consisting of just the contents 
     10// Purpose to make sure that a translation unit consisting of just the contents
    1111// of the header file will compile successfully.
    1212
  • branches/release/libs/uuid/test/test_io.cpp

    r60927 r63941  
    1616#include <boost/lexical_cast.hpp>
    1717#include <string>
     18#include <sstream>
     19#include <iomanip>
     20
     21#ifndef BOOST_NO_STD_WSTRING
     22namespace std {
     23template <typename Elem, typename Traits>
     24std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& os, std::wstring const& s) {
     25    // convert to string
     26    std::string temp(s.begin(), s.end());
     27    os << temp;
     28    return os;
     29}
     30} // namespace std
     31#endif
    1832
    1933int main(int, char*[])
     
    3751        ss3 << u3;
    3852        BOOST_TEST_EQ(ss3.str(), "12345678-90ab-cdef-1234-567890abcdef");
     53       
     54        std::stringstream ss4;
     55        ss4 << std::uppercase << u3;
     56        BOOST_TEST_EQ(ss4.str(), "12345678-90AB-CDEF-1234-567890ABCDEF");
     57       
     58        std::stringstream ss5;
     59        ss5 << 'a' << std::right << std::setfill('*') << std::setw(40) << u1 << 'a';
     60        BOOST_TEST_EQ(ss5.str(), "a****00000000-0000-0000-0000-000000000000a");
     61       
     62        std::stringstream ss6;
     63        ss6 << std::left << std::setfill('*') << std::setw(45) << u1;
     64        BOOST_TEST_EQ(ss6.str(), "00000000-0000-0000-0000-000000000000*********");
    3965    }
     66
     67 #ifndef BOOST_NO_STD_WSTRING
     68    { // test insert/extract operators
     69        std::wstringstream ss1;
     70        ss1 << u1;
     71        BOOST_TEST_EQ(ss1.str(), L"00000000-0000-0000-0000-000000000000");
     72
     73        std::wstringstream ss2;
     74        ss2 << u2;
     75        BOOST_TEST_EQ(ss2.str(), L"00010203-0405-0607-0809-0a0b0c0d0e0f");
     76
     77        std::wstringstream ss3;
     78        ss3 << u3;
     79        BOOST_TEST_EQ(ss3.str(), L"12345678-90ab-cdef-1234-567890abcdef");
     80       
     81        std::wstringstream ss4;
     82        ss4 << std::uppercase << u3;
     83        BOOST_TEST_EQ(ss4.str(), L"12345678-90AB-CDEF-1234-567890ABCDEF");
     84       
     85        std::wstringstream ss5;
     86        ss5 << L'a' << std::right << std::setfill(L'*') << std::setw(40) << u1 << L'a';
     87        BOOST_TEST_EQ(ss5.str(), L"a****00000000-0000-0000-0000-000000000000a");
     88       
     89        std::wstringstream ss6;
     90        ss6 << std::left << std::setfill(L'*') << std::setw(45) << u1;
     91        BOOST_TEST_EQ(ss6.str(), L"00000000-0000-0000-0000-000000000000*********");
     92    }
     93#endif
    4094
    4195    {
     
    52106    }
    53107
     108 #ifndef BOOST_NO_STD_WSTRING
     109   {
     110        uuid u;
     111
     112        std::wstringstream ss;
     113        ss << L"00000000-0000-0000-0000-000000000000";
     114        ss >> u;
     115        BOOST_TEST_EQ(u, u1);
     116
     117        ss << L"12345678-90ab-cdef-1234-567890abcdef";
     118        ss >> u;
     119        BOOST_TEST_EQ(u, u3);
     120    }
     121#endif
     122
    54123    { // test with lexical_cast
    55124        BOOST_TEST_EQ(boost::lexical_cast<std::string>(u1), std::string("00000000-0000-0000-0000-000000000000"));
     
    60129    }
    61130
    62    return boost::report_errors();
     131#ifndef BOOST_NO_STD_WSTRING
     132    { // test with lexical_cast
     133        BOOST_TEST_EQ(boost::lexical_cast<std::wstring>(u1), std::wstring(L"00000000-0000-0000-0000-000000000000"));
     134        BOOST_TEST_EQ(boost::lexical_cast<uuid>(L"00000000-0000-0000-0000-000000000000"), u1);
     135
     136        BOOST_TEST_EQ(boost::lexical_cast<std::wstring>(u3), std::wstring(L"12345678-90ab-cdef-1234-567890abcdef"));
     137        BOOST_TEST_EQ(boost::lexical_cast<uuid>(L"12345678-90ab-cdef-1234-567890abcdef"), u3);
     138    }
     139#endif
     140   
     141    { // test to_string
     142        BOOST_TEST_EQ(to_string(u1), std::string("00000000-0000-0000-0000-000000000000"));
     143        BOOST_TEST_EQ(to_string(u3), std::string("12345678-90ab-cdef-1234-567890abcdef"));
     144    }
     145   
     146#ifndef BOOST_NO_STD_WSTRING
     147    { // test to_wstring
     148        BOOST_TEST_EQ(to_wstring(u1), std::wstring(L"00000000-0000-0000-0000-000000000000"));
     149        BOOST_TEST_EQ(to_wstring(u3), std::wstring(L"12345678-90ab-cdef-1234-567890abcdef"));
     150    }
     151#endif
     152
     153    return boost::report_errors();
    63154}
  • branches/release/libs/uuid/uuid.html

    r61180 r63941  
    6161        <ul>
    6262            <li><a href="#Synopsis_io">Synopsis</a></li>
    63             <li><a href="#Input and Output">Input and Output</a></li>
     63            <li><a href="#Stream_operators">Stream Operators</a></li>
     64            <li><a href="#to_string">To String</a></li>
    6465        </ul>
    6566        <li><a href="#boost/uuid/uuid_serialize.hpp">boost/uuid/uuid_serialize.hpp</a></li>
     
    8586may be identified with a UUID to ensure that different parts of a message are put
    8687back together again.  Distributed computing may use UUIDs to identify a remote
    87 procedure call.  Transactions and classes involved in serialization may be 
     88procedure call.  Transactions and classes involved in serialization may be
    8889identified by UUIDs.  Microsoft's component object model (COM) uses UUIDs to
    8990distinguish different software component interfaces.  UUIDs are inserted into
     
    168169that does initialize itself to a value generated by one of
    169170the defined mechanisms.
     171<p>
     172Note that <tt>boost::is_pod</tt> is specialized for <tt>boost::uuids::uuid</tt>
     173and depends on <a href="http://www.boost.org/libs/type_traits">Boost.TypeTraits</a>.
     174Define <tt>BOOST_UUID_NO_TYPE_TRAITS</tt> before including <a href="./../../boost/uuid/uuid.hpp"><tt>boost/uuid/uuid.hpp</tt></a>
     175to remove the dependency on <a href="http://www.boost.org/libs/type_traits">Boost.TypeTraits</a>.
    170176<pre>
    171177// example using memcpy and aggregate initializers
     
    442448<pre>
    443449boost::uuids::string_generator gen;
    444 boost::uuids::uuid u1 = gen("{01234567-89ab-cdef-0123456789abcdef}");
    445 boost::uuids::uuid u2 = gen(L"01234567-89ab-cdef-0123456789abcdef");
     450boost::uuids::uuid u1 = gen("{01234567-89ab-cdef-0123-456789abcdef}");
     451boost::uuids::uuid u2 = gen(L"01234567-89ab-cdef-0123-456789abcdef");
    446452boost::uuids::uuid u3 = gen(std::string("0123456789abcdef0123456789abcdef"));
    447 boost::uuids::uuid u4 = gen(std::wstring(L"01234567-89ab-cdef-0123456789abcdef"));
     453boost::uuids::uuid u4 = gen(std::wstring(L"01234567-89ab-cdef-0123-456789abcdef"));
    448454</pre>
    449455
     
    539545template &lt;typename ch, typename char_traits&gt;
    540546    std::basic_istream&lt;ch, char_traits&gt;&amp; operator&gt;&gt;(std::basic_istream&lt;ch, char_traits&gt; &amp;is, uuid &amp;u);
     547   
     548std::string to_string(uuid const&amp; u);
     549std::wstring to_wstring(uuid const&amp; u);
    541550
    542551}} // namespace boost::uuids
    543552</pre>
    544553
    545 <h4><a name="Input and Output">Input and Output</a></h4>
    546 <p>
    547 Input and output stream operators <tt>&lt;&lt;</tt> and <tt>&gt;&gt;</tt>
    548 are provided by including <a href="../../boost/uuid/uuid_io.hpp"><tt>boost/uuid/uuid_io.hpp</tt></a>. 
    549 The external representation of a <b>uuid</b> is a string of
    550 hexidecimal digits of the following form: <tt>hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh</tt>
    551 <pre>
    552 boost::uuids::uuid u;
     554<h4><a name="Stream_operators">Stream Operators</a></h4>
     555<p>
     556The standard input and output stream operators <tt>&lt;&lt;</tt> and <tt>&gt;&gt;</tt>
     557are provided by including <a href="../../boost/uuid/uuid_io.hpp"><tt>boost/uuid/uuid_io.hpp</tt></a>.
     558The string representation of a <b>uuid</b> is <tt>hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh</tt>
     559where <tt>h</tt> is a hexidecimal digit.
     560<pre>
     561boost::uuids::uuid u1; // initialize uuid
    553562
    554563std::stringstream ss;
    555 ss &lt;&lt; u;
    556 ss &gt;&gt; u;
     564ss &lt;&lt; u1;
     565
     566boost::uuids::uuid u2;
     567ss &gt;&gt; u2;
     568
     569assert(u1, u2);
     570</pre>
     571
     572<p>
     573One can also use <a href="http://www.boost.org/libs/conversion/lexical_cast.htm"><tt>boost::lexical_cast</tt></a>.
     574<pre>
     575boost::uuids::uuid u1; // initialize uuid
     576
     577std::string s = boost::lexical_cast&lt;std::string&gt;(u);
     578boost::uuids::uuid u2 = boost::lexical_cast&lt;boost::uuids::uuid&gt;(s);
     579
     580assert(u1 == u2);
     581</pre>
     582
     583<h4><a name="to_string">To String</a></h4>
     584<p>
     585The functions <tt>to_string</tt> and <tt>to_wstring</tt> are provided as a
     586convenience to convert a <b>uuid</b> to a string.  They are also likely faster than
     587the stream operators or using <a href="http://www.boost.org/libs/conversion/lexical_cast.htm"><tt>boost::lexical_cast</tt></a>.
     588<pre>
     589boost::uuids::uuid u; // initialize uuid
     590
     591std::string s1 = to_string(u);
     592
     593std::wstring s2 = to_wstring(u);
    557594</pre>
    558595
Note: See TracChangeset for help on using the changeset viewer.