Click here to Skip to main content
Click here to Skip to main content

An unique value template

, 11 Sep 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

LOXmith

Bulgaria Bulgaria
No Biography provided

Comments and Discussions

 
GeneralMultithreading PinmemberLOXmith5-Jun-01 6:11 
GeneralDIY :) PinmemberLOXmith6-Jun-01 0:38 
GeneralRe: DIY :) Pinmemberansont6-Jun-01 22:17 
GeneralRe: DIY :) PinmemberLOXmith6-Jun-01 22:37 
GeneralRe: DIY :) PinmemberJames R. Twine7-Jun-01 10:19 
GeneralRe: DIY :) PinmemberLOXmith7-Jun-01 21:00 
GeneralRe: DIY :) PinmemberJames R. Twine8-Jun-01 3:57 
GeneralRe: DIY :) PinmemberLOXmith8-Jun-01 4:40 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 12 Sep 2001
Article Copyright 2001 by LOXmith
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid