ObjectIDGenerator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // System.Runtime.Serialization.ObjectIDGenerator.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. // Lluis Sanchez ([email protected])
  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. using System.Collections;
  30. namespace System.Runtime.Serialization
  31. {
  32. [Serializable]
  33. [MonoTODO ("Serialization format not compatible with.NET")]
  34. [System.Runtime.InteropServices.ComVisibleAttribute (true)]
  35. public class ObjectIDGenerator
  36. {
  37. // Private field
  38. Hashtable table;
  39. long current; // this is the current ID, starts at 1
  40. static InstanceComparer comparer = new InstanceComparer ();
  41. // ObjectIDGenerator must generate a new id for each object instance.
  42. // If two objects have the same state (i.e. the method Equals() returns true),
  43. // each one should have a different id.
  44. // Thus, the object instance cannot be directly used as key of the hashtable.
  45. // InstanceComparer compares object references instead of object content
  46. // (unless the object is inmutable, like strings).
  47. class InstanceComparer: IComparer, IHashCodeProvider
  48. {
  49. int IComparer.Compare (object o1, object o2)
  50. {
  51. if (o1 is string)
  52. return o1.Equals(o2) ? 0 : 1;
  53. else
  54. return (o1 == o2) ? 0 : 1;
  55. }
  56. int IHashCodeProvider.GetHashCode (object o)
  57. {
  58. return object.InternalGetHashCode (o);
  59. }
  60. }
  61. // constructor
  62. public ObjectIDGenerator ()
  63. : base ()
  64. {
  65. table = new Hashtable (comparer, comparer);
  66. current = 1;
  67. }
  68. // Methods
  69. public virtual long GetId (object obj, out bool firstTime)
  70. {
  71. if (obj == null)
  72. throw new ArgumentNullException ("obj");
  73. object val = table [obj];
  74. if (val != null) {
  75. firstTime = false;
  76. return (long) val;
  77. } else {
  78. firstTime = true;
  79. table.Add (obj, current);
  80. return current ++;
  81. }
  82. }
  83. public virtual long HasId (object obj, out bool firstTime)
  84. {
  85. if (obj == null)
  86. throw new ArgumentNullException ("obj");
  87. object val = table [obj];
  88. if (val != null) {
  89. firstTime = false;
  90. return (long) val;
  91. } else {
  92. firstTime = true;
  93. return 0L; // 0 is the null ID
  94. }
  95. }
  96. internal long NextId
  97. {
  98. get { return current ++; }
  99. }
  100. }
  101. }