feisty meow concerns codebase 2.140
unique_id.h
Go to the documentation of this file.
1#ifndef UNIQUE_ID_CLASS
2#define UNIQUE_ID_CLASS
3
4/*****************************************************************************\
5* *
6* Name : unique_id *
7* Author : Chris Koeritz *
8* *
9*******************************************************************************
10* Copyright (c) 1999-$now By Author. This program is free software; you can *
11* redistribute it and/or modify it under the terms of the GNU General Public *
12* License as published by the Free Software Foundation; either version 2 of *
13* the License or (at your option) any later version. This is online at: *
14* http://www.fsf.org/copyleft/gpl.html *
15* Please send any updates to: fred@gruntose.com *
16\*****************************************************************************/
17
18#include <basis/contracts.h>
19
20namespace structures {
21
23
29template <class uniquifier>
30class unique_id : public virtual basis::equalizable
31{
32public:
33 unique_id(uniquifier initial_value) : _id(initial_value) {}
35
36 unique_id(const unique_id<uniquifier> &to_copy) { *this = to_copy; }
38
40
41 virtual bool equal_to(const equalizable &to_compare) const {
42 const unique_id<uniquifier> *cast = dynamic_cast<const unique_id<uniquifier> *>(&to_compare);
43 if (!cast) throw "error: unique_id::==: unknown type";
44 return cast->_id == _id;
45 }
46
48
50 bool operator == (const unique_id<uniquifier> &to_compare) const
51 { return _id == to_compare._id; }
52
55 { if (this != &to_copy) _id = to_copy._id; return *this; }
56
57 uniquifier raw_id() const { return _id; }
59
60 void set_raw_id(uniquifier new_value) { _id = new_value; }
62
63private:
64 uniquifier _id;
65};
66
68
70
76template <class uniquifier>
77class orderable_unique_id : public unique_id<uniquifier>
78{
79public:
80 orderable_unique_id(const uniquifier &initial_value)
81 : unique_id<uniquifier>(initial_value) {}
83 : unique_id<uniquifier>(initial_value) {}
85
88 bool operator < (const unique_id<uniquifier> &to_compare) const
89 { return this->raw_id() < to_compare.raw_id(); }
90};
91
93
95
96class unique_int : public unique_id<int>
97{
98public:
99 unique_int(int initial = 0) : unique_id<int>(initial) {}
101
102 bool operator ! () const { return raw_id() == 0; }
104
106};
107
108} //namespace.
109
110#endif
111
Base class for object that can tell itself apart from other instances.
Definition contracts.h:44
A unique identifier class that supports sorting.
Definition unique_id.h:78
bool operator<(const unique_id< uniquifier > &to_compare) const
Definition unique_id.h:88
orderable_unique_id(const uniquifier &initial_value)
Definition unique_id.h:80
orderable_unique_id(const unique_id< uniquifier > &initial_value)
Definition unique_id.h:82
Provides an abstraction for the responsibilities of a unique identifier.
Definition unique_id.h:31
uniquifier raw_id() const
Returns the held identifier in its native form.
Definition unique_id.h:57
unique_id(const unique_id< uniquifier > &to_copy)
Constructs a unique id as a copy of the "to_copy" object.
Definition unique_id.h:36
unique_id & operator=(const unique_id< uniquifier > &to_copy)
Sets this id to be the same as "to_copy".
Definition unique_id.h:54
virtual bool equal_to(const equalizable &to_compare) const
Definition unique_id.h:41
unique_id(uniquifier initial_value)
Constructs a unique id from the "initial_value".
Definition unique_id.h:33
bool operator==(const unique_id< uniquifier > &to_compare) const
Returns true if the held id is the same as "to_compare".
Definition unique_id.h:50
void set_raw_id(uniquifier new_value)
Sets the held identifier to "new_value".
Definition unique_id.h:60
A unique identifier based on integers.
Definition unique_id.h:97
bool operator!() const
provides a way to test whether an id is valid.
Definition unique_id.h:102
unique_int(int initial=0)
implicit default for "initial" of zero indicates bogus id.
Definition unique_id.h:99
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55