[4de1c7]: / app / resources / LeapSDK / v3_python27 / src / CSharpExtensions.cs

Download this file

106 lines (98 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
/******************************************************************************\
* Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved. *
* Leap Motion proprietary and confidential. Not for distribution. *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
\******************************************************************************/
using System;
namespace Leap
{
/**
* Various C# extensions used by the Leap C# classes.
*
* @since 3.0
*/
public static class CSharpExtensions
{
/**
* Compares whether two floating point numbers are within an epsilon value of each other.
* @since 3.0
*/
public static bool NearlyEquals(this float a, float b, float epsilon = Constants.EPSILON)
{
float absA = Math.Abs(a);
float absB = Math.Abs(b);
float diff = Math.Abs(a - b);
if (a == b)
{ // shortcut, handles infinities
return true;
}
else if (a == 0 || b == 0 || diff < float.MinValue)
{
// a or b is zero or both are extremely close to it
// relative error is less meaningful here
return diff < (epsilon * float.MinValue);
}
else { // use relative error
return diff / (absA + absB) < epsilon;
}
}
/**
* Reports whether this object has the specified method.
* @since 3.0
*/
public static bool HasMethod(this object objectToCheck, string methodName)
{
var type = objectToCheck.GetType();
return type.GetMethod(methodName) != null;
}
/**
* Returns the ordinal index of this enumeration item.
* @since 3.0
*/
public static int indexOf(this Enum enumItem)
{
return Array.IndexOf(Enum.GetValues(enumItem.GetType()), enumItem);
}
/**
* Gets the item at the ordinal position in this enumeration.
* @since 3.0
*/
public static T itemFor<T>(this int ordinal)
{
T[] values = (T[])Enum.GetValues(typeof(T));
return values[ordinal];
}
/**
* Convenience function to consolidate event dispatching boilerplate code.
* @since 3.0
*/
public static void Dispatch<T>(this EventHandler<T> handler,
object sender, T eventArgs) where T : EventArgs
{
if (handler != null) handler(sender, eventArgs);
}
/**
* Convenience function to consolidate event dispatching boilerplate code.
* Events are dispatched on the message queue of a threads' synchronization
* context, if possible.
* @since 3.0
*/
public static void DispatchOnContext<T>(this EventHandler<T> handler, object sender,
System.Threading.SynchronizationContext context,
T eventArgs) where T : EventArgs
{
if (handler != null)
{
if (context != null)
{
System.Threading.SendOrPostCallback evt = (spc_args) => { handler(sender, spc_args as T); };
context.Post(evt, eventArgs);
}
else
handler(sender, eventArgs);
}
}
}
}