0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00

UUID class

This commit is contained in:
Eliot Horowitz 2009-01-30 15:58:44 -05:00
parent a53d5c7c1c
commit e9fc9ce950
4 changed files with 98 additions and 2 deletions

View File

@ -83,7 +83,7 @@ env.Append( CPPPATH=[ "." ] )
boostLibs = [ "thread" , "filesystem" , "program_options" ]
commonFiles = Split( "stdafx.cpp db/jsobj.cpp db/json.cpp db/commands.cpp db/lasterror.cpp db/nonce.cpp" )
commonFiles += [ "util/background.cpp" , "util/miniwebserver.cpp" , "util/mmap.cpp" , "util/sock.cpp" , "util/util.cpp" ]
commonFiles += [ "util/background.cpp" , "util/miniwebserver.cpp" , "util/mmap.cpp" , "util/sock.cpp" , "util/util.cpp" , "util/UUID.cpp" ]
commonFiles += Glob( "util/*.c" ) + Glob( "grid/*.cpp" )
commonFiles += Split( "client/connpool.cpp client/dbclient.cpp client/model.cpp" )

View File

@ -19,6 +19,7 @@
#include "../db/jsobj.h"
#include "../db/json.h"
#include "../util/UUID.h"
#include "dbtests.h"
@ -429,7 +430,7 @@ namespace JsobjTests {
class initParse1 {
public:
void run(){
OID a;
OID b;
@ -440,6 +441,25 @@ namespace JsobjTests {
ASSERT( a == b );
}
};
}
namespace UUIDTests {
class basic {
public:
void run(){
UUID a;
UUID b;
assert( a != b );
assert( ! ( a == b ) );
b = a;
assert( a == b );
UUID c( a.string() );
assert( a == c );
}
};
}
class All : public UnitTest::Suite {
@ -487,6 +507,7 @@ namespace JsobjTests {
add< BSONObjTests::Validation::Fuzz >( .001 );
add< OIDTests::init1 >();
add< OIDTests::initParse1 >();
add< UUIDTests::basic >();
}
};

47
util/UUID.cpp Normal file
View File

@ -0,0 +1,47 @@
// UUID.cpp
#include "UUID.h"
#include <iostream>
using namespace std;
namespace mongo {
UUID::UUID(){
uuid_generate( _data );
}
UUID::UUID( std::string s ){
uuid_parse( s.c_str() , _data );
}
UUID::~UUID(){
uuid_clear( _data );
}
string UUID::string() const{
char buf[33];
uuid_unparse( _data , buf );
std::string s;
s += buf;
return s;
}
bool UUID::operator==( const UUID& other) const{
return uuid_compare( _data , other._data ) == 0;
}
bool UUID::operator!=( const UUID& other) const{
return uuid_compare( _data , other._data );
}
ostream& operator<<( ostream &out , const UUID &id ){
out << id.string();
return out;
}
}

28
util/UUID.h Normal file
View File

@ -0,0 +1,28 @@
// UUID.h
#pragma once
#include <uuid/uuid.h>
#include <string>
namespace mongo {
class UUID {
public:
UUID();
UUID( std::string s );
~UUID();
std::string string() const;
bool operator==( const UUID& other) const;
bool operator!=( const UUID& other) const;
private:
unsigned char _data[16];
};
std::ostream& operator<<( std::ostream &s, const UUID &id );
}