1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
22
namespace ICSharpCode.NRefactory.TypeSystem
25
/// Provider used for interning.
28
/// A simple IInterningProvider implementation could use 3 dictionaries:
29
/// 1. using value equality comparer (for certain types known to implement value equality, e.g. string and IType)
30
/// 2. using comparer that calls into ISupportsInterning (for types implementing ISupportsInterning)
31
/// 3. list comparer (for InternList method)
33
/// On the first Intern()-call, the provider tells the object to prepare for interning (ISupportsInterning.PrepareForInterning)
34
/// and stores it into a dictionary. On further Intern() calls, the original object is returned for all equal objects.
35
/// This allows reducing the memory usage by using a single object instance where possible.
37
/// Interning provider implementations could also use the interning logic for different purposes:
38
/// for example, it could be used to determine which objects are used jointly between multiple type definitions
39
/// and which are used only within a single type definition. Then a persistent file format could be organized so
40
/// that shared objects are loaded only once, yet non-shared objects get loaded lazily together with the class.
42
public abstract class InterningProvider
44
public static readonly InterningProvider Dummy = new DummyInterningProvider();
47
/// Interns the specified object.
49
/// If the object is freezable, it will be frozen.
51
public abstract ISupportsInterning Intern(ISupportsInterning obj);
54
/// Interns the specified object.
56
/// If the object is freezable, it will be frozen.
58
public T Intern<T>(T obj) where T : class, ISupportsInterning
60
ISupportsInterning input = obj;
61
return (T)Intern(input);
65
/// Interns the specified string.
67
public abstract string Intern(string text);
70
/// Inters a boxed value type.
72
public abstract object InternValue(object obj);
75
/// Interns the given list. Uses reference equality to compare the list elements.
77
public abstract IList<T> InternList<T>(IList<T> list) where T : class;
79
sealed class DummyInterningProvider : InterningProvider
81
public override ISupportsInterning Intern(ISupportsInterning obj)
86
public override string Intern(string text)
91
public override object InternValue(object obj)
96
public override IList<T> InternList<T>(IList<T> list)