| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152 |
- /* Copyright 2010-present MongoDB Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using MongoDB.Bson.Serialization.Attributes;
- namespace MongoDB.Bson.Serialization.Options
- {
- /// <summary>
- /// Represents the external representation of a field or property.
- /// </summary>
- public class RepresentationConverter
- {
- // private fields
- private bool _allowOverflow;
- private bool _allowTruncation;
- // constructors
- /// <summary>
- /// Initializes a new instance of the RepresentationConverter class.
- /// </summary>
- /// <param name="allowOverflow">Whether to allow overflow.</param>
- /// <param name="allowTruncation">Whether to allow truncation.</param>
- public RepresentationConverter(bool allowOverflow, bool allowTruncation)
- {
- _allowOverflow = allowOverflow;
- _allowTruncation = allowTruncation;
- }
- // public properties
- /// <summary>
- /// Gets whether to allow overflow.
- /// </summary>
- public bool AllowOverflow
- {
- get { return _allowOverflow; }
- }
- /// <summary>
- /// Gets whether to allow truncation.
- /// </summary>
- public bool AllowTruncation
- {
- get { return _allowTruncation; }
- }
- // public methods
- /// <summary>
- /// Converts a Decimal128 to a Decimal.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>A Decimal.</returns>
- public decimal ToDecimal(Decimal128 value)
- {
- if (value == Decimal128.MaxValue)
- {
- return decimal.MaxValue;
- }
- else if (value == Decimal128.MinValue)
- {
- return decimal.MinValue;
- }
- else if (Decimal128.IsInfinity(value) || Decimal128.IsNaN(value))
- {
- throw new OverflowException();
- }
- decimal decimalValue;
- if (_allowOverflow)
- {
- try { decimalValue = (decimal)value; } catch (OverflowException) { decimalValue = Decimal128.IsNegative(value) ? decimal.MinValue : decimal.MaxValue; }
- }
- else
- {
- decimalValue = (decimal)value;
- }
- if (!_allowTruncation && value != (Decimal128)decimalValue)
- {
- throw new TruncationException();
- }
- return decimalValue;
- }
- /// <summary>
- /// Converts a Double to a Decimal.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>A Decimal.</returns>
- public decimal ToDecimal(double value)
- {
- if (value == double.MinValue)
- {
- return decimal.MinValue;
- }
- else if (value == double.MaxValue)
- {
- return decimal.MaxValue;
- }
- var decimalValue = (decimal)value;
- if (value < (double)decimal.MinValue || value > (double)decimal.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)decimalValue)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return decimalValue;
- }
- /// <summary>
- /// Converts an Int32 to a Decimal.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>A Decimal.</returns>
- public decimal ToDecimal(int value)
- {
- return (decimal)value;
- }
- /// <summary>
- /// Converts an Int64 to a Decimal.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>A Decimal.</returns>
- public decimal ToDecimal(long value)
- {
- return (decimal)value;
- }
- /// <summary>
- /// Converts a decimal to a Decimal128.
- /// </summary>
- /// <param name="value">A decimal.</param>
- /// <returns>A Decimal128.</returns>
- public Decimal128 ToDecimal128(decimal value)
- {
- if (value == decimal.MaxValue)
- {
- return Decimal128.MaxValue;
- }
- else if (value == decimal.MinValue)
- {
- return Decimal128.MinValue;
- }
- // conversion from decimal to Decimal128 is lossless so need to check for overflow or truncation
- return (Decimal128)value;
- }
- /// <summary>
- /// Converts a Double to a Decimal128.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>A Decimal128.</returns>
- public Decimal128 ToDecimal128(double value)
- {
- if (value == double.MaxValue)
- {
- return Decimal128.MaxValue;
- }
- else if (value == double.MinValue)
- {
- return Decimal128.MinValue;
- }
- else if (double.IsPositiveInfinity(value))
- {
- return Decimal128.PositiveInfinity;
- }
- else if (double.IsNegativeInfinity(value))
- {
- return Decimal128.NegativeInfinity;
- }
- else if (double.IsNaN(value))
- {
- return Decimal128.QNaN;
- }
- var decimal128Value = (Decimal128)value;
- if (!_allowTruncation && value != (double)decimal128Value)
- {
- throw new TruncationException();
- }
- return decimal128Value;
- }
- /// <summary>
- /// Converts an Int32 to a Decimal128.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>A Decimal128.</returns>
- public Decimal128 ToDecimal128(int value)
- {
- return (Decimal128)value;
- }
- /// <summary>
- /// Converts an Int64 to a Decimal128.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>A Decimal128.</returns>
- public Decimal128 ToDecimal128(long value)
- {
- return (Decimal128)value;
- }
- /// <summary>
- /// Converts a UInt64 to a Decimal128.
- /// </summary>
- /// <param name="value">A UInt64.</param>
- /// <returns>A Decimal128.</returns>
- [CLSCompliant(false)]
- public Decimal128 ToDecimal128(ulong value)
- {
- return (Decimal128)value;
- }
- /// <summary>
- /// Converts a Decimal to a Double.
- /// </summary>
- /// <param name="value">A Decimal.</param>
- /// <returns>A Double.</returns>
- public double ToDouble(decimal value)
- {
- if (value == decimal.MinValue)
- {
- return double.MinValue;
- }
- else if (value == decimal.MaxValue)
- {
- return double.MaxValue;
- }
- var doubleValue = (double)value;
- if (value != (decimal)doubleValue)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return doubleValue;
- }
- /// <summary>
- /// Converts a Decimal128 to a Double.
- /// </summary>
- /// <param name="value">A Decimal.</param>
- /// <returns>A Double.</returns>
- public double ToDouble(Decimal128 value)
- {
- if (value == Decimal128.MaxValue)
- {
- return double.MaxValue;
- }
- else if (value == Decimal128.MinValue)
- {
- return double.MinValue;
- }
- else if (Decimal128.IsPositiveInfinity(value))
- {
- return double.PositiveInfinity;
- }
- else if (Decimal128.IsNegativeInfinity(value))
- {
- return double.NegativeInfinity;
- }
- else if (Decimal128.IsNaN(value))
- {
- return double.NaN;
- }
- double doubleValue;
- if (_allowOverflow)
- {
- try { doubleValue = (double)value; } catch (OverflowException) { doubleValue = Decimal128.IsNegative(value) ? double.MinValue : double.MaxValue; }
- }
- else
- {
- doubleValue = (double)value;
- }
- if (!_allowTruncation && value != (Decimal128)doubleValue)
- {
- throw new TruncationException();
- }
- return doubleValue;
- }
- /// <summary>
- /// Converts a Double to a Double.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>A Double.</returns>
- public double ToDouble(double value)
- {
- return value;
- }
- /// <summary>
- /// Converts a Single to a Double.
- /// </summary>
- /// <param name="value">A Single.</param>
- /// <returns>A Double.</returns>
- public double ToDouble(float value)
- {
- if (value == float.MinValue)
- {
- return double.MinValue;
- }
- else if (value == float.MaxValue)
- {
- return double.MaxValue;
- }
- else if (float.IsNegativeInfinity(value))
- {
- return double.NegativeInfinity;
- }
- else if (float.IsPositiveInfinity(value))
- {
- return double.PositiveInfinity;
- }
- else if (float.IsNaN(value))
- {
- return double.NaN;
- }
- return value;
- }
- /// <summary>
- /// Converts an Int32 to a Double.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>A Double.</returns>
- public double ToDouble(int value)
- {
- return value;
- }
- /// <summary>
- /// Converts an Int64 to a Double.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>A Double.</returns>
- public double ToDouble(long value)
- {
- var doubleValue = (double)value;
- if (value != (long)doubleValue)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return doubleValue;
- }
- /// <summary>
- /// Converts an Int16 to a Double.
- /// </summary>
- /// <param name="value">An Int16.</param>
- /// <returns>A Double.</returns>
- public double ToDouble(short value)
- {
- return value;
- }
- /// <summary>
- /// Converts a UInt32 to a Double.
- /// </summary>
- /// <param name="value">A UInt32.</param>
- /// <returns>A Double.</returns>
- [CLSCompliant(false)]
- public double ToDouble(uint value)
- {
- return value;
- }
- /// <summary>
- /// Converts a UInt64 to a Double.
- /// </summary>
- /// <param name="value">A UInt64.</param>
- /// <returns>A Double.</returns>
- [CLSCompliant(false)]
- public double ToDouble(ulong value)
- {
- var doubleValue = (double)value;
- if (value != (ulong)doubleValue)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return doubleValue;
- }
- /// <summary>
- /// Converts a UInt16 to a Double.
- /// </summary>
- /// <param name="value">A UInt16.</param>
- /// <returns>A Double.</returns>
- [CLSCompliant(false)]
- public double ToDouble(ushort value)
- {
- return value;
- }
- /// <summary>
- /// Converts a Decimal128 to an Int16.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>An Int16.</returns>
- public short ToInt16(Decimal128 value)
- {
- short shortValue;
- if (_allowOverflow)
- {
- try { shortValue = (short)value; } catch (OverflowException) { shortValue = Decimal128.IsNegative(value) ? short.MinValue : short.MaxValue; }
- }
- else
- {
- shortValue = (short)value;
- }
- if (!_allowTruncation && value != (Decimal128)shortValue)
- {
- throw new TruncationException();
- }
- return shortValue;
- }
- /// <summary>
- /// Converts a Double to an Int16.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>An Int16.</returns>
- public short ToInt16(double value)
- {
- var int16Value = (short)value;
- if (value < short.MinValue || value > short.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)int16Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return int16Value;
- }
- /// <summary>
- /// Converts an Int32 to an Int16.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>An Int16.</returns>
- public short ToInt16(int value)
- {
- if (value < short.MinValue || value > short.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (short)value;
- }
- /// <summary>
- /// Converts an Int64 to an Int16.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>An Int16.</returns>
- public short ToInt16(long value)
- {
- if (value < short.MinValue || value > short.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (short)value;
- }
- /// <summary>
- /// Converts a Decimal to an Int32.
- /// </summary>
- /// <param name="value">A Decimal.</param>
- /// <returns>An Int32.</returns>
- public int ToInt32(decimal value)
- {
- if (value == decimal.MinValue)
- {
- return int.MinValue;
- }
- else if (value == decimal.MaxValue)
- {
- return int.MaxValue;
- }
- var int32Value = (int)value;
- if (value < int.MinValue || value > int.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (decimal)int32Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return int32Value;
- }
- /// <summary>
- /// Converts a Decimal128 to an Int32.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>An Int32.</returns>
- public int ToInt32(Decimal128 value)
- {
- int intValue;
- if (_allowOverflow)
- {
- try { intValue = (int)value; } catch (OverflowException) { intValue = Decimal128.IsNegative(value) ? int.MinValue : int.MaxValue; }
- }
- else
- {
- intValue = (int)value;
- }
- if (!_allowTruncation && value != (Decimal128)intValue)
- {
- throw new TruncationException();
- }
- return intValue;
- }
- /// <summary>
- /// Converts a Double to an Int32.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>An Int32.</returns>
- public int ToInt32(double value)
- {
- var int32Value = (int)value;
- if (value < int.MinValue || value > int.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)int32Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return int32Value;
- }
- /// <summary>
- /// Converts a Single to an Int32.
- /// </summary>
- /// <param name="value">A Single.</param>
- /// <returns>An Int32.</returns>
- public int ToInt32(float value)
- {
- var int32Value = (int)value;
- if (value < int.MinValue || value > int.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (float)int32Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return int32Value;
- }
- /// <summary>
- /// Converts an Int32 to an Int32.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>An Int32.</returns>
- public int ToInt32(int value)
- {
- return value;
- }
- /// <summary>
- /// Converts an Int64 to an Int32.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>An Int32.</returns>
- public int ToInt32(long value)
- {
- if (value < int.MinValue || value > int.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (int)value;
- }
- /// <summary>
- /// Converts an Int16 to an Int32.
- /// </summary>
- /// <param name="value">An Int16.</param>
- /// <returns>An Int32.</returns>
- public int ToInt32(short value)
- {
- return value;
- }
- /// <summary>
- /// Converts a UInt32 to an Int32.
- /// </summary>
- /// <param name="value">A UInt32.</param>
- /// <returns>An Int32.</returns>
- [CLSCompliant(false)]
- public int ToInt32(uint value)
- {
- if (value > (uint)int.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (int)value;
- }
- /// <summary>
- /// Converts a UInt64 to an Int32.
- /// </summary>
- /// <param name="value">A UInt64.</param>
- /// <returns>An Int32.</returns>
- [CLSCompliant(false)]
- public int ToInt32(ulong value)
- {
- if (value > (ulong)int.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (int)value;
- }
- /// <summary>
- /// Converts a UInt16 to an Int32.
- /// </summary>
- /// <param name="value">A UInt16.</param>
- /// <returns>An Int32.</returns>
- [CLSCompliant(false)]
- public int ToInt32(ushort value)
- {
- return value;
- }
- /// <summary>
- /// Converts a Decimal to an Int64.
- /// </summary>
- /// <param name="value">A Decimal.</param>
- /// <returns>An Int64.</returns>
- public long ToInt64(decimal value)
- {
- if (value == decimal.MinValue)
- {
- return long.MinValue;
- }
- else if (value == decimal.MaxValue)
- {
- return long.MaxValue;
- }
- var int64Value = (long)value;
- if (value < long.MinValue || value > long.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (decimal)int64Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return int64Value;
- }
- /// <summary>
- /// Converts a Decimal128 to an Int64.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>An Int64.</returns>
- public long ToInt64(Decimal128 value)
- {
- long longValue;
- if (_allowOverflow)
- {
- try { longValue = (long)value; } catch (OverflowException) { longValue = Decimal128.IsNegative(value) ? long.MinValue : long.MaxValue; }
- }
- else
- {
- longValue = (long)value;
- }
- if (!_allowTruncation && value != (Decimal128)longValue)
- {
- throw new TruncationException();
- }
- return longValue;
- }
- /// <summary>
- /// Converts a Double to an Int64.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>An Int64.</returns>
- public long ToInt64(double value)
- {
- var int64Value = (long)value;
- if (value < long.MinValue || value > long.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)int64Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return int64Value;
- }
- /// <summary>
- /// Converts a Single to an Int64.
- /// </summary>
- /// <param name="value">A Single.</param>
- /// <returns>An Int64.</returns>
- public long ToInt64(float value)
- {
- var int64Value = (long)value;
- if (value < long.MinValue || value > long.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (float)int64Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return int64Value;
- }
- /// <summary>
- /// Converts an Int32 to an Int64.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>An Int64.</returns>
- public long ToInt64(int value)
- {
- return value;
- }
- /// <summary>
- /// Converts an Int64 to an Int64.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>An Int64.</returns>
- public long ToInt64(long value)
- {
- return value;
- }
- /// <summary>
- /// Converts an Int16 to an Int64.
- /// </summary>
- /// <param name="value">An Int16.</param>
- /// <returns>An Int64.</returns>
- public long ToInt64(short value)
- {
- return value;
- }
- /// <summary>
- /// Converts a UInt32 to an Int64.
- /// </summary>
- /// <param name="value">A UInt32.</param>
- /// <returns>An Int64.</returns>
- [CLSCompliant(false)]
- public long ToInt64(uint value)
- {
- return (long)value;
- }
- /// <summary>
- /// Converts a UInt64 to an Int64.
- /// </summary>
- /// <param name="value">A UInt64.</param>
- /// <returns>An Int64.</returns>
- [CLSCompliant(false)]
- public long ToInt64(ulong value)
- {
- if (value > (ulong)long.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (long)value;
- }
- /// <summary>
- /// Converts a UInt16 to an Int64.
- /// </summary>
- /// <param name="value">A UInt16.</param>
- /// <returns>An Int64.</returns>
- [CLSCompliant(false)]
- public long ToInt64(ushort value)
- {
- return value;
- }
- /// <summary>
- /// Converts a Decimal128 to a Single.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>A Single.</returns>
- public float ToSingle(Decimal128 value)
- {
- if (value == Decimal128.MaxValue)
- {
- return float.MaxValue;
- }
- else if (value == Decimal128.MinValue)
- {
- return float.MinValue;
- }
- else if (Decimal128.IsPositiveInfinity(value))
- {
- return float.PositiveInfinity;
- }
- else if (Decimal128.IsNegativeInfinity(value))
- {
- return float.NegativeInfinity;
- }
- else if (Decimal128.IsNaN(value))
- {
- return float.NaN;
- }
- float floatValue;
- if (_allowOverflow)
- {
- try { floatValue = (float)value; } catch (OverflowException) { floatValue = Decimal128.IsNegative(value) ? float.MinValue : float.MaxValue; }
- }
- else
- {
- floatValue = (float)value;
- }
- if (!_allowTruncation && value != (Decimal128)floatValue)
- {
- throw new TruncationException();
- }
- return floatValue;
- }
- /// <summary>
- /// Converts a Double to a Single.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>A Single.</returns>
- public float ToSingle(double value)
- {
- if (value == double.MinValue)
- {
- return float.MinValue;
- }
- else if (value == double.MaxValue)
- {
- return float.MaxValue;
- }
- else if (double.IsNegativeInfinity(value))
- {
- return float.NegativeInfinity;
- }
- else if (double.IsPositiveInfinity(value))
- {
- return float.PositiveInfinity;
- }
- else if (double.IsNaN(value))
- {
- return float.NaN;
- }
- var floatValue = (float)value;
- if (value < float.MinValue || value > float.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)floatValue)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return floatValue;
- }
- /// <summary>
- /// Converts an Int32 to a Single.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>A Single.</returns>
- public float ToSingle(int value)
- {
- var floatValue = (float)value;
- if (value != (int)floatValue)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return floatValue;
- }
- /// <summary>
- /// Converts an Int64 to a Single.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>A Single.</returns>
- public float ToSingle(long value)
- {
- var floatValue = (float)value;
- if (value != (long)floatValue)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return floatValue;
- }
- /// <summary>
- /// Converts a Decimal128 to a UInt16.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>A UInt16.</returns>
- [CLSCompliant(false)]
- public ushort ToUInt16(Decimal128 value)
- {
- ushort ushortValue;
- if (_allowOverflow)
- {
- try { ushortValue = (ushort)value; } catch (OverflowException) { ushortValue = Decimal128.IsNegative(value) ? ushort.MinValue : ushort.MaxValue; }
- }
- else
- {
- ushortValue = (ushort)value;
- }
- if (!_allowTruncation && value != (Decimal128)ushortValue)
- {
- throw new TruncationException();
- }
- return ushortValue;
- }
- /// <summary>
- /// Converts a Double to a UInt16.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>A UInt16.</returns>
- [CLSCompliant(false)]
- public ushort ToUInt16(double value)
- {
- var uint16Value = (ushort)value;
- if (value < ushort.MinValue || value > ushort.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)uint16Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return uint16Value;
- }
- /// <summary>
- /// Converts an Int32 to a UInt16.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>A UInt16.</returns>
- [CLSCompliant(false)]
- public ushort ToUInt16(int value)
- {
- if (value < ushort.MinValue || value > ushort.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (ushort)value;
- }
- /// <summary>
- /// Converts an Int64 to a UInt16.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>A UInt16.</returns>
- [CLSCompliant(false)]
- public ushort ToUInt16(long value)
- {
- if (value < ushort.MinValue || value > ushort.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (ushort)value;
- }
- /// <summary>
- /// Converts a Decimal128 to a UInt32.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>A UInt32.</returns>
- [CLSCompliant(false)]
- public uint ToUInt32(Decimal128 value)
- {
- uint uintValue;
- if (_allowOverflow)
- {
- try { uintValue = (uint)value; } catch (OverflowException) { uintValue = Decimal128.IsNegative(value) ? uint.MinValue : uint.MaxValue; }
- }
- else
- {
- uintValue = (uint)value;
- }
- if (!_allowTruncation && value != (Decimal128)uintValue)
- {
- throw new TruncationException();
- }
- return uintValue;
- }
- /// <summary>
- /// Converts a Double to a UInt32.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>A UInt32.</returns>
- [CLSCompliant(false)]
- public uint ToUInt32(double value)
- {
- var uint32Value = (uint)value;
- if (value < uint.MinValue || value > uint.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)uint32Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return uint32Value;
- }
- /// <summary>
- /// Converts an Int32 to a UInt32.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>A UInt32.</returns>
- [CLSCompliant(false)]
- public uint ToUInt32(int value)
- {
- if (value < uint.MinValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (uint)value;
- }
- /// <summary>
- /// Converts an Int64 to a UInt32.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>A UInt32.</returns>
- [CLSCompliant(false)]
- public uint ToUInt32(long value)
- {
- if (value < uint.MinValue || value > uint.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (uint)value;
- }
- /// <summary>
- /// Converts a Decimal128 to a UInt64.
- /// </summary>
- /// <param name="value">A Decimal128.</param>
- /// <returns>A UInt64.</returns>
- [CLSCompliant(false)]
- public ulong ToUInt64(Decimal128 value)
- {
- ulong ulongValue;
- if (_allowOverflow)
- {
- try { ulongValue = (ulong)value; } catch (OverflowException) { ulongValue = Decimal128.IsNegative(value) ? ulong.MinValue : ulong.MaxValue; }
- }
- else
- {
- ulongValue = (ulong)value;
- }
- if (!_allowTruncation && value != (Decimal128)ulongValue)
- {
- throw new TruncationException();
- }
- return ulongValue;
- }
- /// <summary>
- /// Converts a Double to a UInt64.
- /// </summary>
- /// <param name="value">A Double.</param>
- /// <returns>A UInt64.</returns>
- [CLSCompliant(false)]
- public ulong ToUInt64(double value)
- {
- var uint64Value = (ulong)value;
- if (value < ulong.MinValue || value > ulong.MaxValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- else if (value != (double)uint64Value)
- {
- if (!_allowTruncation) { throw new TruncationException(); }
- }
- return uint64Value;
- }
- /// <summary>
- /// Converts an Int32 to a UInt64.
- /// </summary>
- /// <param name="value">An Int32.</param>
- /// <returns>A UInt64.</returns>
- [CLSCompliant(false)]
- public ulong ToUInt64(int value)
- {
- if (value < (int)ulong.MinValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (ulong)value;
- }
- /// <summary>
- /// Converts an Int64 to a UInt64.
- /// </summary>
- /// <param name="value">An Int64.</param>
- /// <returns>A UInt64.</returns>
- [CLSCompliant(false)]
- public ulong ToUInt64(long value)
- {
- if (value < (int)ulong.MinValue)
- {
- if (!_allowOverflow) { throw new OverflowException(); }
- }
- return (ulong)value;
- }
- }
- }
|