Public Member Functions

mitk::UIDGenerator Class Reference

Generated unique IDs. More...

#include <mitkUIDGenerator.h>

List of all members.

Public Member Functions

 UIDGenerator (const char *prefix="UID_", unsigned int lengthOfRandomPart=8)
std::string GetUID ()

Detailed Description

Generated unique IDs.

Creates (somehow most of the time) unique IDs from a given prefix, the current date/time and a random part.

The prefix is given to the constructor, together with the desired length of the random part (minimum 5 digits).

You will get another quite unique ID each time you call GetUID.

Definition at line 37 of file mitkUIDGenerator.h.


Constructor & Destructor Documentation

mitk::UIDGenerator::UIDGenerator ( const char *  prefix = "UID_",
unsigned int  lengthOfRandomPart = 8 
)

Definition at line 27 of file mitkUIDGenerator.cpp.

References MITK_ERROR.

:m_Prefix(prefix),
 m_LengthOfRandomPart(lengthOfRandomPart)
{
  if (lengthOfRandomPart < 5)
  {
    MITK_ERROR << lengthOfRandomPart << " are not really unique, right?" << std::endl;
    throw std::invalid_argument("To few digits requested");
  }

  srand((unsigned int) time( (time_t *)0 ));
}

Member Function Documentation

std::string mitk::UIDGenerator::GetUID (  )

Definition at line 40 of file mitkUIDGenerator.cpp.

References int(), QuadProgPP::pow(), and QuadProgPP::t().

Referenced by mitk::SceneIO::SaveScene().

{
  std::ostringstream s;
  s << m_Prefix;
  time_t tt = time(0);
  tm* t = gmtime(&tt);

  if (t)
  {
    s << t->tm_year + 1900;
    
    if (t->tm_mon < 9) s << "0"; // add a 0 for months 1 to 9
    s << t->tm_mon + 1;

    if (t->tm_mday < 10) s << "0"; // add a 0 for days 1 to 9
    s << t->tm_mday;

    if (t->tm_hour < 10) s << "0"; // add a 0 for hours 1 to 9
    s << t->tm_hour;

    if (t->tm_min < 10) s << "0"; // add a 0 for minutes 1 to 9
    s << t->tm_min;
    
    if (t->tm_sec < 10) s << "0"; // add a 0 for seconds 1 to 9
    s << t->tm_sec;
    
    std::ostringstream rs;
    rs << (long int)( pow(10.0, double(m_LengthOfRandomPart)) / double(RAND_MAX) * double(rand()) );

    for (size_t i = rs.str().length(); i < m_LengthOfRandomPart; ++i)
    {
      s << "X";
    }

    s << rs.str();
  }

  return s.str();
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines