Unid

Files CCore/inc/Unid.h CCore/src/Unid.cpp

Unid is a unique identity. It is represented by the structure Unid. It's a POD type with 8 32-bit values. To build a particular Unid the best way is to use a good random number generator like HotBits.


struct Unid
 {
  uint32 id[8];

  friend bool operator < (const Unid &a,const Unid &b);

  friend bool operator > (const Unid &a,const Unid &b);

  friend bool operator <= (const Unid &a,const Unid &b);

  friend bool operator >= (const Unid &a,const Unid &b);

  friend bool operator == (const Unid &a,const Unid &b);

  friend bool operator != (const Unid &a,const Unid &b);

  CmpResult objCmp(const Unid &obj) const;
 };

You may assign a Unid to the type like this:


class SomeClass
 {
  public:

   static const Unid TypeUnid;
 };

const Unid SomeClass::TypeUnid={{ .... }};

and then get it using the helper UnidOf (beware inheritance!):


template <class T>
struct UnidOf
 {
  static const Unid & Get() { return T::TypeUnid; }
 };

The special class UnidRegister can be used to build a global mapping from the Unid to number:


class UnidRegister : NoCopy
 {
   ....

  public:

   UnidRegister();

   ~UnidRegister();

   template <class T>
   ulen getTypeId() noexcept;
 };

The method getTypeId() returns a number, associated with the Unid of the type T (and therefor with the type T). Numbers are assigned sequentially from the 0. It calls abort on failure.