-
Notifications
You must be signed in to change notification settings - Fork 0
/
UUID.cpp
135 lines (123 loc) · 3.56 KB
/
UUID.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//*****************************************************************************
//
// Copyright (C) 2001 Steve Connet. All rights reserved.
//
// Source File Name : UUID.cpp
// Author : Steve Connet
//
// Version : $Id: UUID.cpp,v 1.3 2015/03/14 23:44:56 clu Exp $
//
// Revision History :
//
// $Log: UUID.cpp,v $
// Revision 1.3 2015/03/14 23:44:56 clu
// astyle code formatter applied
//
// Revision 1.2 2002/01/11 03:41:49 sconnet
// *** empty log message ***
//
// Revision 1.1 2001/11/08 06:17:14 sconnet
// Initial revision
//
//
//*****************************************************************************
//#pragma implementation
#include <sstream>
#include <netdb.h>
#include "UUID.h"
using namespace std;
//using namespace ConnetUtils;
// initialize class variable
struct in_addr UUID::ia = { 0 };
//
//-------------------------------------------------------------------------
// Method : operator<<(std::ostream& out, const UUID& uuid)
//
// Implementation : put a uuid into an output stream
//
//-------------------------------------------------------------------------
//
//ostream& ConnetUtils::operator<<(ostream& out, const UUID& uuid)
ostream &operator<<(ostream &out, const UUID &uuid)
{
out << uuid.ia.s_addr << uuid.tv.tv_sec << uuid.tv.tv_usec;
return out;
}
//
//-------------------------------------------------------------------------
// Method : bool operator<(const UUID& lhs, const UUID& rhs)
//
// Implementation : less then comparison
//
//-------------------------------------------------------------------------
//
//bool ConnetUtils::operator<(const UUID& lhs, const UUID& rhs)
bool operator<(const UUID &lhs, const UUID &rhs)
{
return
lhs.tv.tv_usec < rhs.tv.tv_usec &&
lhs.tv.tv_sec < rhs.tv.tv_sec &&
lhs.ia.s_addr < rhs.ia.s_addr;
}
//
//-------------------------------------------------------------------------
// Method : bool operator==(const UUID& lhs, const UUID& rhs)
//
// Implementation : equality comparison
//
//-------------------------------------------------------------------------
//
//bool ConnetUtils::operator==(const UUID& lhs, const UUID& rhs)
bool operator==(const UUID &lhs, const UUID &rhs)
{
return
lhs.tv.tv_usec == rhs.tv.tv_usec &&
lhs.tv.tv_sec == rhs.tv.tv_sec &&
lhs.ia.s_addr == rhs.ia.s_addr;
}
//
//-------------------------------------------------------------------------
// Method : bool operator!=(const UUID& lhs, const UUID& rhs)
//
// Implementation : inequality comparison
//
//-------------------------------------------------------------------------
//
//bool ConnetUtils::operator!=(const UUID& lhs, const UUID& rhs)
bool operator!=(const UUID &lhs, const UUID &rhs)
{
return !(lhs == rhs);
}
//
//-------------------------------------------------------------------------
// Method : constructor
//
// Implementation :
//
//-------------------------------------------------------------------------
//
UUID::UUID()
{
if(0 == ia.s_addr)
{
char host_name[255];
gethostname(host_name, 255);
struct hostent *host = gethostbyname(host_name);
memcpy(&ia, host->h_addr, host->h_length);
}
gettimeofday(&tv, NULL);
}
//
//-------------------------------------------------------------------------
// Method : UUID::operator std::string()
//
// Implementation : cast this uuid into a string
//
//-------------------------------------------------------------------------
//
UUID::operator std::string()
{
ostringstream oss;
oss << *this;
return oss.str();
}