ObjectIDGenerator.cs 3.4 KB

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