~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory/TypeSystem/IInterningProvider.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
 
 
22
namespace ICSharpCode.NRefactory.TypeSystem
 
23
{
 
24
        /// <summary>
 
25
        /// Provider used for interning.
 
26
        /// </summary>
 
27
        /// <remarks>
 
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)
 
32
        /// 
 
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.
 
36
        /// 
 
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.
 
41
        /// </remarks>
 
42
        public abstract class InterningProvider
 
43
        {
 
44
                public static readonly InterningProvider Dummy = new DummyInterningProvider();
 
45
                
 
46
                /// <summary>
 
47
                /// Interns the specified object.
 
48
                /// 
 
49
                /// If the object is freezable, it will be frozen.
 
50
                /// </summary>
 
51
                public abstract ISupportsInterning Intern(ISupportsInterning obj);
 
52
                
 
53
                /// <summary>
 
54
                /// Interns the specified object.
 
55
                /// 
 
56
                /// If the object is freezable, it will be frozen.
 
57
                /// </summary>
 
58
                public T Intern<T>(T obj) where T : class, ISupportsInterning
 
59
                {
 
60
                        ISupportsInterning input = obj;
 
61
                        return (T)Intern(input);
 
62
                }
 
63
                
 
64
                /// <summary>
 
65
                /// Interns the specified string.
 
66
                /// </summary>
 
67
                public abstract string Intern(string text);
 
68
                
 
69
                /// <summary>
 
70
                /// Inters a boxed value type.
 
71
                /// </summary>
 
72
                public abstract object InternValue(object obj);
 
73
                
 
74
                /// <summary>
 
75
                /// Interns the given list. Uses reference equality to compare the list elements.
 
76
                /// </summary>
 
77
                public abstract IList<T> InternList<T>(IList<T> list) where T : class;
 
78
                
 
79
                sealed class DummyInterningProvider : InterningProvider
 
80
                {
 
81
                        public override ISupportsInterning Intern(ISupportsInterning obj)
 
82
                        {
 
83
                                return obj;
 
84
                        }
 
85
                        
 
86
                        public override string Intern(string text)
 
87
                        {
 
88
                                return text;
 
89
                        }
 
90
                        
 
91
                        public override object InternValue(object obj)
 
92
                        {
 
93
                                return obj;
 
94
                        }
 
95
                        
 
96
                        public override IList<T> InternList<T>(IList<T> list)
 
97
                        {
 
98
                                return list;
 
99
                        }
 
100
                }
 
101
        }
 
102
}