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
40 class EnumType : IDictionary<string, int>, INamedType
\r
42 private string name;
\r
45 get { return name; }
\r
53 private readonly IDictionary<string, int> dictionary;
\r
54 private readonly object owner;
\r
55 private readonly MemberInfo memberInfo;
\r
57 internal static bool IsEnum(string literalType)
\r
60 IDictionary<string, int> values;
\r
61 return Extract(literalType, out enumName, out values);
\r
65 /// Extracts enum name and value from a given string.
\r
66 /// The string is in the following form:
\r
67 /// enumName key1[=value1]{,keyN[=valueN]}
\r
68 /// if enumName is 'enum', then the enum is anonymous
\r
70 /// <param name="literalType"></param>
\r
71 /// <param name="enumName"></param>
\r
72 /// <param name="values"></param>
\r
73 /// <returns></returns>
\r
74 private static bool Extract(string literalType, out string enumName, out IDictionary<string, int> values)
\r
77 values = new Dictionary<string, int>();
\r
79 if (string.IsNullOrEmpty(literalType))
\r
82 var nameValues = literalType.Split(new[] { ' ' }, 2);
\r
83 if (nameValues.Length == 2)
\r
86 string name = nameValues[0].Trim();
\r
87 if (!name.IsIdentifier())
\r
90 // now extract the values
\r
91 IDictionary<string, int> readValues = new Dictionary<string, int>();
\r
92 int currentValue = 1;
\r
93 var keyValues = nameValues[1].Split(',');
\r
94 foreach (var keyValue in keyValues)
\r
96 // 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
97 var keyValueParts = keyValue.Split(new[] { '=' }, 2);
\r
98 var key = keyValueParts[0].Trim();
\r
100 if (!key.IsIdentifier())
\r
103 if (keyValueParts.Length > 1)
\r
105 if (!int.TryParse(keyValueParts[1], out currentValue))
\r
108 readValues[key] = currentValue++;
\r
110 if (name == "enum")
\r
111 enumName = string.Empty;
\r
114 values = readValues;
\r
121 /// Does the opposite: creates a literal string from values
\r
123 private void UpdateMember()
\r
125 var pairs = from kvp in dictionary orderby kvp.Value select kvp;
\r
126 int currentValue = 1;
\r
127 var keyValues = new List<string>();
\r
128 foreach (var pair in pairs)
\r
131 if (pair.Value == currentValue)
\r
132 keyValue = pair.Key;
\r
135 currentValue = pair.Value;
\r
136 keyValue = string.Format("{0}={1}", pair.Key, pair.Value);
\r
138 keyValues.Add(keyValue);
\r
141 string literalType = string.IsNullOrEmpty(Name) ? "enum" : Name;
\r
142 literalType += " ";
\r
143 literalType += string.Join(", ", keyValues.ToArray());
\r
144 MemberInfoExtensions.SetMemberValue(memberInfo, owner, literalType);
\r
147 internal EnumType(object owner, MemberInfo memberInfo)
\r
149 this.owner = owner;
\r
150 this.memberInfo = memberInfo;
\r
152 Extract((string)memberInfo.GetMemberValue(owner), out name, out dictionary);
\r
156 #region IDictionary implementation
\r
158 public void Add(KeyValuePair<string, int> item)
\r
160 dictionary.Add(item);
\r
164 public void Clear()
\r
166 dictionary.Clear();
\r
170 public bool Contains(KeyValuePair<string, int> item)
\r
172 return dictionary.Contains(item);
\r
175 public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex)
\r
177 dictionary.CopyTo(array, arrayIndex);
\r
180 public bool Remove(KeyValuePair<string, int> item)
\r
182 bool removed = dictionary.Remove(item);
\r
189 get { return dictionary.Count; }
\r
192 public bool IsReadOnly
\r
194 get { return dictionary.IsReadOnly; }
\r
197 public bool ContainsKey(string key)
\r
199 return dictionary.ContainsKey(key);
\r
202 public void Add(string key, int value)
\r
204 dictionary.Add(key, value);
\r
208 public bool Remove(string key)
\r
210 bool removed = dictionary.Remove(key);
\r
215 public bool TryGetValue(string key, out int value)
\r
217 return dictionary.TryGetValue(key, out value);
\r
220 public int this[string key]
\r
222 get { return dictionary[key]; }
\r
225 dictionary[key] = value;
\r
230 public ICollection<string> Keys
\r
232 get { return dictionary.Keys; }
\r
235 public ICollection<int> Values
\r
237 get { return dictionary.Values; }
\r
240 IEnumerator IEnumerable.GetEnumerator()
\r
242 return ((IEnumerable<KeyValuePair<string, int>>)this).GetEnumerator();
\r
245 public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
\r
247 return dictionary.GetEnumerator();
\r