c++11 boost - uuid、utility

2018-07-11 12:30:34

uuid 是一个很小的类,可以生成16字节的全球唯一标识符。

sha1 可以将任意长度的文本压缩成20字节的摘要。

下面是使用例子

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
using namespace boost::uuids;

void case1()
{
    uuid u ;
    assert(uuid::static_size() == 16);
    assert(u.size() == 16);

    vector<unsigned char> v(16, 7);
    std::copy(v.begin(), v.end(), u.begin());
    assert(u.data[0] == u.data[1]
            && u.data[15] == 7);

    cout << u << endl;
    std::fill_n(u.data+ 10,6, 8);

    cout << u << endl;
}


void case2()
{
    uuid u ;

    std::fill_n(u.begin(), u.size(), 0xab);
    assert(!u.is_nil());
    assert(u.variant() == u.variant_rfc_4122);  //4122变体类型
    assert(u.version() == u.version_unknown);  //生成算法未知
    cout << u << endl;

    std::memset(u.data, 0, 16);
    assert(u.is_nil());
    uuid u1 ,u2;

    std::fill_n(u1.begin(), u1.size(), 0xab);
    std::fill_n(u2.begin(), u2.size(), 0x10);
    assert(u1 != u2 && u1 > u2);

    u2.data[0] = 0xff;
    assert( u1 < u2);

    std::memset(u1.data, 0, 16);
    std::memset(u2.data, 0, 16);
    assert(u1 == u2);

}


void case3()
{
    uuid u = nil_generator()();
    assert(u.is_nil());

    u = nil_uuid();
    assert(u.is_nil());

    string_generator sgen;
	
	cout << endl;
    uuid u1 = sgen("0123456789abcdef0123456789abcdef");
    cout << u1 << endl;
    uuid u2 = sgen("01234567-89ab-cdef-0123-456789abcdef");
    cout << u2 << endl;
    uuid u3 = sgen(L"{01234567-89ab-cdef-0123-456789abcdef}");
    cout << u3 << endl;
    cout << endl;
}

//名字生成器
void case4()
{
	//先指定基准uuid
    uuid www_xxx_com= string_generator()
        ("{0123456789abcdef0123456789abcdef}");
    name_generator ngen(www_xxx_com);

    uuid u1 = ngen("mario");
    assert(!u1.is_nil() &&
            u1.version() == uuid::version_name_based_sha1);

    uuid u2 = ngen("link");
    cout << u2 << endl;

	//随机生成器,保证生成的不重复
    random_generator rgen;

    uuid u = rgen();
    assert(u.version()== uuid::version_random_number_based);
    cout << u << endl;
    cout << endl;

}

class uuid_t : public uuid
{
private:
    static random_generator& rand_uuid()
    {
        static random_generator gen;
        return gen;
    }
    static string_generator& string_uuid()
    {
        static string_generator gen;
        return gen;
    }

public:
    uuid_t(): uuid(rand_uuid()()){}
    uuid_t(int): uuid(nil_uuid()){}
    uuid_t(const char* str): uuid(string_uuid()(str)) {}
    uuid_t(const uuid& u, const char* str):
        uuid(name_generator(u)(str))    {}
    explicit uuid_t(const uuid& u): uuid(u){}

    operator uuid()
    {   return static_cast<uuid&>(*this);   }
    operator uuid() const
    {   return static_cast< const uuid&>(*this);}
};

void case5()
{
    uuid_t u0 = 0;
    assert(u0.is_nil());

    uuid_t u1,u2;
    cout << u1 << endl;
    cout << u2 << endl;

    uuid_t u3("{01234567-89ab-cdef-0123-456789abcdef}");
    cout << u3 << endl;

    cout << uuid_t(u3, "test name gen") << endl;

}

//////////////////////////////////////////
#include <boost/lexical_cast.hpp>
using namespace boost;

//转换
void case6()
{
	cout << endl;
    uuid u = string_generator()
        ("01234567-89ab-cdef-0123-456789abcdef");

    string str = to_string(u);
    cout << str << endl;

    {
        uuid u = lexical_cast<uuid>
            ("01234567-89ab-cdef-0123-456789abcdef");
        cout << u << endl;
        string str = lexical_cast<string>(u);
        cout << str << endl;

    }
    cout << endl;
}

//////////////////////////////////////////
#include <boost/version.hpp>
#if BOOST_VERSION <= 106400
#include <boost/uuid/sha1.hpp>
#else
#include <boost/uuid/detail/sha1.hpp>
#endif
using namespace boost::uuids::detail;

//摘要算法,可以将任意长度的字符串压缩成20字节。
void case7()
{
    sha1 sha;

    const char *szMsg = "a short message";
    //sha.process_byte(0x10);   //一个字节一个字节喂
    
    //下面两个等同
    sha.process_bytes(szMsg, strlen(szMsg));
    //sha.process_block(szMsg, szMsg + strlen(szMsg));

    unsigned int digest[5];
    sha.get_digest(digest);
    for (int i = 0; i < 5 ; ++i)
    {
        cout << hex << digest[i];
    }
}


//////////////////////////////////////////

int main()
{
    case1();
    case2();
    case3();
    case4();
    case5();
    case6();
    case7();
}
[root@192 c++]# g++ -std=c++11 main.cpp 
[root@192 c++]# ./a.out 
07070707-0707-0707-0707-070707070707
07070707-0707-0707-0707-080808080808
abababab-abab-abab-abab-abababababab

01234567-89ab-cdef-0123-456789abcdef
01234567-89ab-cdef-0123-456789abcdef
01234567-89ab-cdef-0123-456789abcdef

f1a16014-5e8c-5bf1-bc26-42a90d9d8941
25319c22-0a08-42b5-bb6e-1361f712b643

4e30a0f2-c1bf-4648-ab05-41b9657badf3
9ad1434a-37f5-4623-8de8-db298bcf572a
01234567-89ab-cdef-0123-456789abcdef
47afa5cb-3039-56c6-b5b8-6b044b785cbe

01234567-89ab-cdef-0123-456789abcdef
01234567-89ab-cdef-0123-456789abcdef
01234567-89ab-cdef-0123-456789abcdef

b5adf4f048f244c71ce10b2e4246f54e188008dd


utility 库包含了一些小工具。

// Copyright (c) 2015
// Author: Chrono Law
#include <bitset>
#include <iostream>
using namespace std;

#include <boost/utility.hpp>

//二进制常量
void case1()
{
	//前面加上0x
    cout << hex << showbase;
    cout <<  BOOST_BINARY(0110) << endl;
    cout <<  BOOST_BINARY(0110 1101) << endl;
    cout <<  BOOST_BINARY(10110110 01) << endl;
    cout <<  bitset<5>(BOOST_BINARY(0110)) << endl;

    cout <<  BOOST_BINARY_UL(101 1001) << endl;
    long long x = BOOST_BINARY_LL(1101);
    cout << x << endl;

}

//调用函数名
#include <boost/current_function.hpp>

double func()
{
      cout << BOOST_CURRENT_FUNCTION << endl;
        return 0.0;
}

string str = BOOST_CURRENT_FUNCTION;      //错误用法,不能用在函数作用域外

void case2()
{
    cout << str << endl;
    cout << __FUNCTION__ << endl;
    cout << BOOST_CURRENT_FUNCTION << endl;
    func();
}

int main()
{
    case1();
    case2();
}
[root@192 c++]# g++ -std=c++11 main.cpp 
[root@192 c++]# ./a.out 
0x6
0x6d
0x2d9
00110
0x59
0xd
top level
case2
void case2()
double func()


 备注

1.编译器版本gcc4.8.5,运行环境centos7 64位
2.本文只做简单记录用,详细用法请参考 Boost Library,或者是罗剑锋的 boost程序库完全开发指南 书本
3..原文地址http://www.freecls.com/a/2712/a0


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。