5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 using System.Collections;
\r
28 using System.Collections.Generic;
\r
30 using System.Reflection;
\r
33 namespace DbLinq.Schema.Dbml.Adapter
\r
38 class EnumType : IDictionary<string, int>, INamedType
\r
40 private string name;
\r
43 get { return name; }
\r
51 private readonly IDictionary<string, int> dictionary;
\r
52 private readonly object owner;
\r
53 private readonly MemberInfo memberInfo;
\r
55 internal static bool IsEnum(string literalType)
\r
58 IDictionary<string, int> values;
\r
59 return Extract(literalType, out enumName, out values);
\r
63 /// Extracts enum name and value from a given string.
\r
64 /// The string is in the following form:
\r
65 /// enumName key1[=value1]{,keyN[=valueN]}
\r
66 /// if enumName is 'enum', then the enum is anonymous
\r
68 /// <param name="literalType"></param>
\r
69 /// <param name="enumName"></param>
\r
70 /// <param name="values"></param>
\r
71 /// <returns></returns>
\r
72 private static bool Extract(string literalType, out string enumName, out IDictionary<string, int> values)
\r
75 values = new Dictionary<string, int>();
\r
77 if (string.IsNullOrEmpty(literalType))
\r
80 var nameValues = literalType.Split(new[] { ' ' }, 2);
\r
81 if (nameValues.Length == 2)
\r
84 string name = nameValues[0].Trim();
\r
85 if (!name.IsIdentifier())
\r
88 // now extract the values
\r
89 IDictionary<string, int> readValues = new Dictionary<string, int>();
\r
90 int currentValue = 1;
\r
91 var keyValues = nameValues[1].Split(',');
\r
92 foreach (var keyValue in keyValues)
\r
94 // a value may indicate its numeric equivalent, or not (in this case, we work the same way as C# enums, with an implicit counter)
\r
95 var keyValueParts = keyValue.Split(new[] { '=' }, 2);
\r
96 var key = keyValueParts[0].Trim();
\r
98 if (!key.IsIdentifier())
\r
101 if (keyValueParts.Length > 1)
\r
103 if (!int.TryParse(keyValueParts[1], out currentValue))
\r
106 readValues[key] = currentValue++;
\r
108 if (name == "enum")
\r
109 enumName = string.Empty;
\r
112 values = readValues;
\r
119 /// Does the opposite: creates a literal string from values
\r
121 private void UpdateMember()
\r
123 var pairs = from kvp in dictionary orderby kvp.Value select kvp;
\r
124 int currentValue = 1;
\r
125 var keyValues = new List<string>();
\r
126 foreach (var pair in pairs)
\r
129 if (pair.Value == currentValue)
\r
130 keyValue = pair.Key;
\r
133 currentValue = pair.Value;
\r
134 keyValue = string.Format("{0}={1}", pair.Key, pair.Value);
\r
136 keyValues.Add(keyValue);
\r
139 string literalType = string.IsNullOrEmpty(Name) ? "enum" : Name;
\r
140 literalType += " ";
\r
141 literalType += string.Join(", ", keyValues.ToArray());
\r
142 MemberInfoExtensions.SetMemberValue(memberInfo, owner, literalType);
\r
145 internal EnumType(object owner, MemberInfo memberInfo)
\r
147 this.owner = owner;
\r
148 this.memberInfo = memberInfo;
\r
150 Extract((string)memberInfo.GetMemberValue(owner), out name, out dictionary);
\r
154 #region IDictionary implementation
\r
156 public void Add(KeyValuePair<string, int> item)
\r
158 dictionary.Add(item);
\r
162 public void Clear()
\r
164 dictionary.Clear();
\r
168 public bool Contains(KeyValuePair<string, int> item)
\r
170 return dictionary.Contains(item);
\r
173 public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex)
\r
175 dictionary.CopyTo(array, arrayIndex);
\r
178 public bool Remove(KeyValuePair<string, int> item)
\r
180 bool removed = dictionary.Remove(item);
\r
187 get { return dictionary.Count; }
\r
190 public bool IsReadOnly
\r
192 get { return dictionary.IsReadOnly; }
\r
195 public bool ContainsKey(string key)
\r
197 return dictionary.ContainsKey(key);
\r
200 public void Add(string key, int value)
\r
202 dictionary.Add(key, value);
\r
206 public bool Remove(string key)
\r
208 bool removed = dictionary.Remove(key);
\r
213 public bool TryGetValue(string key, out int value)
\r
215 return dictionary.TryGetValue(key, out value);
\r
218 public int this[string key]
\r
220 get { return dictionary[key]; }
\r
223 dictionary[key] = value;
\r
228 public ICollection<string> Keys
\r
230 get { return dictionary.Keys; }
\r
233 public ICollection<int> Values
\r
235 get { return dictionary.Values; }
\r
238 IEnumerator IEnumerable.GetEnumerator()
\r
240 return ((IEnumerable<KeyValuePair<string, int>>)this).GetEnumerator();
\r
243 public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
\r
245 return dictionary.GetEnumerator();
\r