79b259f66785978d0808df3f2e6341a4558ab5d6
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / ObjectIDGenerator.cs
1 //
2 // System.Runtime.Serialization.ObjectIDGenerator.cs
3 //
4 // Author: Duncan Mak  (duncan@ximian.com)
5 //         Lluis Sanchez (lsg@ctv.es)
6 //
7 // (C) Ximian, Inc.
8 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31
32 namespace System.Runtime.Serialization
33 {
34         [Serializable]
35         [MonoTODO ("Serialization format not compatible with.NET")]
36         [System.Runtime.InteropServices.ComVisibleAttribute (true)]
37         public class ObjectIDGenerator
38         {
39                 // Private field
40                 Hashtable table;
41                 long current; // this is the current ID, starts at 1
42                 static InstanceComparer comparer = new InstanceComparer ();
43
44
45                 // ObjectIDGenerator must generate a new id for each object instance.
46                 // If two objects have the same state (i.e. the method Equals() returns true),
47                 // each one should have a different id.
48                 // Thus, the object instance cannot be directly used as key of the hashtable.
49                 // InstanceComparer compares object references instead of object content
50                 // (unless the object is inmutable, like strings).
51
52                 class InstanceComparer: IComparer, IHashCodeProvider
53                 {
54                         int IComparer.Compare (object o1, object o2)
55                         {
56                                 if (o1 is string)
57                                         return o1.Equals(o2) ? 0 : 1;
58                                 else 
59                                         return (o1 == o2) ? 0 : 1;
60                         }
61
62                         int IHashCodeProvider.GetHashCode (object o)
63                         {
64 #if !INSIDE_CORLIB && MOONLIGHT
65                                 return o == null ? 0 : o .GetHashCode ();
66 #else
67                                 return object.InternalGetHashCode (o);
68 #endif
69                         }
70                 }
71                 
72                 // constructor
73                 public ObjectIDGenerator ()
74                         : base ()
75                 {
76                         table = new Hashtable (comparer, comparer);
77                         current = 1;
78                 }
79
80                 // Methods
81                 public virtual long GetId (object obj, out bool firstTime)
82                 {
83                         if (obj == null)
84                                 throw new ArgumentNullException ("obj");
85
86                         object val = table [obj];
87
88                         if (val != null) {
89                                 firstTime = false;
90                                 return (long) val;
91
92                         } else {
93                                 firstTime = true;
94                                 table.Add (obj, current);
95                                 return current ++; 
96                         }
97                 }
98
99                 public virtual long HasId (object obj, out bool firstTime)
100                 {
101                         if (obj == null)
102                                 throw new ArgumentNullException ("obj");
103
104                         object val = table [obj];
105  
106                         if (val != null) {
107                                 firstTime = false;
108                                 return (long) val;
109
110                         } else {                                
111                                 firstTime = true;
112                                 return 0L; // 0 is the null ID
113                         }
114                 }
115
116                 internal long NextId
117                 {
118                         get { return current ++; }
119                 }
120         }
121 }