File: MetadataItem.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (288 lines) | stat: -rw-r--r-- 9,585 bytes parent folder | download | duplicates (6)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//---------------------------------------------------------------------
// <copyright file="MetadataItem.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
using System.Globalization;

namespace System.Data.Metadata.Edm
{
    /// <summary>
    /// Represents the base item class for all the metadata
    /// </summary>
    public abstract partial class MetadataItem
    {
        #region Constructors
        /// <summary>
        /// Implementing this internal constructor so that this class can't be derived
        /// outside this assembly
        /// </summary>
        internal MetadataItem()
        {
        }
        internal MetadataItem(MetadataFlags flags)
        {
            _flags = flags;
        }
        #endregion

        #region Fields
        [Flags]
        internal enum MetadataFlags {
            // GlobalItem
            None = 0,  // DataSpace flags are off by one so that zero can be the uninitialized state
            CSpace = 1,   // (1 << 0)
            OSpace = 2,   // (1 << 1)
            OCSpace = 3,  // CSpace | OSpace
            SSpace = 4,   // (1 << 2)
            CSSpace = 5,  // CSpace | SSpace

            DataSpace = OSpace | CSpace | SSpace | OCSpace | CSSpace,

            // MetadataItem
            Readonly = (1 << 3),

            // EdmType
            IsAbstract = (1 << 4),

            // FunctionParameter
            In = (1 << 9),
            Out = (1 << 10),
            InOut = In | Out,
            ReturnValue = (1 << 11),

            ParameterMode = (In | Out | InOut | ReturnValue),
        }
        private MetadataFlags _flags;
        private object _flagsLock = new object();
        private MetadataCollection<MetadataProperty> _itemAttributes;
        private Documentation _documentation;
        #endregion

        #region Properties

        /// <summary>
        /// Returns the kind of the type
        /// </summary>
        public abstract BuiltInTypeKind BuiltInTypeKind
        {
            get;
        }

        /// <summary>
        /// List of item attributes on this type
        /// </summary>
        [MetadataProperty(BuiltInTypeKind.MetadataProperty, true)]
        public ReadOnlyMetadataCollection<MetadataProperty> MetadataProperties
        {
            get
            {
                if (null == _itemAttributes)
                {
                    MetadataPropertyCollection itemAttributes = new MetadataPropertyCollection(this);
                    if (IsReadOnly)
                    {
                        itemAttributes.SetReadOnly();
                    }
                    System.Threading.Interlocked.CompareExchange<MetadataCollection<MetadataProperty>>(
                        ref _itemAttributes, itemAttributes, null);
                }
                return _itemAttributes.AsReadOnlyMetadataCollection();
            }
        }

        /// <summary>
        /// List of item attributes on this type
        /// </summary>
        internal MetadataCollection<MetadataProperty> RawMetadataProperties
        {
            get
            {
                return _itemAttributes;
            }
        }

        /// <summary>
        /// List of item attributes on this type
        /// </summary>
        public Documentation Documentation
        {
            get
            {
                return _documentation; 
            }
            set
            {
                _documentation = value;
            }
        }

        /// <summary>
        /// Identity of the item
        /// </summary>
        internal abstract String Identity { get; }

        /// <summary>
        /// Just checks for identities to be equal
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        internal virtual bool EdmEquals(MetadataItem item)
        {
            return ((null != item) &&
                    ((this == item) || // same reference
                     (this.BuiltInTypeKind == item.BuiltInTypeKind &&
                      this.Identity == item.Identity)));
        }

        /// <summary>
        /// Returns true if this item is not-changeable. Otherwise returns false.
        /// </summary>
        internal bool IsReadOnly
        {
            get
            {
                return GetFlag(MetadataFlags.Readonly);
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Validates the types and sets the readOnly property to true. Once the type is set to readOnly,
        /// it can never be changed. 
        /// </summary>
        internal virtual void SetReadOnly()
        {
            if (!IsReadOnly)
            {
                if (null != _itemAttributes)
                {
                    _itemAttributes.SetReadOnly();
                }
                SetFlag(MetadataFlags.Readonly, true);
            }
        }

        /// <summary>
        /// Builds identity string for this item. By default, the method calls the identity property.
        /// </summary>
        /// <param name="builder"></param>
        internal virtual void BuildIdentity(StringBuilder builder)
        {
            builder.Append(this.Identity);
        }

        /// <summary>
        /// Adds the given metadata property to the metadata property collection
        /// </summary>
        /// <param name="metadataProperty"></param>
        internal void AddMetadataProperties(List<MetadataProperty> metadataProperties)
        {
            this.MetadataProperties.Source.AtomicAddRange(metadataProperties);
        }
        #endregion

        #region MetadataFlags
        internal DataSpace GetDataSpace()
        {
            switch (_flags & MetadataFlags.DataSpace)
            {
                default: return (DataSpace)(-1);
                case MetadataFlags.CSpace: return DataSpace.CSpace;
                case MetadataFlags.OSpace: return DataSpace.OSpace;
                case MetadataFlags.SSpace: return DataSpace.SSpace;
                case MetadataFlags.OCSpace: return DataSpace.OCSpace;
                case MetadataFlags.CSSpace: return DataSpace.CSSpace;
            }
        }
        internal void SetDataSpace(DataSpace space)
        {
            _flags = (_flags & ~MetadataFlags.DataSpace) | (MetadataFlags.DataSpace & Convert(space));
        }
        private static MetadataFlags Convert(DataSpace space) {
            switch (space)
            {
                default: return MetadataFlags.None; // invalid
                case DataSpace.CSpace: return MetadataFlags.CSpace;
                case DataSpace.OSpace: return MetadataFlags.OSpace;
                case DataSpace.SSpace: return MetadataFlags.SSpace;
                case DataSpace.OCSpace: return MetadataFlags.OCSpace;
                case DataSpace.CSSpace: return MetadataFlags.CSSpace;
            }
        }

        internal ParameterMode GetParameterMode()
        {
            switch (_flags & MetadataFlags.ParameterMode)
            {
                default: return (ParameterMode)(-1); // invalid
                case MetadataFlags.In: return ParameterMode.In;
                case MetadataFlags.Out: return ParameterMode.Out;
                case MetadataFlags.InOut: return ParameterMode.InOut;
                case MetadataFlags.ReturnValue: return ParameterMode.ReturnValue;
            }
        }
        internal void SetParameterMode(ParameterMode mode)
        {
            _flags = (_flags & ~MetadataFlags.ParameterMode) | (MetadataFlags.ParameterMode & Convert(mode));
        }
        private static MetadataFlags Convert(ParameterMode mode)
        {
            switch (mode)
            {
                default: return MetadataFlags.ParameterMode; // invalid
                case ParameterMode.In: return MetadataFlags.In;
                case ParameterMode.Out: return MetadataFlags.Out;
                case ParameterMode.InOut: return MetadataFlags.InOut;
                case ParameterMode.ReturnValue: return MetadataFlags.ReturnValue;
            }
        }

        internal bool GetFlag(MetadataFlags flag)
        {
            return (flag == (_flags & flag)); 
        }
        internal void SetFlag(MetadataFlags flag, bool value)
        {
            if ((flag & MetadataFlags.Readonly) == MetadataFlags.Readonly)
            {
                Debug.Assert(System.Convert.ToInt32(flag & ~MetadataFlags.Readonly,CultureInfo.InvariantCulture) == 0,
                            "SetFlag() invoked with Readonly and additional flags.");
            }

            lock (_flagsLock)
            {
                // an attempt to set the ReadOnly flag on a MetadataItem that is already read-only
                // is a no-op
                //
                if (IsReadOnly && ((flag & MetadataFlags.Readonly) == MetadataFlags.Readonly))
                {
                    return;
                }

                Util.ThrowIfReadOnly(this);
                if (value)
                {
                    _flags |= flag;
                }
                else
                {
                    _flags &= ~flag;
                }
            }
        }
        #endregion
    }
}