2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
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/>.
29 /// Interface for working with LWES event attributes values.
31 public interface IEventAttribute
34 /// Indicates the attribute's value equals the default value for the attribute.
42 /// The attribute's type name.
50 /// The attribute's type token.
58 /// Used by the LWES event system during serialization. Encodes
59 /// the event attribute into the byte buffer.
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
);
70 /// Used by the LWES event system during serialization. Determines
71 /// the number of bytes required to encode the attribute.
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
);
79 /// Gets the value by conversion to type V.
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>
87 /// Mutates the attribute's value.
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);
95 /// Mutates the attribute's value.
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);
103 /// Mutates the attribute's value.
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);
111 /// Mutates the attribute's value.
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);
119 /// Mutates the attribute's value.
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);
127 /// Mutates the attribute's value.
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);
135 /// Mutates the attribute's value.
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);
143 /// Mutates the attribute's value.
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);
151 /// Mutates the attribute's value.
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);
159 /// Tries to get the attribute's value as a UInt16
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);
166 /// Tries to get the attribute's value as a Int16
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);
173 /// Tries to get the attribute's value as a UInt32
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);
180 /// Tries to get the attribute's value as a Int32
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);
187 /// Tries to get the attribute's value as a String
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);
194 /// Tries to get the attribute's value as a IPAddress
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);
201 /// Tries to get the attribute's value as a Int64
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);
208 /// Tries to get the attribute's value as a UInt64
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);
215 /// Tries to get the attribute's value as a Boolean
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
231 /// Encapsulates an event attribute's value along with metadata
232 /// required by serialization.
235 internal struct EventAttribute
<T
> : IEventAttribute
<T
>
239 private static readonly int[] TypeSizeMap
=
246 CalculateEncodedSize
, // calculated by encoding
247 4, // sizeof IP address
253 private const int CalculateEncodedSize
= -1;
254 private const int InvalidTypeSize
= 0;
256 AttributeTemplate _template
;
263 public EventAttribute(AttributeTemplate template
, T
value)
265 _template
= template
;
269 #endregion Constructors
274 /// Indicates the attribute's value equals the default value for the attribute.
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; }
293 get { return _value; }
296 #endregion Properties
301 /// Used by the LWES event system during serialization. Encodes
302 /// the event attribute into the byte buffer.
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)
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
>());
324 case TypeToken
.INT16
:
325 count
+= LwesSerializer
.Write(buffer
, ref ofs
, GetValue
<Int16
>());
327 case TypeToken
.UINT32
:
328 count
+= LwesSerializer
.Write(buffer
, ref ofs
, GetValue
<UInt32
>());
330 case TypeToken
.INT32
:
331 count
+= LwesSerializer
.Write(buffer
, ref ofs
, GetValue
<Int32
>());
333 case TypeToken
.STRING
:
334 count
+= LwesSerializer
.Write(buffer
, ref ofs
, GetValue
<string>(), encoder
);
336 case TypeToken
.IP_ADDR
:
338 Coercion
.TryCoerce(GetValue
<IPAddress
>(), out addr
);
339 count
+= LwesSerializer
.Write(buffer
, ref ofs
, addr
);
341 case TypeToken
.INT64
:
342 count
+= LwesSerializer
.Write(buffer
, ref ofs
, GetValue
<Int64
>());
344 case TypeToken
.UINT64
:
345 count
+= LwesSerializer
.Write(buffer
, ref ofs
, GetValue
<UInt64
>());
347 case TypeToken
.BOOLEAN
:
348 count
+= LwesSerializer
.Write(buffer
, ref ofs
, GetValue
<bool>());
351 throw new InvalidOperationException();
359 /// Used by the LWES event system during serialization. Determines
360 /// the number of bytes required to encode the attribute.
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
);
374 return _template
.GetByteCount() + size
;
376 throw new InvalidOperationException("Cannot calculate byte size for an undefined type");
380 /// Gets the value by conversion to type V.
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
393 return (V
)Convert
.ChangeType(_value
, typeof(T
));
398 /// Mutates the attribute's value.
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);
409 /// Mutates the attribute's value.
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);
420 /// Mutates the attribute's value.
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);
431 /// Mutates the attribute's value.
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);
442 /// Mutates the attribute's value.
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);
453 /// Mutates the attribute's value.
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);
464 /// Mutates the attribute's value.
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);
475 /// Mutates the attribute's value.
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);
486 /// Mutates the attribute's value.
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
)
506 /// Tries to get the attribute's value as a Boolean
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);
516 /// Tries to get the attribute's value as a Boolean
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);
526 /// Tries to get the attribute's value as a Boolean
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);
536 /// Tries to get the attribute's value as a Boolean
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);
546 /// Tries to get the attribute's value as a Boolean
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);
556 /// Tries to get the attribute's value as a Boolean
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);
566 /// Tries to get the attribute's value as a Boolean
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);
576 /// Tries to get the attribute's value as a Boolean
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);
586 /// Tries to get the attribute's value as a Boolean
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);
598 internal static class EventAttribute
603 /// Creates an event attribute from a template and a value.
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);
615 /// Creates an event attribute from a template. The new attribute will
616 /// have a default value.
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
)
644 return value.ToString();
647 internal static IEventAttribute
MutateByCoercion(EventTemplate ev
, AttributeTemplate attr
, ushort value)
649 TypeToken tt
= attr
.TypeToken
;
652 case TypeToken
.UINT16
: return new EventAttribute
<UInt16
>(attr
, value);
653 case TypeToken
.INT16
:
655 if (Coercion
.TryCoerce(value, out sh
))
657 return new EventAttribute
<Int16
>(attr
, sh
);
660 case TypeToken
.UINT32
:
662 if (Coercion
.TryCoerce(value, out ui
))
664 return new EventAttribute
<UInt32
>(attr
, ui
);
667 case TypeToken
.INT32
:
669 if (Coercion
.TryCoerce(value, out i
))
671 return new EventAttribute
<UInt32
>(attr
, i
);
674 case TypeToken
.STRING
:
676 if (Coercion
.TryCoerce(value, out s
))
678 return new EventAttribute
<string>(attr
, s
);
681 case TypeToken
.IP_ADDR
:
683 if (Coercion
.TryCoerce(value, out ip
))
685 return new EventAttribute
<string>(attr
, ip
);
688 case TypeToken
.INT64
:
690 if (Coercion
.TryCoerce(value, out l
))
692 return new EventAttribute
<string>(attr
, l
);
695 case TypeToken
.UINT64
:
697 if (Coercion
.TryCoerce(value, out ul
))
699 return new EventAttribute
<string>(attr
, ul
);
702 case TypeToken
.BOOLEAN
:
704 if (Coercion
.TryCoerce(value, out b
))
706 return new EventAttribute
<string>(attr
, b
);
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
;
721 case TypeToken
.UINT16
:
723 if (Coercion
.TryCoerce(value, out sh
))
725 return new EventAttribute
<UInt16
>(attr
, sh
);
728 case TypeToken
.INT16
: return new EventAttribute
<Int16
>(attr
, value);
729 case TypeToken
.UINT32
:
731 if (Coercion
.TryCoerce(value, out ui
))
733 return new EventAttribute
<UInt32
>(attr
, ui
);
736 case TypeToken
.INT32
:
738 if (Coercion
.TryCoerce(value, out i
))
740 return new EventAttribute
<UInt32
>(attr
, i
);
743 case TypeToken
.STRING
:
745 if (Coercion
.TryCoerce(value, out s
))
747 return new EventAttribute
<string>(attr
, s
);
750 case TypeToken
.IP_ADDR
:
752 if (Coercion
.TryCoerce(value, out ip
))
754 return new EventAttribute
<string>(attr
, ip
);
757 case TypeToken
.INT64
:
759 if (Coercion
.TryCoerce(value, out l
))
761 return new EventAttribute
<string>(attr
, l
);
764 case TypeToken
.UINT64
:
766 if (Coercion
.TryCoerce(value, out ul
))
768 return new EventAttribute
<string>(attr
, ul
);
771 case TypeToken
.BOOLEAN
:
773 if (Coercion
.TryCoerce(value, out b
))
775 return new EventAttribute
<string>(attr
, b
);
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
;
790 case TypeToken
.UINT16
:
792 if (Coercion
.TryCoerce(value, out ush
))
794 return new EventAttribute
<UInt16
>(attr
, ush
);
797 case TypeToken
.INT16
:
799 if (Coercion
.TryCoerce(value, out sh
))
801 return new EventAttribute
<Int16
>(attr
, sh
);
804 case TypeToken
.UINT32
: return new EventAttribute
<Int32
>(attr
, value);
805 case TypeToken
.INT32
:
807 if (Coercion
.TryCoerce(value, out i
))
809 return new EventAttribute
<UInt32
>(attr
, i
);
812 case TypeToken
.STRING
:
814 if (Coercion
.TryCoerce(value, out s
))
816 return new EventAttribute
<string>(attr
, s
);
819 case TypeToken
.IP_ADDR
:
821 if (Coercion
.TryCoerce(value, out ip
))
823 return new EventAttribute
<string>(attr
, ip
);
826 case TypeToken
.INT64
:
828 if (Coercion
.TryCoerce(value, out l
))
830 return new EventAttribute
<string>(attr
, l
);
833 case TypeToken
.UINT64
:
835 if (Coercion
.TryCoerce(value, out ul
))
837 return new EventAttribute
<string>(attr
, ul
);
840 case TypeToken
.BOOLEAN
:
842 if (Coercion
.TryCoerce(value, out b
))
844 return new EventAttribute
<string>(attr
, b
);
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
;
859 case TypeToken
.UINT16
:
861 if (Coercion
.TryCoerce(value, out ush
))
863 return new EventAttribute
<UInt16
>(attr
, ush
);
866 case TypeToken
.INT16
:
868 if (Coercion
.TryCoerce(value, out sh
))
870 return new EventAttribute
<Int16
>(attr
, sh
);
873 case TypeToken
.UINT32
: return new EventAttribute
<UInt32
>(attr
, value);
874 case TypeToken
.INT32
:
876 if (Coercion
.TryCoerce(value, out i
))
878 return new EventAttribute
<UInt32
>(attr
, i
);
881 case TypeToken
.STRING
:
883 if (Coercion
.TryCoerce(value, out s
))
885 return new EventAttribute
<string>(attr
, s
);
888 case TypeToken
.IP_ADDR
:
890 if (Coercion
.TryCoerce(value, out ip
))
892 return new EventAttribute
<string>(attr
, ip
);
895 case TypeToken
.INT64
:
897 if (Coercion
.TryCoerce(value, out l
))
899 return new EventAttribute
<string>(attr
, l
);
902 case TypeToken
.UINT64
:
904 if (Coercion
.TryCoerce(value, out ul
))
906 return new EventAttribute
<string>(attr
, ul
);
909 case TypeToken
.BOOLEAN
:
911 if (Coercion
.TryCoerce(value, out b
))
913 return new EventAttribute
<string>(attr
, b
);
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
;
928 case TypeToken
.UINT16
:
930 if (Coercion
.TryCoerce(value, out ush
))
932 return new EventAttribute
<UInt16
>(attr
, ush
);
935 case TypeToken
.INT16
:
937 if (Coercion
.TryCoerce(value, out sh
))
939 return new EventAttribute
<Int16
>(attr
, sh
);
942 case TypeToken
.UINT32
:
944 if (Coercion
.TryCoerce(value, out ui
))
946 return new EventAttribute
<UInt32
>(attr
, ui
);
949 case TypeToken
.INT32
:
951 if (Coercion
.TryCoerce(value, out i
))
953 return new EventAttribute
<UInt32
>(attr
, i
);
956 case TypeToken
.STRING
: return new EventAttribute
<string>(attr
, value);
957 case TypeToken
.IP_ADDR
:
959 if (Coercion
.TryCoerce(value, out ip
))
961 return new EventAttribute
<IPAddress
>(attr
, ip
);
964 case TypeToken
.INT64
:
966 if (Coercion
.TryCoerce(value, out l
))
968 return new EventAttribute
<long>(attr
, l
);
971 case TypeToken
.UINT64
:
973 if (Coercion
.TryCoerce(value, out ul
))
975 return new EventAttribute
<ulong>(attr
, ul
);
978 case TypeToken
.BOOLEAN
:
980 if (Coercion
.TryCoerce(value, out b
))
982 return new EventAttribute
<bool>(attr
, b
);
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
;
997 case TypeToken
.UINT16
:
999 if (Coercion
.TryCoerce(value, out ush
))
1001 return new EventAttribute
<UInt16
>(attr
, ush
);
1004 case TypeToken
.INT16
:
1006 if (Coercion
.TryCoerce(value, out sh
))
1008 return new EventAttribute
<Int16
>(attr
, sh
);
1011 case TypeToken
.UINT32
:
1013 if (Coercion
.TryCoerce(value, out ui
))
1015 return new EventAttribute
<UInt32
>(attr
, ui
);
1018 case TypeToken
.INT32
:
1020 if (Coercion
.TryCoerce(value, out i
))
1022 return new EventAttribute
<UInt32
>(attr
, i
);
1025 case TypeToken
.STRING
:
1027 if (Coercion
.TryCoerce(value, out s
))
1029 return new EventAttribute
<string>(attr
, s
);
1032 case TypeToken
.IP_ADDR
: return new EventAttribute
<IPAddress
>(attr
, value);
1033 case TypeToken
.INT64
:
1035 if (Coercion
.TryCoerce(value, out l
))
1037 return new EventAttribute
<string>(attr
, l
);
1040 case TypeToken
.UINT64
:
1042 if (Coercion
.TryCoerce(value, out ul
))
1044 return new EventAttribute
<string>(attr
, ul
);
1047 case TypeToken
.BOOLEAN
:
1049 if (Coercion
.TryCoerce(value, out b
))
1051 return new EventAttribute
<string>(attr
, b
);
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
;
1066 case TypeToken
.UINT16
:
1068 if (Coercion
.TryCoerce(value, out ush
))
1070 return new EventAttribute
<UInt16
>(attr
, ush
);
1073 case TypeToken
.INT16
:
1075 if (Coercion
.TryCoerce(value, out sh
))
1077 return new EventAttribute
<Int16
>(attr
, sh
);
1080 case TypeToken
.UINT32
:
1082 if (Coercion
.TryCoerce(value, out ui
))
1084 return new EventAttribute
<UInt32
>(attr
, ui
);
1087 case TypeToken
.INT32
:
1089 if (Coercion
.TryCoerce(value, out i
))
1091 return new EventAttribute
<UInt32
>(attr
, i
);
1094 case TypeToken
.STRING
:
1096 if (Coercion
.TryCoerce(value, out s
))
1098 return new EventAttribute
<string>(attr
, s
);
1101 case TypeToken
.IP_ADDR
:
1103 if (Coercion
.TryCoerce(value, out ip
))
1105 return new EventAttribute
<string>(attr
, ip
);
1108 case TypeToken
.INT64
: return new EventAttribute
<Int64
>(attr
, value);
1109 case TypeToken
.UINT64
:
1111 if (Coercion
.TryCoerce(value, out ul
))
1113 return new EventAttribute
<string>(attr
, ul
);
1116 case TypeToken
.BOOLEAN
:
1118 if (Coercion
.TryCoerce(value, out b
))
1120 return new EventAttribute
<string>(attr
, b
);
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
;
1135 case TypeToken
.UINT16
:
1137 if (Coercion
.TryCoerce(value, out ush
))
1139 return new EventAttribute
<UInt16
>(attr
, ush
);
1142 case TypeToken
.INT16
:
1144 if (Coercion
.TryCoerce(value, out sh
))
1146 return new EventAttribute
<Int16
>(attr
, sh
);
1149 case TypeToken
.UINT32
:
1151 if (Coercion
.TryCoerce(value, out ui
))
1153 return new EventAttribute
<UInt32
>(attr
, ui
);
1156 case TypeToken
.INT32
:
1158 if (Coercion
.TryCoerce(value, out i
))
1160 return new EventAttribute
<UInt32
>(attr
, i
);
1163 case TypeToken
.STRING
:
1165 if (Coercion
.TryCoerce(value, out s
))
1167 return new EventAttribute
<string>(attr
, s
);
1170 case TypeToken
.IP_ADDR
:
1172 if (Coercion
.TryCoerce(value, out ip
))
1174 return new EventAttribute
<string>(attr
, ip
);
1177 case TypeToken
.INT64
:
1179 if (Coercion
.TryCoerce(value, out l
))
1181 return new EventAttribute
<string>(attr
, l
);
1184 case TypeToken
.UINT64
: return new EventAttribute
<UInt64
>(attr
, value);
1185 case TypeToken
.BOOLEAN
:
1187 if (Coercion
.TryCoerce(value, out b
))
1189 return new EventAttribute
<string>(attr
, b
);
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
;
1204 case TypeToken
.UINT16
:
1206 if (Coercion
.TryCoerce(value, out ush
))
1208 return new EventAttribute
<UInt16
>(attr
, ush
);
1211 case TypeToken
.INT16
:
1213 if (Coercion
.TryCoerce(value, out sh
))
1215 return new EventAttribute
<Int16
>(attr
, sh
);
1218 case TypeToken
.UINT32
:
1220 if (Coercion
.TryCoerce(value, out ui
))
1222 return new EventAttribute
<UInt32
>(attr
, ui
);
1225 case TypeToken
.INT32
:
1227 if (Coercion
.TryCoerce(value, out i
))
1229 return new EventAttribute
<UInt32
>(attr
, i
);
1232 case TypeToken
.STRING
:
1234 if (Coercion
.TryCoerce(value, out s
))
1236 return new EventAttribute
<string>(attr
, s
);
1239 case TypeToken
.IP_ADDR
:
1241 if (Coercion
.TryCoerce(value, out ip
))
1243 return new EventAttribute
<string>(attr
, ip
);
1246 case TypeToken
.INT64
:
1248 if (Coercion
.TryCoerce(value, out l
))
1250 return new EventAttribute
<string>(attr
, l
);
1253 case TypeToken
.UINT64
:
1255 if (Coercion
.TryCoerce(value, out ul
))
1257 return new EventAttribute
<string>(attr
, ul
);
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")