a8/a8/uuid.h
2018-10-16 11:57:00 +08:00

37 lines
973 B
C++

#ifndef A8_UUID_H
#define A8_UUID_H
namespace a8
{
namespace uuid
{
const long long A8_EPOCH = 1419120000000LL;
const int MACHINE_ID_BIT_NUM = 12;
const int SEQUNCE_ID_BIT_NUM = 10;
const int MAX_MACHINE_ID = (1 << MACHINE_ID_BIT_NUM) - 1; //oldvalue: 0x3FF
const int MAX_SEQUNCE_ID = (1 << SEQUNCE_ID_BIT_NUM) - 1; //oldvalue: 0xFFF
//毫秒级时间41位+机器ID 12位+毫秒内序列10
class SnowFlake
{
public:
static long long FetchTime(long long value);
static int FetchMachineId(long long value);
static int FetchSequnceId(long long value);
public:
long long Generate();
void SetMachineId(unsigned short machineid);
private:
unsigned short machine_id = 0; //12 bit
unsigned short sequence_id = 0; //10 bit
long long last_generate_tick = 0;
};
}
}
#endif