13 Star 69 Fork 22

johnsonyl/cpps

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
uuid.cpp 2.01 KB
一键复制 编辑 原始数据 按行查看 历史
johnsonyl 提交于 2021-03-06 23:52 +08:00 . 2021-03-06 更新

#include <cpps/cpps.h>
#include <iostream>
#include <string>
#include <fstream>
#ifndef _WIN32
#include <uuid/uuid.h>
#else
#include <objbase.h>
#endif
#define GUID_LEN 128
using namespace cpps;
using namespace std;
class cpps_uuid
{
public:
unsigned int Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
void constructor(object _p) {
if (_p.isstring()) {
sscanf(_p.tostring().c_str(),
"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
(unsigned int*)&Data1, (unsigned int*)&Data2, (unsigned int*)&Data3,
(unsigned int*)&Data4[0], (unsigned int*)&Data4[1], (unsigned int*)&Data4[2],
(unsigned int*)&Data4[3], (unsigned int*)&Data4[4], (unsigned int*)&Data4[5],
(unsigned int*)&Data4[6], (unsigned int*)&Data4[7]);
}
else {
init();
}
}
void init() {
#ifdef _WIN32
GUID guid;
if (CoCreateGuid(&guid) != NULL)
{
return;
}
memcpy(this, &guid, sizeof(guid));
#else
uuid_t uuid;
uuid_generate(uuid);
memcpy(this, uuid, sizeof(cpps_uuid));
#endif
}
std::string tostring() {
char buffer[GUID_LEN] = { 0 };
sprintf(buffer,
"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
Data1, Data2, Data3,
Data4[0], Data4[1], Data4[2],
Data4[3], Data4[4], Data4[5],
Data4[6], Data4[7]);
return buffer;
}
};
cpps_value cpps_uuid_create(C*c) {
cpps_uuid* ret_uuid;
cpps_value ret;
newclass<cpps_uuid>(c, &ret_uuid,&ret);
ret_uuid->init();
return ret;
}
std::string cpps_uuid_uuid4() {
cpps_uuid uuid;
uuid.init();
return uuid.tostring();
}
cpps_export_void cpps_attach(cpps::C* c)
{
cpps::cpps_init_cpps_class(c);
cpps::_module(c, "uuid")[
_class<cpps_uuid>("UUID")
.def("constructor", &cpps_uuid::constructor)
.def("tostring", &cpps_uuid::tostring),
def_inside("create", cpps_uuid_create),
def("uuid4", cpps_uuid_uuid4)
];
}
cpps_export_void cpps_detach(cpps::C * c)
{
cpps::_unmodule(c, "uuid");
}
cpps_export_finish
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cppscript/cpps.git
git@gitee.com:cppscript/cpps.git
cppscript
cpps
cpps
master

搜索帮助