//******************************************************************************************************
// BinaryValue.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), 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.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 01/25/2008 - J. Ritchie Carroll
// Initial version of source generated.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
#region [ Contributor License Agreements ]
/**************************************************************************\
Copyright © 2009 - J. Ritchie Carroll
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/
#endregion
using System;
using System.Diagnostics.CodeAnalysis;
namespace GSF
{
///
/// Represents a binary data sample stored as a byte array ordered in the
/// endianness of the OS, but implicitly castable to most common native types.
///
public class NativeBinaryValue : BinaryValueBase
{
#region [ Constructors ]
/// Creates a new binary value, ordered in the endianness of the OS, from the given byte array.
/// The buffer which contains the binary representation of the value.
/// The offset in the buffer where the data starts.
/// The number of data bytes that make up the binary value.
/// is null
///
/// is outside the range of the -or-
/// is less than 0 -or-
/// and do not specify a valid region in the
///
/// This constructor assumes a type code of Empty to represent "undefined".
public NativeBinaryValue(byte[] buffer, int startIndex, int length)
: base(TypeCode.Empty, buffer, startIndex, length)
{
}
/// Creates a new binary value, ordered in the endianness of the OS, from the given byte array.
/// The buffer which contains the binary representation of the value.
/// This constructor assumes a type code of Empty to represent "undefined".
public NativeBinaryValue(byte[] buffer)
: base(TypeCode.Empty, buffer, 0, buffer.Length)
{
}
/// Creates a new binary value, ordered in the endianness of the OS, from the given byte array.
/// The type code of the native value that the binary value represents.
/// The buffer which contains the binary representation of the value.
/// The offset in the buffer where the data starts.
/// The number of data bytes that make up the binary value.
/// is null
///
/// is outside the range of the -or-
/// is less than 0 -or-
/// and do not specify a valid region in the
///
public NativeBinaryValue(TypeCode typeCode, byte[] buffer, int startIndex, int length)
: base(typeCode, buffer, startIndex, length)
{
}
/// Creates a new binary value, ordered in the endianness of the OS, from the given byte array.
/// The type code of the native value that the binary value represents.
/// The buffer which contains the binary representation of the value.
public NativeBinaryValue(TypeCode typeCode, byte[] buffer)
: base(typeCode, buffer, 0, buffer.Length)
{
}
#endregion
#region [ Methods ]
///
/// Returns a that represents the current .
///
/// A that represents the current .
public override string ToString()
{
return ((Double)ConvertToType(TypeCode.Double)).ToString();
}
///
/// Returns a representation of source value converted to specified .
///
/// Desired for destination value.
/// A representation of source value converted to specified .
/// Unable to convert binary value to specified type.
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode")]
public NativeBinaryValue ConvertToType(TypeCode typeCode)
{
switch (this.TypeCode)
{
case TypeCode.Byte:
switch (typeCode)
{
case TypeCode.Byte:
return ToByte();
case TypeCode.Double:
return (Double)ToByte();
case TypeCode.Int16:
return (Int16)ToByte();
case TypeCode.Int32:
return (Int32)ToByte();
case TypeCode.Int64:
return (Int64)ToByte();
case TypeCode.Single:
return (Single)ToByte();
case TypeCode.UInt16:
return (UInt16)ToByte();
case TypeCode.UInt32:
return (UInt32)ToByte();
case TypeCode.UInt64:
return (UInt64)ToByte();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.Int16:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToInt16();
case TypeCode.Double:
return (Double)ToInt16();
case TypeCode.Int16:
return ToInt16();
case TypeCode.Int32:
return (Int32)ToInt16();
case TypeCode.Single:
return (Single)ToInt16();
case TypeCode.Int64:
return (Int64)ToInt16();
case TypeCode.UInt16:
return (UInt16)ToInt16();
case TypeCode.UInt32:
return (UInt32)ToInt16();
case TypeCode.UInt64:
return (UInt64)ToInt16();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.Int32:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToInt32();
case TypeCode.Double:
return (Double)ToInt32();
case TypeCode.Int16:
return (Int16)ToInt32();
case TypeCode.Int32:
return ToInt32();
case TypeCode.Int64:
return (Int64)ToInt32();
case TypeCode.Single:
return (Single)ToInt32();
case TypeCode.UInt16:
return (UInt16)ToInt32();
case TypeCode.UInt32:
return (UInt32)ToInt32();
case TypeCode.UInt64:
return (UInt64)ToInt32();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.Int64:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToInt64();
case TypeCode.Double:
return (Double)ToInt64();
case TypeCode.Int16:
return (Int16)ToInt64();
case TypeCode.Int32:
return (Int32)ToInt64();
case TypeCode.Single:
return (Single)ToInt64();
case TypeCode.Int64:
return ToInt64();
case TypeCode.UInt16:
return (UInt16)ToInt64();
case TypeCode.UInt32:
return (UInt32)ToInt64();
case TypeCode.UInt64:
return (UInt64)ToInt64();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.Single:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToSingle();
case TypeCode.Double:
return (Double)ToSingle();
case TypeCode.Int16:
return (Int16)ToSingle();
case TypeCode.Int32:
return (Int32)ToSingle();
case TypeCode.Int64:
return (Int64)ToSingle();
case TypeCode.Single:
return ToSingle();
case TypeCode.UInt16:
return (UInt16)ToSingle();
case TypeCode.UInt32:
return (UInt32)ToSingle();
case TypeCode.UInt64:
return (UInt64)ToSingle();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.Double:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToDouble();
case TypeCode.Double:
return ToDouble();
case TypeCode.Int16:
return (Int16)ToDouble();
case TypeCode.Int32:
return (Int32)ToDouble();
case TypeCode.Int64:
return (Int64)ToDouble();
case TypeCode.Single:
return (Single)ToDouble();
case TypeCode.UInt16:
return (UInt16)ToDouble();
case TypeCode.UInt32:
return (UInt32)ToDouble();
case TypeCode.UInt64:
return (UInt64)ToDouble();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.UInt16:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToUInt16();
case TypeCode.Double:
return (Double)ToUInt16();
case TypeCode.Int16:
return (Int16)ToUInt16();
case TypeCode.Int32:
return (Int32)ToUInt16();
case TypeCode.Int64:
return (Int64)ToUInt16();
case TypeCode.Single:
return (Single)ToUInt16();
case TypeCode.UInt16:
return ToUInt16();
case TypeCode.UInt32:
return (UInt32)ToUInt16();
case TypeCode.UInt64:
return (UInt64)ToUInt16();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.UInt32:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToUInt32();
case TypeCode.Double:
return (Double)ToUInt32();
case TypeCode.Int16:
return (Int16)ToUInt32();
case TypeCode.Int32:
return (Int32)ToUInt32();
case TypeCode.Int64:
return (Int64)ToUInt32();
case TypeCode.Single:
return (Single)ToUInt32();
case TypeCode.UInt16:
return (UInt16)ToUInt32();
case TypeCode.UInt32:
return ToUInt32();
case TypeCode.UInt64:
return (UInt64)ToUInt32();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
case TypeCode.UInt64:
switch (typeCode)
{
case TypeCode.Byte:
return (Byte)ToUInt64();
case TypeCode.Double:
return (Double)ToUInt64();
case TypeCode.Int16:
return (Int16)ToUInt64();
case TypeCode.Int32:
return (Int32)ToUInt64();
case TypeCode.Int64:
return (Int64)ToUInt64();
case TypeCode.Single:
return (Single)ToUInt64();
case TypeCode.UInt16:
return (UInt16)ToUInt64();
case TypeCode.UInt32:
return (UInt32)ToUInt64();
case TypeCode.UInt64:
return ToUInt64();
default:
throw new InvalidOperationException("Unable to convert binary value to " + typeCode);
}
default:
throw new InvalidOperationException("Unable to convert binary value from " + this.TypeCode);
}
}
#endregion
#region [ Operators ]
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator Byte(NativeBinaryValue value)
{
return value.ToByte();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(Byte value)
{
return new NativeBinaryValue(TypeCode.Byte, new[] { value });
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator Int16(NativeBinaryValue value)
{
return value.ToInt16();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(Int16 value)
{
return new NativeBinaryValue(TypeCode.Int16, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator UInt16(NativeBinaryValue value)
{
return value.ToUInt16();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(UInt16 value)
{
return new NativeBinaryValue(TypeCode.UInt16, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator Int24(NativeBinaryValue value)
{
return value.ToInt24();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(Int24 value)
{
return new NativeBinaryValue(TypeCode.Empty, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator UInt24(NativeBinaryValue value)
{
return value.ToUInt24();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(UInt24 value)
{
return new NativeBinaryValue(TypeCode.Empty, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator Int32(NativeBinaryValue value)
{
return value.ToInt32();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(Int32 value)
{
return new NativeBinaryValue(TypeCode.Int32, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator UInt32(NativeBinaryValue value)
{
return value.ToUInt32();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(UInt32 value)
{
return new NativeBinaryValue(TypeCode.UInt32, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator Int64(NativeBinaryValue value)
{
return value.ToInt64();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(Int64 value)
{
return new NativeBinaryValue(TypeCode.Int64, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator UInt64(NativeBinaryValue value)
{
return value.ToUInt64();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(UInt64 value)
{
return new NativeBinaryValue(TypeCode.UInt64, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator Single(NativeBinaryValue value)
{
return value.ToSingle();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(Single value)
{
return new NativeBinaryValue(TypeCode.Single, s_endianOrder.GetBytes(value));
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator Double(NativeBinaryValue value)
{
return value.ToDouble();
}
///
/// Implicitly converts to .
///
/// to convert to .
/// A representation of .
public static implicit operator NativeBinaryValue(Double value)
{
return new NativeBinaryValue(TypeCode.Double, s_endianOrder.GetBytes(value));
}
#endregion
#region [ Static ]
static NativeBinaryValue()
{
s_endianOrder = NativeEndianOrder.Default;
}
#endregion
}
}