n-values UUID generator, reusable IDs
I need a simple UUID generator. The ID is required to be unique for this single instance. Another requirement is, that it has n hashes coexisting at a time, and being releasable. I don't know wether this fits the UUID concept or not. I allrdy thought about a Stack with n-values using pop and push, but this practice seems b开发者_运维百科ad memory wise.
Using random based UUIDs (excluding cryptographic ones) isn't save enough, as by bad luck there could be 2 matching IDs, which can not be accapet (though neglegable chance), as this is supposed to be used in an productive environment.
I highly recommend this library, it's a boost candidate - we're using it in one of our projects and it works just fine. The link is to v13. This version + other versions can be located at www.boostpro.com
Example :
#include <string>
#include <iostream>
#include <UUID/boost/uuid/uuid.hpp>
using namespace std;
/*! returns a filename which is a uuid + .dat*/
std::string generate_filename()
{
boost::uuids::uuid_generator gen;
boost::uuids::uuid u = gen();
return u.to_string()+ ".dat";
}
void main()
{
for(int i = 0; i < 10; i++)
cout << generate_filename() << endl;
}
If that's what you seek, please mark this post as an answer :)
Universally Unique Identifiers (UUID) / Globally Unique Identifier (GUID)
The problem of generating unique IDs can be broken down as uniqueness over space and uniqueness over time which, when combined, aim to produce a globally unique sequence.
UUIDs are officially and specifically defined as part of the ISO-11578 standard other specifications also exist, like RFC 4122, ITU-T Rec. X.667.
OSSP uuid ( http://www.ossp.org/pkg/lib/uuid/ ) is an API for ISO C, ISO C++, Perl and PHP and a corresponding CLI for the generation of DCE 1.1, ISO/IEC 11578:1996, and RFC4122 compliant Universally Unique Identifiers (UUIDs). It supports DCE 1.1 variant UUIDs of version 1 (time and node based), version 3 (name based, MD5), version 4 (random number based), and version 5 (name based, SHA-1). UUIDs are 128-bit numbers that are intended to have a high likelihood of uniqueness over space and time and are computationally difficult to guess. They are globally unique identifiers that can be locally generated without contacting a global registration authority. It is Open Sourced under the MIT/X Consortium License.
I have included some further explanations in the http://en.wikibooks.org/wiki/The_World_of_Peer-to-Peer_%28P2P%29/Building_a_P2P_System#Unique_ID
On windows check the RPC library (see #include "Rpcdce.h" ) it has functions to generate UUIDs.
精彩评论