| 1 | // | 
|---|
| 2 | // identity.h --- definition of the Identity class | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of the SC Toolkit. | 
|---|
| 10 | // | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | // GNU Library General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 24 | // | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
| 26 | // | 
|---|
| 27 |  | 
|---|
| 28 | #ifndef _util_ref_identity_h | 
|---|
| 29 | #define _util_ref_identity_h | 
|---|
| 30 |  | 
|---|
| 31 | #ifdef __GNUG__ | 
|---|
| 32 | #pragma interface | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include <iostream> | 
|---|
| 36 |  | 
|---|
| 37 | #include <scconfig.h> | 
|---|
| 38 |  | 
|---|
| 39 | namespace sc { | 
|---|
| 40 |  | 
|---|
| 41 | class Identity; | 
|---|
| 42 |  | 
|---|
| 43 | /** Identifier's are used to distinguish and order objects.  On many | 
|---|
| 44 | architectures a pointer to the object will suffice, but the C++ | 
|---|
| 45 | standard only guarantees that this works for two pointers pointing | 
|---|
| 46 | within the same structure or array.  Classes need to inherit from | 
|---|
| 47 | Identity to use this mechanism.  Identity, Identifier, and the | 
|---|
| 48 | shorthand boolean operations may have to be modified for certain | 
|---|
| 49 | architectures.  */ | 
|---|
| 50 | class Identifier { | 
|---|
| 51 | private: | 
|---|
| 52 | const void* id; | 
|---|
| 53 | public: | 
|---|
| 54 | /// Create an Identifier for a null object. | 
|---|
| 55 | Identifier(): id(0) {} | 
|---|
| 56 | /// Create an Identifier for the given object. | 
|---|
| 57 | Identifier(const Identity* i): id((void*)i) {} | 
|---|
| 58 | /// Create an Identifier for the given object. | 
|---|
| 59 | Identifier(const Identifier& i): id(i.id) {} | 
|---|
| 60 | /// The destructor does nothing. | 
|---|
| 61 | ~Identifier() {} | 
|---|
| 62 |  | 
|---|
| 63 | /// Assign to the given Identifier. | 
|---|
| 64 | void operator = (const Identifier& i) { id = i.id; } | 
|---|
| 65 |  | 
|---|
| 66 | /// Less than. | 
|---|
| 67 | int operator < (const Identifier&i) const { return id < i.id; } | 
|---|
| 68 | /// Greater than. | 
|---|
| 69 | int operator > (const Identifier&i) const { return id > i.id; } | 
|---|
| 70 | /// Equal. | 
|---|
| 71 | int operator == (const Identifier&i) const { return id == i.id; } | 
|---|
| 72 | /// Less than or equal. | 
|---|
| 73 | int operator <= (const Identifier&i) const { return id <= i.id; } | 
|---|
| 74 | /// Greater than or equal. | 
|---|
| 75 | int operator >= (const Identifier&i) const { return id >= i.id; } | 
|---|
| 76 | /// Not equal. | 
|---|
| 77 | int operator != (const Identifier&i) const { return id != i.id; } | 
|---|
| 78 |  | 
|---|
| 79 | void print(std::ostream&) const; | 
|---|
| 80 | }; | 
|---|
| 81 |  | 
|---|
| 82 | std::ostream & operator << (std::ostream &o, const Identifier &i); | 
|---|
| 83 |  | 
|---|
| 84 | /** Identity gives objects a unique identity and ordering relationship | 
|---|
| 85 | relative to all other objects. | 
|---|
| 86 |  | 
|---|
| 87 | Identity must be virtually inherited if multiple inheritance is | 
|---|
| 88 | to be used. */ | 
|---|
| 89 | class Identity { | 
|---|
| 90 | public: | 
|---|
| 91 | virtual ~Identity(); | 
|---|
| 92 | /** Return the Identifier for this argument. | 
|---|
| 93 | Usually this is just the pointer to the object. */ | 
|---|
| 94 | Identifier identifier() { return this; } | 
|---|
| 95 | }; | 
|---|
| 96 | /// Less than for two Identity pointers. | 
|---|
| 97 | inline int lt(const Identity*i, const Identity*j) { return i < j; } | 
|---|
| 98 | /// Greater than for two Identity pointers. | 
|---|
| 99 | inline int gt(const Identity*i, const Identity*j) { return i > j; } | 
|---|
| 100 | /// Less than or equal for two Identity pointers. | 
|---|
| 101 | inline int le(const Identity*i, const Identity*j) { return i <= j; } | 
|---|
| 102 | /// Greater than or equal for two Identity pointers. | 
|---|
| 103 | inline int ge(const Identity*i, const Identity*j) { return i >= j; } | 
|---|
| 104 | /// Equal for two Identity pointers. | 
|---|
| 105 | inline int eq(const Identity*i, const Identity*j) { return i == j; } | 
|---|
| 106 | /// Not equal for two Identity pointers. | 
|---|
| 107 | inline int ne(const Identity*i, const Identity*j) { return i != j; } | 
|---|
| 108 | /** Compare for two Identity pointers.  Returns -1, 0, or 1, like | 
|---|
| 109 | the C library function strcmp. */ | 
|---|
| 110 | inline int cmp(const Identity*i, const Identity*j) | 
|---|
| 111 | { | 
|---|
| 112 | return (i==j)?0:((i<j)?-1:1); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | #endif | 
|---|
| 118 |  | 
|---|
| 119 | // Local Variables: | 
|---|
| 120 | // mode: c++ | 
|---|
| 121 | // c-file-style: "CLJ" | 
|---|
| 122 | // End: | 
|---|