Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ObjectIDGenerator.cs « System.Runtime.Serialization « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c2821892a26d9c4cad4ee3adcc5cb32066137467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// System.Runtime.Serialization.ObjectIDGenerator.cs
//
// Author: Duncan Mak  (duncan@ximian.com)
//
// (C) Ximian, Inc.
//

using System;
using System.Collections;
using System.Runtime.Serialization;

namespace System.Runtime.Serialization
{
	[Serializable]
	public class ObjectIDGenerator
	{
		// Private field
		Hashtable table;
		long current; // this is the current ID, starts at 1
		
		// constructor
		public ObjectIDGenerator ()
			: base ()
		{
			table = new Hashtable ();
			current = 1;
		}

		// Methods
		public virtual long GetId (object obj, out bool firstTime)
		{
			if (obj == null)
				throw new ArgumentNullException ("The obj parameter is null.");

			if (table.ContainsKey (obj)) {
				firstTime = false;
				return (long) table [obj];

			} else {
				firstTime = true;
				table.Add (obj, current);
				return current ++; 
			}
		}

		public virtual long HasId (object obj, out bool firstTime)
		{
			if (obj == null)
				throw new ArgumentNullException ("The obj parameter is null.");

			if (table.ContainsKey (obj)) {
				firstTime = false;
				return (long) table [obj];

			} else {				
				firstTime = true;
				return 0L; // 0 is the null ID
			}
		}
	}
}