zoukankan      html  css  js  c++  java
  • UUID的封装类

    UUID,即通用唯一识别码,产生的随机数据重复概率几乎为零,平时写程序的时候还是会用到,所以封装了一下,使用起来更简单了。
    头文件:

     
    1. namespace God {  
    2.   
    3. struct Uuid {  
    4. public:  
    5.     DEFINE_PTR(Uuid);  
    6.     typedef uint16_t size_type;  
    7.   
    8.     enum {  
    9.         UUID_LENGTH = sizeof(uuid_t),  
    10.     };  
    11.   
    12. public:  
    13.     Uuid(bool empty = false) {  
    14.         reset(empty);  
    15.     }  
    16.   
    17.     Uuid(const byte_t *data, size_type len) {  
    18.         reset(data, len);  
    19.     }  
    20.   
    21.     Uuid(const std::string &str) {  
    22.         reset(str.c_str());  
    23.     }  
    24.   
    25.     Uuid(const char *str) {  
    26.         reset(str);  
    27.     }  
    28.   
    29.     void reset(bool empty = false);  
    30.   
    31.     void reset(const byte_t *data, size_type len);  
    32.   
    33.     void reset(const std::string &str) {  
    34.         reset(str.c_str());  
    35.     }  
    36.   
    37.     void reset(const char *str);  
    38.   
    39.     bool empty() const;  
    40.   
    41.     size_type toByteArray(byte_t *data, size_type len) const;  
    42.   
    43.     std::string toString() const;  
    44.   
    45.     bool operator < (const Uuid &rhs) const;  
    46.   
    47.     bool operator == (const Uuid &rhs) const;  
    48.   
    49.     bool operator != (const Uuid &rhs) const;  
    50.   
    51. public:  
    52.     const static Uuid Empty;  
    53.   
    54. private:  
    55.     uuid_t m_data;  
    56. };  
    57.   
    58. }  

    实现文件:

     
    1. namespace God {  
    2.   
    3. const Uuid Uuid::Empty(true);  
    4.   
    5. void Uuid::reset(bool empty) {  
    6.     if (empty) {  
    7.         uuid_clear(m_data);  
    8.     } else {  
    9.         uuid_generate(m_data);  
    10.     }  
    11. }  
    12.   
    13. void Uuid::reset(const byte_t *data, size_type len) {  
    14.     if (len != (size_type)UUID_LENGTH) {  
    15.         GOD_THROW_EXCEPTION(InvalidArgumentException("len"));  
    16.     }  
    17.     memcpy(m_data, data, (size_type)UUID_LENGTH);  
    18. }  
    19.   
    20. void Uuid::reset(const char *str) {  
    21.     if (uuid_parse(str, m_data) == -1) {  
    22.         GOD_THROW_EXCEPTION_FROM_LAST_ERROR_WITH_API("uuid_parse");  
    23.     }  
    24. }  
    25.   
    26. bool Uuid::empty() const {  
    27.     return uuid_is_null(m_data) != 0;  
    28. }  
    29.   
    30. Uuid::size_type Uuid::toByteArray(byte_t *data, size_type len) const {  
    31.     size_type copyLen = std::min(len, (size_type)UUID_LENGTH);  
    32.     memcpy(data, m_data, copyLen);  
    33.     return copyLen;  
    34. }  
    35.   
    36. std::string Uuid::toString() const {  
    37.     return hexstringFromData(m_data, (size_type)UUID_LENGTH);  
    38. }  
    39.   
    40. bool Uuid::operator < (const Uuid &rhs) const {  
    41.     return memcmp(m_data, rhs.m_data, (size_type)UUID_LENGTH) < 0;  
    42. }  
    43.   
    44. bool Uuid::operator == (const Uuid &rhs) const {  
    45.     return memcmp(m_data, rhs.m_data, (size_type)UUID_LENGTH) == 0;  
    46. }  
    47.   
    48. bool Uuid::operator != (const Uuid &rhs) const {  
    49.     return memcmp(m_data, rhs.m_data, (size_type)UUID_LENGTH) != 0;  
    50. }  
    51.   
    52. }  

    想偷懒的直接拿去用吧,呵呵..

    详情请访问libgod官网..

  • 相关阅读:
    如何开启无线网卡
    E-SATA接口
    sata express接口
    联想服务器驱动
    国家信息安全漏洞共享平台
    SQL SERVER 性能调优
    计算机网络知识库
    CCNP 视频
    ORACLE 培训 -相克军
    phxsql
  • 原文地址:https://www.cnblogs.com/libgod/p/3445008.html
Copyright © 2011-2022 走看看