unique_id.h

Go to the documentation of this file.
00001 #ifndef UNIQUE_ID_CLASS
00002 #define UNIQUE_ID_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : unique_id                                                         *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1999-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include <basis/definitions.h>
00019 
00021 
00027 template <class uniquifier>
00028 class unique_id
00029 {
00030 public:
00031   inline unique_id(uniquifier initial_value) : _id(initial_value) {}
00033   inline unique_id(const unique_id<uniquifier> &to_copy) { *this = to_copy; }
00035   inline ~unique_id() {}
00036 
00038 
00040   inline bool operator == (const unique_id<uniquifier> &to_compare) const
00041           { return _id == to_compare._id; }
00043   inline unique_id & operator = (const unique_id<uniquifier> &to_copy)
00044           { if (this != &to_copy) _id = to_copy._id; return *this; }
00045 
00047   inline bool operator != (const unique_id &to_compare) const 
00048           { return ! (*this == to_compare); }
00049 
00050   uniquifier raw_id() const { return _id; }
00052   void set_raw_id(uniquifier new_value) { _id = new_value; }
00054 
00055 private:
00056   uniquifier _id;  
00057 };
00058 
00060 
00062 
00068 template <class uniquifier>
00069 class orderable_unique_id : public unique_id<uniquifier>
00070 {
00071 public:
00072   inline orderable_unique_id(const uniquifier &initial_value)
00073           : unique_id<uniquifier>(initial_value) {}
00074   inline orderable_unique_id(const unique_id<uniquifier> &initial_value)
00075           : unique_id<uniquifier>(initial_value) {}
00076   inline ~orderable_unique_id() {}
00077 
00080   bool operator < (const unique_id<uniquifier> &to_compare) const
00081           { return this->raw_id() < to_compare.raw_id(); }
00082 
00083   // remaining comparisons are based on necessary function.
00084   inline bool operator <= (const unique_id<uniquifier> &to_compare) const 
00085           { return (*this == to_compare) || (*this < to_compare); }
00086   inline bool operator > (const unique_id<uniquifier> &to_compare) const 
00087           { return (*this != to_compare) && ! (*this < to_compare); }
00088   inline bool operator >= (const unique_id<uniquifier> &to_compare) const 
00089           { return (*this == to_compare) || ! (*this < to_compare); }
00090 };
00091 
00093 
00095 
00096 class unique_int : public unique_id<int>
00097 {
00098 public:
00099   unique_int(int initial = 0) : unique_id<int>(initial) {}
00101 
00102   inline bool operator ! () const { return raw_id() == 0; }
00104 
00106 };
00107 
00108 #endif
00109 

Generated on Fri Nov 28 04:29:13 2008 for HOOPLE Libraries by  doxygen 1.5.1