Slight edit to copyright statement. Added Diagnostics utility and extension methods...
[lwes-dotnet/github-mirror.git] / Org.Lwes / EventAttribute.cs
bloba8eae94fc6d335c04c8b90a60ea1795ed1974847
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes
22 using System;
23 using System.Net;
24 using System.Text;
26 using Org.Lwes.ESF;
28 /// <summary>
29 /// Interface for working with LWES event attributes values.
30 /// </summary>
31 public interface IEventAttribute
33 /// <summary>
34 /// Indicates the attribute's value equals the default value for the attribute.
35 /// </summary>
36 bool HasDefaultValue
38 get;
41 /// <summary>
42 /// The attribute's type name.
43 /// </summary>
44 string TypeName
46 get;
49 /// <summary>
50 /// The attribute's type token.
51 /// </summary>
52 TypeToken TypeToken
54 get;
57 /// <summary>
58 /// Used by the LWES event system during serialization. Encodes
59 /// the event attribute into the byte buffer.
60 /// </summary>
61 /// <param name="buffer">destination buffer</param>
62 /// <param name="ofs">offset into the buffer where the attribute
63 /// will be encoded</param>
64 /// <param name="encoder">character encoder used for transforming
65 /// character data to bytes</param>
66 /// <returns>number of bytes used during the encoding</returns>
67 int BinaryEncode(byte[] buffer, ref int ofs, Encoder encoder);
69 /// <summary>
70 /// Used by the LWES event system during serialization. Determines
71 /// the number of bytes required to encode the attribute.
72 /// </summary>
73 /// <param name="enc">character encoder used for calculating the
74 /// number of bytes required for character data</param>
75 /// <returns>size in bytes of the value when encoded</returns>
76 int GetByteCount(Encoding enc);
78 /// <summary>
79 /// Gets the value by conversion to type V.
80 /// </summary>
81 /// <typeparam name="V">value type V.</typeparam>
82 /// <returns>the value, converted to type V.</returns>
83 /// <exception cref="InvalidCastException">thrown if the value cannot be converted</exception>
84 V GetValue<V>();
86 /// <summary>
87 /// Mutates the attribute's value.
88 /// </summary>
89 /// <param name="ev">the attribute's event template</param>
90 /// <param name="value">a new value</param>
91 /// <returns>An event attribute containing the value given.</returns>
92 IEventAttribute Mutate(EventTemplate ev, ushort value);
94 /// <summary>
95 /// Mutates the attribute's value.
96 /// </summary>
97 /// <param name="ev">the attribute's event template</param>
98 /// <param name="value">a new value</param>
99 /// <returns>An event attribute containing the value given.</returns>
100 IEventAttribute Mutate(EventTemplate ev, short value);
102 /// <summary>
103 /// Mutates the attribute's value.
104 /// </summary>
105 /// <param name="ev">the attribute's event template</param>
106 /// <param name="value">a new value</param>
107 /// <returns>An event attribute containing the value given.</returns>
108 IEventAttribute Mutate(EventTemplate ev, uint value);
110 /// <summary>
111 /// Mutates the attribute's value.
112 /// </summary>
113 /// <param name="ev">the attribute's event template</param>
114 /// <param name="value">a new value</param>
115 /// <returns>An event attribute containing the value given.</returns>
116 IEventAttribute Mutate(EventTemplate ev, int value);
118 /// <summary>
119 /// Mutates the attribute's value.
120 /// </summary>
121 /// <param name="ev">the attribute's event template</param>
122 /// <param name="value">a new value</param>
123 /// <returns>An event attribute containing the value given.</returns>
124 IEventAttribute Mutate(EventTemplate ev, string value);
126 /// <summary>
127 /// Mutates the attribute's value.
128 /// </summary>
129 /// <param name="ev">the attribute's event template</param>
130 /// <param name="value">a new value</param>
131 /// <returns>An event attribute containing the value given.</returns>
132 IEventAttribute Mutate(EventTemplate ev, IPAddress value);
134 /// <summary>
135 /// Mutates the attribute's value.
136 /// </summary>
137 /// <param name="ev">the attribute's event template</param>
138 /// <param name="value">a new value</param>
139 /// <returns>An event attribute containing the value given.</returns>
140 IEventAttribute Mutate(EventTemplate ev, long value);
142 /// <summary>
143 /// Mutates the attribute's value.
144 /// </summary>
145 /// <param name="ev">the attribute's event template</param>
146 /// <param name="value">a new value</param>
147 /// <returns>An event attribute containing the value given.</returns>
148 IEventAttribute Mutate(EventTemplate ev, ulong value);
150 /// <summary>
151 /// Mutates the attribute's value.
152 /// </summary>
153 /// <param name="ev">the attribute's event template</param>
154 /// <param name="value">a new value</param>
155 /// <returns>An event attribute containing the value given.</returns>
156 IEventAttribute Mutate(EventTemplate ev, bool value);
158 /// <summary>
159 /// Tries to get the attribute's value as a UInt16
160 /// </summary>
161 /// <param name="value">reference to a variable that will hold the value upon success</param>
162 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
163 bool TryGetValue(out ushort value);
165 /// <summary>
166 /// Tries to get the attribute's value as a Int16
167 /// </summary>
168 /// <param name="value">reference to a variable that will hold the value upon success</param>
169 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
170 bool TryGetValue(out short value);
172 /// <summary>
173 /// Tries to get the attribute's value as a UInt32
174 /// </summary>
175 /// <param name="value">reference to a variable that will hold the value upon success</param>
176 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
177 bool TryGetValue(out uint value);
179 /// <summary>
180 /// Tries to get the attribute's value as a Int32
181 /// </summary>
182 /// <param name="value">reference to a variable that will hold the value upon success</param>
183 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
184 bool TryGetValue(out int value);
186 /// <summary>
187 /// Tries to get the attribute's value as a String
188 /// </summary>
189 /// <param name="value">reference to a variable that will hold the value upon success</param>
190 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
191 bool TryGetValue(out string value);
193 /// <summary>
194 /// Tries to get the attribute's value as a IPAddress
195 /// </summary>
196 /// <param name="value">reference to a variable that will hold the value upon success</param>
197 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
198 bool TryGetValue(out IPAddress value);
200 /// <summary>
201 /// Tries to get the attribute's value as a Int64
202 /// </summary>
203 /// <param name="value">reference to a variable that will hold the value upon success</param>
204 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
205 bool TryGetValue(out long value);
207 /// <summary>
208 /// Tries to get the attribute's value as a UInt64
209 /// </summary>
210 /// <param name="value">reference to a variable that will hold the value upon success</param>
211 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
212 bool TryGetValue(out ulong value);
214 /// <summary>
215 /// Tries to get the attribute's value as a Boolean
216 /// </summary>
217 /// <param name="value">reference to a variable that will hold the value upon success</param>
218 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
219 bool TryGetValue(out bool value);
222 internal interface IEventAttribute<T> : IEventAttribute
224 T Value
226 get;
230 /// <summary>
231 /// Encapsulates an event attribute's value along with metadata
232 /// required by serialization.
233 /// </summary>
234 [Serializable]
235 internal struct EventAttribute<T> : IEventAttribute<T>
237 #region Fields
239 private static readonly int[] TypeSizeMap =
242 2, // sizeof(ushort)
243 2, // sizeof(short)
244 4, // sizeof(uint)
245 4, // sizeof(int)
246 CalculateEncodedSize, // calculated by encoding
247 4, // sizeof IP address
248 8, // sizeof(long)
249 8, // sizeof(ulong)
250 1, // sizeof(byte)
253 private const int CalculateEncodedSize = -1;
254 private const int InvalidTypeSize = 0;
256 AttributeTemplate _template;
257 T _value;
259 #endregion Fields
261 #region Constructors
263 public EventAttribute(AttributeTemplate template, T value)
265 _template = template;
266 _value = value;
269 #endregion Constructors
271 #region Properties
273 /// <summary>
274 /// Indicates the attribute's value equals the default value for the attribute.
275 /// </summary>
276 public bool HasDefaultValue
278 get { return Object.Equals(_value, default(T)); }
281 public string TypeName
283 get { return EsfParser.TypeTokenNameMap[(int)_template.TypeToken]; }
286 public TypeToken TypeToken
288 get { return _template.TypeToken; }
291 public T Value
293 get { return _value; }
296 #endregion Properties
298 #region Methods
300 /// <summary>
301 /// Used by the LWES event system during serialization. Encodes
302 /// the event attribute into the byte buffer.
303 /// </summary>
304 /// <param name="buffer">destination buffer</param>
305 /// <param name="offset">offset into the buffer where the attribute
306 /// will be encoded</param>
307 /// <param name="encoder">character encoder used for transforming
308 /// character data to bytes</param>
309 /// <returns>number of bytes used during the encoding</returns>
310 public int BinaryEncode(byte[] buffer, ref int offset, Encoder encoder)
312 /* Encoded using the following protocol:
314 * ATTRIBUTEWORD,TYPETOKEN,(UINT16|INT16|UINT32|INT32|UINT64|INT64|BOOLEAN|STRING)
316 int ofs = offset;
317 int count = _template.BinaryEncode(buffer, ref ofs, encoder);
319 switch (_template.TypeToken)
321 case TypeToken.UINT16:
322 count += LwesSerializer.Write(buffer, ref ofs, GetValue<UInt16>());
323 break;
324 case TypeToken.INT16:
325 count += LwesSerializer.Write(buffer, ref ofs, GetValue<Int16>());
326 break;
327 case TypeToken.UINT32:
328 count += LwesSerializer.Write(buffer, ref ofs, GetValue<UInt32>());
329 break;
330 case TypeToken.INT32:
331 count += LwesSerializer.Write(buffer, ref ofs, GetValue<Int32>());
332 break;
333 case TypeToken.STRING:
334 count += LwesSerializer.Write(buffer, ref ofs, GetValue<string>(), encoder);
335 break;
336 case TypeToken.IP_ADDR:
337 uint addr;
338 Coercion.TryCoerce(GetValue<IPAddress>(), out addr);
339 count += LwesSerializer.Write(buffer, ref ofs, addr);
340 break;
341 case TypeToken.INT64:
342 count += LwesSerializer.Write(buffer, ref ofs, GetValue<Int64>());
343 break;
344 case TypeToken.UINT64:
345 count += LwesSerializer.Write(buffer, ref ofs, GetValue<UInt64>());
346 break;
347 case TypeToken.BOOLEAN:
348 count += LwesSerializer.Write(buffer, ref ofs, GetValue<bool>());
349 break;
350 default:
351 throw new InvalidOperationException();
354 offset = ofs;
355 return count;
358 /// <summary>
359 /// Used by the LWES event system during serialization. Determines
360 /// the number of bytes required to encode the attribute.
361 /// </summary>
362 /// <param name="enc">character encoder used for calculating the
363 /// number of bytes required for character data</param>
364 /// <returns>size in bytes of the value when encoded</returns>
365 public int GetByteCount(Encoding enc)
367 int size = TypeSizeMap[(int)_template.TypeToken];
368 if (size == CalculateEncodedSize)
370 size = enc.GetByteCount(GetValue<string>()) + sizeof(UInt16);
372 if (size > 0)
374 return _template.GetByteCount() + size;
376 throw new InvalidOperationException("Cannot calculate byte size for an undefined type");
379 /// <summary>
380 /// Gets the value by conversion to type V.
381 /// </summary>
382 /// <typeparam name="V">value type V.</typeparam>
383 /// <returns>the value, converted to type V.</returns>
384 /// <exception cref="InvalidCastException">thrown if the value cannot be converted</exception>
385 public V GetValue<V>()
387 if (typeof(T).IsInstanceOfType(_value))
389 return (V)((object)_value); // Implicit conversion by boxing/unboxing
391 else
393 return (V)Convert.ChangeType(_value, typeof(T));
397 /// <summary>
398 /// Mutates the attribute's value.
399 /// </summary>
400 /// <param name="ev">the attribute's event template</param>
401 /// <param name="value">a new value</param>
402 /// <returns>An event attribute containing the value given.</returns>
403 public IEventAttribute Mutate(EventTemplate ev, ushort value)
405 return EventAttribute.MutateByCoercion(ev, _template, value);
408 /// <summary>
409 /// Mutates the attribute's value.
410 /// </summary>
411 /// <param name="ev">the attribute's event template</param>
412 /// <param name="value">a new value</param>
413 /// <returns>An event attribute containing the value given.</returns>
414 public IEventAttribute Mutate(EventTemplate ev, short value)
416 return EventAttribute.MutateByCoercion(ev, _template, value);
419 /// <summary>
420 /// Mutates the attribute's value.
421 /// </summary>
422 /// <param name="ev">the attribute's event template</param>
423 /// <param name="value">a new value</param>
424 /// <returns>An event attribute containing the value given.</returns>
425 public IEventAttribute Mutate(EventTemplate ev, uint value)
427 return EventAttribute.MutateByCoercion(ev, _template, value);
430 /// <summary>
431 /// Mutates the attribute's value.
432 /// </summary>
433 /// <param name="ev">the attribute's event template</param>
434 /// <param name="value">a new value</param>
435 /// <returns>An event attribute containing the value given.</returns>
436 public IEventAttribute Mutate(EventTemplate ev, int value)
438 return EventAttribute.MutateByCoercion(ev, _template, value);
441 /// <summary>
442 /// Mutates the attribute's value.
443 /// </summary>
444 /// <param name="ev">the attribute's event template</param>
445 /// <param name="value">a new value</param>
446 /// <returns>An event attribute containing the value given.</returns>
447 public IEventAttribute Mutate(EventTemplate ev, string value)
449 return EventAttribute.MutateByCoercion(ev, _template, value);
452 /// <summary>
453 /// Mutates the attribute's value.
454 /// </summary>
455 /// <param name="ev">the attribute's event template</param>
456 /// <param name="value">a new value</param>
457 /// <returns>An event attribute containing the value given.</returns>
458 public IEventAttribute Mutate(EventTemplate ev, IPAddress value)
460 return EventAttribute.MutateByCoercion(ev, _template, value);
463 /// <summary>
464 /// Mutates the attribute's value.
465 /// </summary>
466 /// <param name="ev">the attribute's event template</param>
467 /// <param name="value">a new value</param>
468 /// <returns>An event attribute containing the value given.</returns>
469 public IEventAttribute Mutate(EventTemplate ev, ulong value)
471 return EventAttribute.MutateByCoercion(ev, _template, value);
474 /// <summary>
475 /// Mutates the attribute's value.
476 /// </summary>
477 /// <param name="ev">the attribute's event template</param>
478 /// <param name="value">a new value</param>
479 /// <returns>An event attribute containing the value given.</returns>
480 public IEventAttribute Mutate(EventTemplate ev, long value)
482 return EventAttribute.MutateByCoercion(ev, _template, value);
485 /// <summary>
486 /// Mutates the attribute's value.
487 /// </summary>
488 /// <param name="ev">the attribute's event template</param>
489 /// <param name="value">a new value</param>
490 /// <returns>An event attribute containing the value given.</returns>
491 public IEventAttribute Mutate(EventTemplate ev, bool value)
493 return EventAttribute.MutateByCoercion(ev, _template, value);
496 public override string ToString()
498 return String.Concat(_template.Name
499 , ": { Name: '", _template.Name
500 , "', Type: ", _template.TypeToken
501 , ", Value: ", EventAttribute.ConvertValueToString(_template, _value)
502 , "}");
505 /// <summary>
506 /// Tries to get the attribute's value as a Boolean
507 /// </summary>
508 /// <param name="value">reference to a variable that will hold the value upon success</param>
509 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
510 public bool TryGetValue(out ushort value)
512 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
515 /// <summary>
516 /// Tries to get the attribute's value as a Boolean
517 /// </summary>
518 /// <param name="value">reference to a variable that will hold the value upon success</param>
519 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
520 public bool TryGetValue(out short value)
522 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
525 /// <summary>
526 /// Tries to get the attribute's value as a Boolean
527 /// </summary>
528 /// <param name="value">reference to a variable that will hold the value upon success</param>
529 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
530 public bool TryGetValue(out uint value)
532 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
535 /// <summary>
536 /// Tries to get the attribute's value as a Boolean
537 /// </summary>
538 /// <param name="value">reference to a variable that will hold the value upon success</param>
539 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
540 public bool TryGetValue(out int value)
542 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
545 /// <summary>
546 /// Tries to get the attribute's value as a Boolean
547 /// </summary>
548 /// <param name="value">reference to a variable that will hold the value upon success</param>
549 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
550 public bool TryGetValue(out string value)
552 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
555 /// <summary>
556 /// Tries to get the attribute's value as a Boolean
557 /// </summary>
558 /// <param name="value">reference to a variable that will hold the value upon success</param>
559 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
560 public bool TryGetValue(out IPAddress value)
562 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
565 /// <summary>
566 /// Tries to get the attribute's value as a Boolean
567 /// </summary>
568 /// <param name="value">reference to a variable that will hold the value upon success</param>
569 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
570 public bool TryGetValue(out long value)
572 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
575 /// <summary>
576 /// Tries to get the attribute's value as a Boolean
577 /// </summary>
578 /// <param name="value">reference to a variable that will hold the value upon success</param>
579 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
580 public bool TryGetValue(out ulong value)
582 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
585 /// <summary>
586 /// Tries to get the attribute's value as a Boolean
587 /// </summary>
588 /// <param name="value">reference to a variable that will hold the value upon success</param>
589 /// <returns><em>true</em> if the value is retrieved; otherwise <em>false</em></returns>
590 public bool TryGetValue(out bool value)
592 return Coercion.TryCoerce(_template.TypeToken, _value, out value);
595 #endregion Methods
598 internal static class EventAttribute
600 #region Methods
602 /// <summary>
603 /// Creates an event attribute from a template and a value.
604 /// </summary>
605 /// <typeparam name="V">value type V</typeparam>
606 /// <param name="template">an attribute template</param>
607 /// <param name="value">a value for the attribute</param>
608 /// <returns>a new event attribute</returns>
609 public static IEventAttribute Create<V>(AttributeTemplate template, V value)
611 return new EventAttribute<V>(template, value);
614 /// <summary>
615 /// Creates an event attribute from a template. The new attribute will
616 /// have a default value.
617 /// </summary>
618 /// <param name="template">an attribute template</param>
619 /// <returns>a new event attribute</returns>
620 public static IEventAttribute Create(AttributeTemplate template)
622 switch (template.TypeToken)
624 case TypeToken.UINT16: return new EventAttribute<UInt16>(template, default(UInt16));
625 case TypeToken.INT16: return new EventAttribute<Int16>(template, default(Int16));
626 case TypeToken.UINT32: return new EventAttribute<UInt32>(template, default(UInt32));
627 case TypeToken.INT32: return new EventAttribute<Int32>(template, default(Int32));
628 case TypeToken.STRING: return new EventAttribute<string>(template, String.Empty);
629 case TypeToken.IP_ADDR: return new EventAttribute<IPAddress>(template, IPAddress.Any);
630 case TypeToken.INT64: return new EventAttribute<Int64>(template, default(Int64));
631 case TypeToken.UINT64: return new EventAttribute<UInt64>(template, default(UInt64));
632 case TypeToken.BOOLEAN: return new EventAttribute<bool>(template, default(bool));
634 throw new InvalidOperationException("cannot create an event value for an unknown attribute type");
637 internal static string ConvertValueToString<V>(AttributeTemplate attr, V value)
639 if (attr.TypeToken == TypeToken.STRING)
640 return String.Concat("'", value.ToString().Replace("'", "\\'"), "'");
641 else if (attr.TypeToken == TypeToken.UNDEFINED)
642 return "undefined";
643 else
644 return value.ToString();
647 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, ushort value)
649 TypeToken tt = attr.TypeToken;
650 switch (tt)
652 case TypeToken.UINT16: return new EventAttribute<UInt16>(attr, value);
653 case TypeToken.INT16:
654 short sh;
655 if (Coercion.TryCoerce(value, out sh))
657 return new EventAttribute<Int16>(attr, sh);
659 break;
660 case TypeToken.UINT32:
661 uint ui;
662 if (Coercion.TryCoerce(value, out ui))
664 return new EventAttribute<UInt32>(attr, ui);
666 break;
667 case TypeToken.INT32:
668 uint i;
669 if (Coercion.TryCoerce(value, out i))
671 return new EventAttribute<UInt32>(attr, i);
673 break;
674 case TypeToken.STRING:
675 string s;
676 if (Coercion.TryCoerce(value, out s))
678 return new EventAttribute<string>(attr, s);
680 break;
681 case TypeToken.IP_ADDR:
682 string ip;
683 if (Coercion.TryCoerce(value, out ip))
685 return new EventAttribute<string>(attr, ip);
687 break;
688 case TypeToken.INT64:
689 string l;
690 if (Coercion.TryCoerce(value, out l))
692 return new EventAttribute<string>(attr, l);
694 break;
695 case TypeToken.UINT64:
696 string ul;
697 if (Coercion.TryCoerce(value, out ul))
699 return new EventAttribute<string>(attr, ul);
701 break;
702 case TypeToken.BOOLEAN:
703 string b;
704 if (Coercion.TryCoerce(value, out b))
706 return new EventAttribute<string>(attr, b);
708 break;
710 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
711 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
712 , ", received non-coercible type UInt16")
716 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, short value)
718 TypeToken tt = attr.TypeToken;
719 switch (tt)
721 case TypeToken.UINT16:
722 ushort sh;
723 if (Coercion.TryCoerce(value, out sh))
725 return new EventAttribute<UInt16>(attr, sh);
727 break;
728 case TypeToken.INT16: return new EventAttribute<Int16>(attr, value);
729 case TypeToken.UINT32:
730 uint ui;
731 if (Coercion.TryCoerce(value, out ui))
733 return new EventAttribute<UInt32>(attr, ui);
735 break;
736 case TypeToken.INT32:
737 uint i;
738 if (Coercion.TryCoerce(value, out i))
740 return new EventAttribute<UInt32>(attr, i);
742 break;
743 case TypeToken.STRING:
744 string s;
745 if (Coercion.TryCoerce(value, out s))
747 return new EventAttribute<string>(attr, s);
749 break;
750 case TypeToken.IP_ADDR:
751 string ip;
752 if (Coercion.TryCoerce(value, out ip))
754 return new EventAttribute<string>(attr, ip);
756 break;
757 case TypeToken.INT64:
758 string l;
759 if (Coercion.TryCoerce(value, out l))
761 return new EventAttribute<string>(attr, l);
763 break;
764 case TypeToken.UINT64:
765 string ul;
766 if (Coercion.TryCoerce(value, out ul))
768 return new EventAttribute<string>(attr, ul);
770 break;
771 case TypeToken.BOOLEAN:
772 string b;
773 if (Coercion.TryCoerce(value, out b))
775 return new EventAttribute<string>(attr, b);
777 break;
779 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
780 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
781 , ", received non-coercible type Int16")
785 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, int value)
787 TypeToken tt = attr.TypeToken;
788 switch (tt)
790 case TypeToken.UINT16:
791 ushort ush;
792 if (Coercion.TryCoerce(value, out ush))
794 return new EventAttribute<UInt16>(attr, ush);
796 break;
797 case TypeToken.INT16:
798 short sh;
799 if (Coercion.TryCoerce(value, out sh))
801 return new EventAttribute<Int16>(attr, sh);
803 break;
804 case TypeToken.UINT32: return new EventAttribute<Int32>(attr, value);
805 case TypeToken.INT32:
806 uint i;
807 if (Coercion.TryCoerce(value, out i))
809 return new EventAttribute<UInt32>(attr, i);
811 break;
812 case TypeToken.STRING:
813 string s;
814 if (Coercion.TryCoerce(value, out s))
816 return new EventAttribute<string>(attr, s);
818 break;
819 case TypeToken.IP_ADDR:
820 string ip;
821 if (Coercion.TryCoerce(value, out ip))
823 return new EventAttribute<string>(attr, ip);
825 break;
826 case TypeToken.INT64:
827 string l;
828 if (Coercion.TryCoerce(value, out l))
830 return new EventAttribute<string>(attr, l);
832 break;
833 case TypeToken.UINT64:
834 string ul;
835 if (Coercion.TryCoerce(value, out ul))
837 return new EventAttribute<string>(attr, ul);
839 break;
840 case TypeToken.BOOLEAN:
841 string b;
842 if (Coercion.TryCoerce(value, out b))
844 return new EventAttribute<string>(attr, b);
846 break;
848 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
849 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
850 , ", received non-coercible type Int16")
854 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, uint value)
856 TypeToken tt = attr.TypeToken;
857 switch (tt)
859 case TypeToken.UINT16:
860 ushort ush;
861 if (Coercion.TryCoerce(value, out ush))
863 return new EventAttribute<UInt16>(attr, ush);
865 break;
866 case TypeToken.INT16:
867 short sh;
868 if (Coercion.TryCoerce(value, out sh))
870 return new EventAttribute<Int16>(attr, sh);
872 break;
873 case TypeToken.UINT32: return new EventAttribute<UInt32>(attr, value);
874 case TypeToken.INT32:
875 uint i;
876 if (Coercion.TryCoerce(value, out i))
878 return new EventAttribute<UInt32>(attr, i);
880 break;
881 case TypeToken.STRING:
882 string s;
883 if (Coercion.TryCoerce(value, out s))
885 return new EventAttribute<string>(attr, s);
887 break;
888 case TypeToken.IP_ADDR:
889 string ip;
890 if (Coercion.TryCoerce(value, out ip))
892 return new EventAttribute<string>(attr, ip);
894 break;
895 case TypeToken.INT64:
896 string l;
897 if (Coercion.TryCoerce(value, out l))
899 return new EventAttribute<string>(attr, l);
901 break;
902 case TypeToken.UINT64:
903 string ul;
904 if (Coercion.TryCoerce(value, out ul))
906 return new EventAttribute<string>(attr, ul);
908 break;
909 case TypeToken.BOOLEAN:
910 string b;
911 if (Coercion.TryCoerce(value, out b))
913 return new EventAttribute<string>(attr, b);
915 break;
917 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
918 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
919 , ", received non-coercible type UInt16")
923 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, string value)
925 TypeToken tt = attr.TypeToken;
926 switch (tt)
928 case TypeToken.UINT16:
929 ushort ush;
930 if (Coercion.TryCoerce(value, out ush))
932 return new EventAttribute<UInt16>(attr, ush);
934 break;
935 case TypeToken.INT16:
936 short sh;
937 if (Coercion.TryCoerce(value, out sh))
939 return new EventAttribute<Int16>(attr, sh);
941 break;
942 case TypeToken.UINT32:
943 uint ui;
944 if (Coercion.TryCoerce(value, out ui))
946 return new EventAttribute<UInt32>(attr, ui);
948 break;
949 case TypeToken.INT32:
950 uint i;
951 if (Coercion.TryCoerce(value, out i))
953 return new EventAttribute<UInt32>(attr, i);
955 break;
956 case TypeToken.STRING: return new EventAttribute<string>(attr, value);
957 case TypeToken.IP_ADDR:
958 IPAddress ip;
959 if (Coercion.TryCoerce(value, out ip))
961 return new EventAttribute<IPAddress>(attr, ip);
963 break;
964 case TypeToken.INT64:
965 long l;
966 if (Coercion.TryCoerce(value, out l))
968 return new EventAttribute<long>(attr, l);
970 break;
971 case TypeToken.UINT64:
972 ulong ul;
973 if (Coercion.TryCoerce(value, out ul))
975 return new EventAttribute<ulong>(attr, ul);
977 break;
978 case TypeToken.BOOLEAN:
979 bool b;
980 if (Coercion.TryCoerce(value, out b))
982 return new EventAttribute<bool>(attr, b);
984 break;
986 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
987 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
988 , ", received non-coercible type string")
992 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, IPAddress value)
994 TypeToken tt = attr.TypeToken;
995 switch (tt)
997 case TypeToken.UINT16:
998 ushort ush;
999 if (Coercion.TryCoerce(value, out ush))
1001 return new EventAttribute<UInt16>(attr, ush);
1003 break;
1004 case TypeToken.INT16:
1005 short sh;
1006 if (Coercion.TryCoerce(value, out sh))
1008 return new EventAttribute<Int16>(attr, sh);
1010 break;
1011 case TypeToken.UINT32:
1012 uint ui;
1013 if (Coercion.TryCoerce(value, out ui))
1015 return new EventAttribute<UInt32>(attr, ui);
1017 break;
1018 case TypeToken.INT32:
1019 uint i;
1020 if (Coercion.TryCoerce(value, out i))
1022 return new EventAttribute<UInt32>(attr, i);
1024 break;
1025 case TypeToken.STRING:
1026 string s;
1027 if (Coercion.TryCoerce(value, out s))
1029 return new EventAttribute<string>(attr, s);
1031 break;
1032 case TypeToken.IP_ADDR: return new EventAttribute<IPAddress>(attr, value);
1033 case TypeToken.INT64:
1034 string l;
1035 if (Coercion.TryCoerce(value, out l))
1037 return new EventAttribute<string>(attr, l);
1039 break;
1040 case TypeToken.UINT64:
1041 string ul;
1042 if (Coercion.TryCoerce(value, out ul))
1044 return new EventAttribute<string>(attr, ul);
1046 break;
1047 case TypeToken.BOOLEAN:
1048 string b;
1049 if (Coercion.TryCoerce(value, out b))
1051 return new EventAttribute<string>(attr, b);
1053 break;
1055 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
1056 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
1057 , ", received non-coercible type IPAddress")
1061 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, long value)
1063 TypeToken tt = attr.TypeToken;
1064 switch (tt)
1066 case TypeToken.UINT16:
1067 ushort ush;
1068 if (Coercion.TryCoerce(value, out ush))
1070 return new EventAttribute<UInt16>(attr, ush);
1072 break;
1073 case TypeToken.INT16:
1074 short sh;
1075 if (Coercion.TryCoerce(value, out sh))
1077 return new EventAttribute<Int16>(attr, sh);
1079 break;
1080 case TypeToken.UINT32:
1081 uint ui;
1082 if (Coercion.TryCoerce(value, out ui))
1084 return new EventAttribute<UInt32>(attr, ui);
1086 break;
1087 case TypeToken.INT32:
1088 uint i;
1089 if (Coercion.TryCoerce(value, out i))
1091 return new EventAttribute<UInt32>(attr, i);
1093 break;
1094 case TypeToken.STRING:
1095 string s;
1096 if (Coercion.TryCoerce(value, out s))
1098 return new EventAttribute<string>(attr, s);
1100 break;
1101 case TypeToken.IP_ADDR:
1102 string ip;
1103 if (Coercion.TryCoerce(value, out ip))
1105 return new EventAttribute<string>(attr, ip);
1107 break;
1108 case TypeToken.INT64: return new EventAttribute<Int64>(attr, value);
1109 case TypeToken.UINT64:
1110 string ul;
1111 if (Coercion.TryCoerce(value, out ul))
1113 return new EventAttribute<string>(attr, ul);
1115 break;
1116 case TypeToken.BOOLEAN:
1117 string b;
1118 if (Coercion.TryCoerce(value, out b))
1120 return new EventAttribute<string>(attr, b);
1122 break;
1124 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
1125 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
1126 , ", received non-coercible type Int64")
1130 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, ulong value)
1132 TypeToken tt = attr.TypeToken;
1133 switch (tt)
1135 case TypeToken.UINT16:
1136 ushort ush;
1137 if (Coercion.TryCoerce(value, out ush))
1139 return new EventAttribute<UInt16>(attr, ush);
1141 break;
1142 case TypeToken.INT16:
1143 short sh;
1144 if (Coercion.TryCoerce(value, out sh))
1146 return new EventAttribute<Int16>(attr, sh);
1148 break;
1149 case TypeToken.UINT32:
1150 uint ui;
1151 if (Coercion.TryCoerce(value, out ui))
1153 return new EventAttribute<UInt32>(attr, ui);
1155 break;
1156 case TypeToken.INT32:
1157 uint i;
1158 if (Coercion.TryCoerce(value, out i))
1160 return new EventAttribute<UInt32>(attr, i);
1162 break;
1163 case TypeToken.STRING:
1164 string s;
1165 if (Coercion.TryCoerce(value, out s))
1167 return new EventAttribute<string>(attr, s);
1169 break;
1170 case TypeToken.IP_ADDR:
1171 string ip;
1172 if (Coercion.TryCoerce(value, out ip))
1174 return new EventAttribute<string>(attr, ip);
1176 break;
1177 case TypeToken.INT64:
1178 string l;
1179 if (Coercion.TryCoerce(value, out l))
1181 return new EventAttribute<string>(attr, l);
1183 break;
1184 case TypeToken.UINT64: return new EventAttribute<UInt64>(attr, value);
1185 case TypeToken.BOOLEAN:
1186 string b;
1187 if (Coercion.TryCoerce(value, out b))
1189 return new EventAttribute<string>(attr, b);
1191 break;
1193 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
1194 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
1195 , ", received non-coercible type UInt64")
1199 internal static IEventAttribute MutateByCoercion(EventTemplate ev, AttributeTemplate attr, bool value)
1201 TypeToken tt = attr.TypeToken;
1202 switch (tt)
1204 case TypeToken.UINT16:
1205 ushort ush;
1206 if (Coercion.TryCoerce(value, out ush))
1208 return new EventAttribute<UInt16>(attr, ush);
1210 break;
1211 case TypeToken.INT16:
1212 short sh;
1213 if (Coercion.TryCoerce(value, out sh))
1215 return new EventAttribute<Int16>(attr, sh);
1217 break;
1218 case TypeToken.UINT32:
1219 uint ui;
1220 if (Coercion.TryCoerce(value, out ui))
1222 return new EventAttribute<UInt32>(attr, ui);
1224 break;
1225 case TypeToken.INT32:
1226 uint i;
1227 if (Coercion.TryCoerce(value, out i))
1229 return new EventAttribute<UInt32>(attr, i);
1231 break;
1232 case TypeToken.STRING:
1233 string s;
1234 if (Coercion.TryCoerce(value, out s))
1236 return new EventAttribute<string>(attr, s);
1238 break;
1239 case TypeToken.IP_ADDR:
1240 string ip;
1241 if (Coercion.TryCoerce(value, out ip))
1243 return new EventAttribute<string>(attr, ip);
1245 break;
1246 case TypeToken.INT64:
1247 string l;
1248 if (Coercion.TryCoerce(value, out l))
1250 return new EventAttribute<string>(attr, l);
1252 break;
1253 case TypeToken.UINT64:
1254 string ul;
1255 if (Coercion.TryCoerce(value, out ul))
1257 return new EventAttribute<string>(attr, ul);
1259 break;
1260 case TypeToken.BOOLEAN: return new EventAttribute<bool>(attr, value);
1262 throw new NoSuchAttributeTypeException(string.Concat("type mismatch attempting to set "
1263 , ev.Name, ".", attr.Name, ": expecting " + EsfParser.TypeTokenNameMap[(int)tt]
1264 , ", received non-coercible type Boolean")
1268 #endregion Methods