[]
        
(Showing Draft Content)

GrapeCity.Documents.DX.DXUtil

Class DXUtil

Namespace
GrapeCity.Documents.DX
Assembly
GcDocs.DX.Windows.dll

Utility class.

public static class DXUtil
Inheritance
object
DXUtil
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()

Fields

ToDegrees

The multiplier for conversion from radians to degrees.

public const float ToDegrees = 57.29578

Field Value

float

ToRadians

The multiplier for conversion from degrees to radians.

public const float ToRadians = 0.017453292

Field Value

float

ZeroTolerance

The value for which all absolute numbers smaller than are considered equal to zero.

public const float ZeroTolerance = 1E-06

Field Value

float

Methods

AllocateClearedMemory(int, byte, int)

Allocate an aligned memory buffer and clear it with a specified value (0 by default).

public static IntPtr AllocateClearedMemory(int sizeInBytes, byte clearValue = 0, int align = 16)

Parameters

sizeInBytes int

Size of the buffer to allocate.

clearValue byte

Default value used to clear the buffer.

align int

Alignment, 16 bytes by default.

Returns

System.IntPtr

A pointer to a buffer aligned.

Remarks

To free this buffer, call FreeMemory(IntPtr).

AllocateMemory(int, int)

Allocate an aligned memory buffer.

public static IntPtr AllocateMemory(int sizeInBytes, int align = 16)

Parameters

sizeInBytes int

Size of the buffer to allocate.

align int

Alignment, 16 bytes by default.

Returns

System.IntPtr

A pointer to a buffer aligned.

Remarks

To free this buffer, call FreeMemory(IntPtr).

Any<T>(IEnumerable<T>)

Test if there is an element in this enumeration.

public static bool Any<T>(IEnumerable<T> source)

Parameters

source System.Collections.Generic.IEnumerable<T><T>

The enumerable source.

Returns

bool

true if there is an element in this enumeration, false otherwise

Type Parameters

T

Type of the element

Array<T>(T, int)

Creates an Array of count elements with the given value.

public static T[] Array<T>(T value, int count)

Parameters

value T
count int

Returns

T[]

Type Parameters

T

BuildPropertyGetter<T>(Type, PropertyInfo)

Builds a fast property getter from a type and a property info.

public static GetValueFastDelegate<T> BuildPropertyGetter<T>(Type customEffectType, PropertyInfo propertyInfo)

Parameters

customEffectType System.Type

Type of the custom effect.

propertyInfo System.Reflection.PropertyInfo

The property info to get the value from.

Returns

GetValueFastDelegate<T>

A compiled delegate.

Type Parameters

T

Type of the getter.

BuildPropertySetter<T>(Type, PropertyInfo)

Builds a fast property setter from a type and a property info.

public static SetValueFastDelegate<T> BuildPropertySetter<T>(Type customEffectType, PropertyInfo propertyInfo)

Parameters

customEffectType System.Type

Type of the custom effect.

propertyInfo System.Reflection.PropertyInfo

The property info to set the value to.

Returns

SetValueFastDelegate<T>

A compiled delegate.

Type Parameters

T

Type of the setter.

Clamp(int, int, int)

Clamps the specified value.

public static int Clamp(int value, int min, int max)

Parameters

value int

The value.

min int

The min.

max int

The max.

Returns

int

The result of clamping a value between min and max

Clamp(float, float, float)

Clamps the specified value.

public static float Clamp(float value, float min, float max)

Parameters

value float

The value.

min float

The min.

max float

The max.

Returns

float

The result of clamping a value between min and max

ClearMemory(IntPtr, byte, int)

Clears the memory.

public static void ClearMemory(IntPtr dest, byte value, int sizeInBytesToClear)

Parameters

dest System.IntPtr

The dest.

value byte

The value.

sizeInBytesToClear int

The size in bytes to clear.

Compare(IEnumerator, IEnumerator)

Compares two collection, element by elements.

public static bool Compare(IEnumerator leftIt, IEnumerator rightIt)

Parameters

leftIt System.Collections.IEnumerator

A "from" enumerator.

rightIt System.Collections.IEnumerator

A "to" enumerator.

Returns

bool

true if lists are identical; otherwise, false.

CompareMemory(IntPtr, IntPtr, int)

Compares two block of memory.

public static bool CompareMemory(IntPtr from, IntPtr against, int sizeToCompare)

Parameters

from System.IntPtr

The pointer to compare from.

against System.IntPtr

The pointer to compare against.

sizeToCompare int

The size in bytes to compare.

Returns

bool

true if the buffers are equivalent; otherwise, false.

ConvertToBoolArray(Bool[])

Converts Bool array to bool array.

public static bool[] ConvertToBoolArray(Bool[] array)

Parameters

array Bool[]

The array.

Returns

bool[]

Converted array of bool.

CopyMemory(IntPtr, IntPtr, int)

Native memcpy.

public static void CopyMemory(IntPtr dest, IntPtr src, int sizeInBytesToCopy)

Parameters

dest System.IntPtr

The destination memory location.

src System.IntPtr

The source memory location.

sizeInBytesToCopy int

The byte count.

Dispose<T>(ref T)

Safely dispose a referencem if not null, and set it to null after dispose.

public static void Dispose<T>(ref T comObject) where T : class, IDisposable

Parameters

comObject T

Object to dispose.

Type Parameters

T

The type of COM interface to dispose.

Remarks

The reference will be set to null after dispose.

Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)

Selects distinct elements from an enumeration.

public static IEnumerable<TSource> Distinct<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer = null)

Parameters

source System.Collections.Generic.IEnumerable<T><TSource>

The source.

comparer System.Collections.Generic.IEqualityComparer<T><TSource>

The comparer.

Returns

System.Collections.Generic.IEnumerable<T><TSource>

A enumeration of selected values

Type Parameters

TSource

The type of the T source.

FreeDisposedShadowPointers()

Releases native pointers to already disposed shadow objects.

public static void FreeDisposedShadowPointers()

Remarks

Call this method when all unmanaged objects using shadows are destroyed.

FreeMemory(IntPtr)

Allocate an aligned memory buffer.

public static void FreeMemory(IntPtr alignedBuffer)

Parameters

alignedBuffer System.IntPtr

Remarks

The buffer must have been allocated with AllocateMemory(int, int).

GetCustomAttribute<T>(MemberInfo, bool)

Gets the custom attribute.

public static T GetCustomAttribute<T>(MemberInfo memberInfo, bool inherited = false) where T : Attribute

Parameters

memberInfo System.Reflection.MemberInfo

The member info.

inherited bool

if set to true [inherited].

Returns

T

The custom attribute or null if not found.

Type Parameters

T

Type of the custom attribute.

GetCustomAttributes<T>(MemberInfo, bool)

Gets the custom attributes.

public static IEnumerable<T> GetCustomAttributes<T>(MemberInfo memberInfo, bool inherited = false) where T : Attribute

Parameters

memberInfo System.Reflection.MemberInfo

The member info.

inherited bool

if set to true [inherited].

Returns

System.Collections.Generic.IEnumerable<T><T>

The custom attribute or null if not found.

Type Parameters

T

Type of the custom attribute.

GetGuidFromType(Type)

Gets the System.Guid from a type.

public static Guid GetGuidFromType(Type type)

Parameters

type System.Type

The type.

Returns

System.Guid

The guid associated with this type.

IsAssignableFrom(Type, Type)

Determines whether fromType can be assigned to toType.

public static bool IsAssignableFrom(Type toType, Type fromType)

Parameters

toType System.Type

To type.

fromType System.Type

From type.

Returns

bool

true if [is assignable from] [the specified to type]; otherwise, false.

IsEnum(Type)

Determines whether the specified type to test is an enum.

public static bool IsEnum(Type typeToTest)

Parameters

typeToTest System.Type

The type to test.

Returns

bool

true if the specified type to test is an enum; otherwise, false.

IsOne(float)

Determines whether the specified value is close to one (1.0f).

public static bool IsOne(float a)

Parameters

a float

The floating value.

Returns

bool

true if the specified value is close to one (1.0f); otherwise, false.

IsValueType(Type)

Determines whether the specified type to test is a value type.

public static bool IsValueType(Type typeToTest)

Parameters

typeToTest System.Type

The type to test.

Returns

bool

true if the specified type to test is a value type; otherwise, false.

IsZero(float)

Determines whether the specified value is close to zero (0.0f).

public static bool IsZero(float a)

Parameters

a float

The floating value.

Returns

bool

true if the specified value is close to zero (0.0f); otherwise, false.

Join(string, IEnumerator)

String helper join method to display an enumrable of object as a single string.

public static string Join(string separator, IEnumerator elements)

Parameters

separator string

The separator.

elements System.Collections.IEnumerator

The enumerable.

Returns

string

A string with array elements serparated by the seperator.

Lerp(double, double, double)

Interpolates between two values using a linear function by a given amount.

public static double Lerp(double from, double to, double amount)

Parameters

from double

Value to interpolate from.

to double

Value to interpolate to.

amount double

Interpolation amount.

Returns

double

The result of linear interpolation of values based on the amount.

Remarks

Lerp(float, float, float)

Interpolates between two values using a linear function by a given amount.

public static float Lerp(float from, float to, float amount)

Parameters

from float

Value to interpolate from.

to float

Value to interpolate to.

amount float

Interpolation amount.

Returns

float

The result of linear interpolation of values based on the amount.

Remarks

NearEqual(float, float)

Checks if a and b are almost equals, taking into account the magnitude of floating point numbers. See Remarks. See remarks.

public static bool NearEqual(float a, float b)

Parameters

a float

The left value to compare.

b float

The right value to compare.

Returns

bool

true if a almost equal to b, false otherwise

Remarks

The code is using the technique described by Bruce Dawson in Comparing Floating point numbers 2012 edition.

Pin<T>(T[], Action<IntPtr>)

Pins the specified source and call an action with the pinned pointer.

public static void Pin<T>(T[] source, Action<IntPtr> pinAction) where T : struct

Parameters

source T[]

The source array.

pinAction System.Action<T><System.IntPtr>

The pin action to perform on the pinned pointer.

Type Parameters

T

The type of the structure to pin.

Pin<T>(ref T, Action<IntPtr>)

Pins the specified source and call an action with the pinned pointer.

public static void Pin<T>(ref T source, Action<IntPtr> pinAction) where T : struct

Parameters

source T

The source.

pinAction System.Action<T><System.IntPtr>

The pin action to perform on the pinned pointer.

Type Parameters

T

The type of the structure to pin.

PtrToStringUni(IntPtr, int)

Converts a pointer to a null-terminating string up to maxLength characters to a .Net string.

public static string PtrToStringUni(IntPtr pointer, int maxLength)

Parameters

pointer System.IntPtr

The pointer to an Unicode null string.

maxLength int

Maximum length of the string.

Returns

string

The converted string.

Read<T>(IntPtr, T[], int, int)

Reads the specified array T[] data from a memory location.

public static IntPtr Read<T>(IntPtr source, T[] data, int offset, int count) where T : struct

Parameters

source System.IntPtr

Memory location to read from.

data T[]

The data write to.

offset int

The offset in the array to write to.

count int

The number of T element to read from the memory location.

Returns

System.IntPtr

source pointer + sizeof(T) * count.

Type Parameters

T

Type of a data to read.

Read<T>(IntPtr, ref T)

Reads the specified T data from a memory location.

public static void Read<T>(IntPtr source, ref T data) where T : struct

Parameters

source System.IntPtr

Memory location to read from.

data T

The data write to.

Type Parameters

T

Type of a data to read.

Read<T>(IntPtr)

Reads the specified T data from a memory location.

public static T Read<T>(IntPtr source) where T : struct

Parameters

source System.IntPtr

Memory location to read from.

Returns

T

The data read from the memory location.

Type Parameters

T

Type of a data to read.

ReadAndPosition<T>(IntPtr, ref T)

Reads the specified T data from a memory location.

public static IntPtr ReadAndPosition<T>(IntPtr source, ref T data) where T : struct

Parameters

source System.IntPtr

Memory location to read from.

data T

The data write to.

Returns

System.IntPtr

source pointer + sizeof(T).

Type Parameters

T

Type of a data to read.

ReadOut<T>(IntPtr, out T)

Reads the specified T data from a memory location.

public static void ReadOut<T>(IntPtr source, out T data) where T : struct

Parameters

source System.IntPtr

Memory location to read from.

data T

The data write to.

Type Parameters

T

Type of a data to read.

ReadStream(Stream, ref int)

Read stream to a byte[] buffer.

public static byte[] ReadStream(Stream stream, ref int readLength)

Parameters

stream System.IO.Stream

Input stream.

readLength int

Length to read.

Returns

byte[]

A byte[] buffer.

SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TResult>>)

Select elements from an enumeration.

public static IEnumerable<TResult> SelectMany<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)

Parameters

source System.Collections.Generic.IEnumerable<T><TSource>

The source.

selector System.Func<T, TResult><TSource, System.Collections.Generic.IEnumerable<T><TResult>>

The selector.

Returns

System.Collections.Generic.IEnumerable<T><TResult>

A enumeration of selected values

Type Parameters

TSource

The type of the T source.

TResult

The type of the T result.

SizeOf<T>()

Return the sizeof a struct from a CLR. Equivalent to sizeof operator but works on generics too.

public static int SizeOf<T>() where T : struct

Returns

int

Size of this struct.

Type Parameters

T

A struct to evaluate.

SizeOf<T>(T[])

Return the sizeof an array of struct. Equivalent to sizeof operator but works on generics too.

public static int SizeOf<T>(T[] array) where T : struct

Parameters

array T[]

The array of struct to evaluate.

Returns

int

Size in bytes of this array of struct.

Type Parameters

T

A struct.

Sleep(TimeSpan)

Suspends the current thread.

public static void Sleep(TimeSpan sleepTimeInMillis)

Parameters

sleepTimeInMillis System.TimeSpan

The duration to sleep in milliseconds.

StringToHGlobalAnsi(string)

Copies the contents of a managed String into unmanaged memory, converting into ANSI format as it copies.

public static IntPtr StringToHGlobalAnsi(string s)

Parameters

s string

A managed string to be copied.

Returns

System.IntPtr

The address, in unmanaged memory, to where s was copied, or IntPtr.Zero if s is null.

StringToHGlobalUni(string)

Copies the contents of a managed String into unmanaged memory.

public static IntPtr StringToHGlobalUni(string s)

Parameters

s string

A managed string to be copied.

Returns

System.IntPtr

The address, in unmanaged memory, to where s was copied, or IntPtr.Zero if s is null.

ToArray<T>(IEnumerable<T>)

Transforms an System.Collections.Generic.IEnumerable<T> to an array of T.

public static T[] ToArray<T>(IEnumerable<T> source)

Parameters

source System.Collections.Generic.IEnumerable<T><T>

The enumerable source.

Returns

T[]

an array of T

Type Parameters

T

Type of the element

ToByteArray<T>(T[])

Converts a structured array to an equivalent byte array.

public static byte[] ToByteArray<T>(T[] source) where T : struct

Parameters

source T[]

The source array.

Returns

byte[]

Converted byte array.

Type Parameters

T

The type of source array.

WithinEpsilon(float, float, float)

Checks if a - b are almost equals within a float epsilon.

public static bool WithinEpsilon(float a, float b, float epsilon)

Parameters

a float

The left value to compare.

b float

The right value to compare.

epsilon float

Epsilon value

Returns

bool

true if a almost equal to b within a float epsilon, false otherwise

Write<T>(IntPtr, T[], int, int)

Writes the specified array T[] data to a memory location.

public static IntPtr Write<T>(IntPtr destination, T[] data, int offset, int count) where T : struct

Parameters

destination System.IntPtr

Memory location to write to.

data T[]

The array of T data to write.

offset int

The offset in the array to read from.

count int

The number of T element to write to the memory location.

Returns

System.IntPtr

destination pointer + sizeof(T) * count.

Type Parameters

T

Type of a data to write.

Write<T>(IntPtr, ref T)

Writes the specified T data to a memory location.

public static void Write<T>(IntPtr destination, ref T data) where T : struct

Parameters

destination System.IntPtr

Memory location to write to.

data T

The data to write.

Type Parameters

T

Type of a data to write.

WriteAndPosition<T>(IntPtr, ref T)

Writes the specified T data to a memory location.

public static IntPtr WriteAndPosition<T>(IntPtr destination, ref T data) where T : struct

Parameters

destination System.IntPtr

Memory location to write to.

data T

The data to write.

Returns

System.IntPtr

destination pointer + sizeof(T).

Type Parameters

T

Type of a data to write.