// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Globalization;
using System.Runtime.InteropServices;
namespace SharpGen.Runtime
{
///
/// The maximum number of bytes to which a pointer can point. Use for a count that must span the full range of a pointer.
/// Equivalent to the native type size_t.
///
public readonly struct PointerSize : IEquatable
{
private readonly IntPtr _size;
///
/// An empty pointer size initialized to zero.
///
public static readonly PointerSize Zero = new PointerSize(0);
///
/// Initializes a new instance of the struct.
///
/// The size.
public PointerSize(IntPtr size)
{
_size = size;
}
///
/// Default constructor.
///
/// value to set
private unsafe PointerSize(void* size)
{
_size = new IntPtr(size);
}
///
/// Default constructor.
///
/// value to set
public PointerSize(int size)
{
_size = new IntPtr(size);
}
///
/// Default constructor.
///
/// value to set
public PointerSize(long size)
{
_size = new IntPtr(size);
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "{0}", _size);
}
///
/// Returns a that represents this instance.
///
/// The format.
///
/// A that represents this instance.
///
public string ToString(string format)
{
if (format == null)
return ToString();
return string.Format(CultureInfo.CurrentCulture, "{0}", _size.ToString(format));
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override int GetHashCode()
{
return _size.GetHashCode();
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public bool Equals(PointerSize other)
{
return _size.Equals(other._size);
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override bool Equals(object value)
{
if(ReferenceEquals(null, value)) return false;
return value is PointerSize && Equals((PointerSize)value);
}
///
/// Adds two sizes.
///
/// The first size to add.
/// The second size to add.
/// The sum of the two sizes.
public static PointerSize operator +(PointerSize left, PointerSize right)
{
return new PointerSize(left._size.ToInt64() + right._size.ToInt64());
}
///
/// Assert a size (return it unchanged).
///
/// The size to assert (unchanged).
/// The asserted (unchanged) size.
public static PointerSize operator +(PointerSize value)
{
return value;
}
///
/// Subtracts two sizes.
///
/// The first size to subtract.
/// The second size to subtract.
/// The difference of the two sizes.
public static PointerSize operator -(PointerSize left, PointerSize right)
{
return new PointerSize(left._size.ToInt64() - right._size.ToInt64());
}
///
/// Reverses the direction of a given size.
///
/// The size to negate.
/// A size facing in the opposite direction.
public static PointerSize operator -(PointerSize value)
{
return new PointerSize(-value._size.ToInt64());
}
///
/// Scales a size by the given value.
///
/// The size to scale.
/// The amount by which to scale the size.
/// The scaled size.
public static PointerSize operator *(int scale, PointerSize value)
{
return new PointerSize(scale*value._size.ToInt64());
}
///
/// Scales a size by the given value.
///
/// The size to scale.
/// The amount by which to scale the size.
/// The scaled size.
public static PointerSize operator *(PointerSize value, int scale)
{
return new PointerSize(scale*value._size.ToInt64());
}
///
/// Scales a size by the given value.
///
/// The size to scale.
/// The amount by which to scale the size.
/// The scaled size.
public static PointerSize operator /(PointerSize value, int scale)
{
return new PointerSize(value._size.ToInt64()/scale);
}
///
/// Tests for equality between two objects.
///
/// The first value to compare.
/// The second value to compare.
/// true if has the same value as ; otherwise, false.
public static bool operator ==(PointerSize left, PointerSize right)
{
return left.Equals(right);
}
///
/// Tests for inequality between two objects.
///
/// The first value to compare.
/// The second value to compare.
/// true if has a different value than ; otherwise, false.
public static bool operator !=(PointerSize left, PointerSize right)
{
return !left.Equals(right);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator int(PointerSize value)
{
return value._size.ToInt32();
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator long(PointerSize value)
{
return value._size.ToInt64();
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator PointerSize(int value)
{
return new PointerSize(value);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator PointerSize(long value)
{
return new PointerSize(value);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator PointerSize(IntPtr value)
{
return new PointerSize(value);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator IntPtr(PointerSize value)
{
return value._size;
}
///
/// Performs an implicit conversion from void* to .
///
/// The value.
/// The result of the conversion.
public static unsafe implicit operator PointerSize(void* value)
{
return new PointerSize(value);
}
///
/// Performs an implicit conversion from to void*.
///
/// The value.
/// The result of the conversion.
public static unsafe implicit operator void*(PointerSize value)
{
return (void*) value._size;
}
}
}