PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Ruby/Runtime/ConstantStorage.cs

#
C# | 70 lines | 45 code | 10 blank | 15 comment | 8 complexity | 6888733effba3a8b5f5d0e7913c6d8ee MD5 | raw file
 1/* ****************************************************************************
 2 *
 3 * Copyright (c) Microsoft Corporation. 
 4 *
 5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 6 * copy of the license can be found in the License.html file at the root of this distribution. If 
 7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
 8 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 9 * by the terms of the Apache License, Version 2.0.
10 *
11 * You must not remove this notice, or any other, from this software.
12 *
13 *
14 * ***************************************************************************/
15
16using System;
17using Microsoft.Scripting.Utils;
18using IronRuby.Builtins;
19
20namespace IronRuby.Runtime {
21    internal struct ConstantStorage {
22        internal static ConstantStorage Removed = new ConstantStorage(false);
23
24        public readonly object Value;
25        public readonly WeakReference WeakValue;
26
27        internal bool IsRemoved {
28            get { return Value == Removed.Value; }
29        }
30
31        internal ConstantStorage(object value) {
32            Value = value;
33
34            if (value == null) {
35                // We use null as a missing constant sentinel and the real null is wrapped in a singleton WeakRef:
36                WeakValue = ConstantSiteCache.WeakNull;
37            } else {
38                switch (Type.GetTypeCode(value.GetType())) {
39                    case TypeCode.Object:
40                        RubyModule module = value as RubyModule;
41                        if (module != null) {
42                            WeakValue = module.WeakSelf;
43                        } else {
44                            WeakValue = new WeakReference(value);
45                        }
46                        break;
47
48                    case TypeCode.String:
49                        WeakValue = new WeakReference(value);
50                        break;
51
52                    default:
53                        WeakValue = null;
54                        break;
55                }
56            }
57        }
58
59        internal ConstantStorage(object value, WeakReference weakValue) {
60            Assert.NotNull(value, weakValue);
61            Value = value;
62            WeakValue = weakValue;
63        }
64
65        private ConstantStorage(bool dummy) {
66            Value = new object();
67            WeakValue = null;
68        }
69    }
70}