Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C++
Article

An unique value template

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
11 Sep 2001 68.5K   21   8
Maintain unique ID for every object with no memory overhead

Introduction

The purpose of this class is :-

  • Defines template for automatic sequential ID numbers.
  • Useful for implementing IDs.
  • Supports multiple unique ID categories.
  • The values are guaranteed to be sequential in each category.

Interface

template <typename TValue, typename TCategory>
class unique_value
{
public:
    typedef TValue value_type;
    typedef TCategory category_type;

    // default constructor
    unique_value(); 

    // cast to value_type operator
    operator const value_type&() const;

    // read acessor for the unique counter
    static const value_type& get_counter();

protected:
    value_type m_value; //the unique value
}

Usage

  • Use as a numeric data type in the class you want to have unique IDs.
  • The category is determined of both TValue and TCategory.
  • It's recommended to create a typedef and to use it for all your IDs unless you have a reason not to do so.
  • Use INIT_UNIQUE_CAT and INIT_UNIQUE macros to initialize categories
INIT_UNIQUE(long);
typedef unique_value<long> unique_long;

class some_data_class
{
    unique_long ID; // unique for every object
    int data1;

    //...

    int dataN;
}


class different_category_data_class;
INIT_UNIQUE_CAT(long, different_category_data_class);

class different_category_data_class
{
    unique_value<long, different_category_data_class> ID;

    //...
}


foo() 
{ 
    some_data_class data_object; 
    different_category_data_class another_object;
    cout << data_object.ID << another_object.ID; 
}

Code listing

#if !defined(__UNIQUE_VALUE__)
#define __UNIQUE_VALUE__

typedef int default_unique_category;

template <typename TValue, typename TCategory=default_unique_category>
class unique_value
{
public:
    typedef TValue      value_type;
    typedef TCategory   category_type;

    // default constructor
    unique_value():m_value(m_counter){m_counter++;};        

    // cast to value_type operator
    operator const value_type & () const {return m_value;}; 

    // read acessor for the unique counter
    static const value_type& get_counter() {return m_counter;}

protected:
    value_type m_value;             //the unique value

private:
    unique_value   (value_type&);   // FORBIDEN assignment constructor
    void operator= (value_type&);   // FORBIDEN assignment operator

    // static variable used for unique value generation
    static value_type m_counter;    
};


// static members initialization macros
// call before using the template, once per template instance
#define INIT_UNIQUE(t_value) \
    INIT_UNIQUE_CAT(t_value, default_unique_category)

#define INIT_UNIQUE_CAT(t_value, t_category) \
    unique_value<t_value, t_category>::value_type \ 
    unique_value<t_value,t_category>::m_counter=0

#endif

History

  • Created - 6 May 2001
  • Added multithreading support - 6 June 2001
  • Removed multithreading support - 12 Sept 2001

Conclusion

I'd be glad if somebody uses this class. If you do, please mail me at LOXmith@abv.bg. I had to remove multithreading support. The reason is that you can't achieve platform dependent behaviour using platform independent ways. If you still need multithreading - see the discussion below.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMultithreading Pin
LOXmith5-Jun-01 6:11
LOXmith5-Jun-01 6:11 
GeneralDIY :) Pin
LOXmith6-Jun-01 0:38
LOXmith6-Jun-01 0:38 
GeneralRe: DIY :) Pin
6-Jun-01 22:17
suss6-Jun-01 22:17 
GeneralRe: DIY :) Pin
LOXmith6-Jun-01 22:37
LOXmith6-Jun-01 22:37 
GeneralRe: DIY :) Pin
James R. Twine7-Jun-01 10:19
James R. Twine7-Jun-01 10:19 
GeneralRe: DIY :) Pin
LOXmith7-Jun-01 21:00
LOXmith7-Jun-01 21:00 
GeneralRe: DIY :) Pin
James R. Twine8-Jun-01 3:57
James R. Twine8-Jun-01 3:57 
GeneralRe: DIY :) Pin
LOXmith8-Jun-01 4:40
LOXmith8-Jun-01 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.