/* The MIT License Copyright (c) 2007-2008 Kevin Moore (http://j832.com) 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.Threading; using System.Diagnostics; using System.Runtime.InteropServices; namespace J832.Common { /// /// Contains general helper methods. /// public static class Util { #region Methods to verify conditions (Require) /// /// If is false, throw an empty . /// /// The 'truth' to evaluate. [DebuggerStepThrough] public static void Require(bool truth) { if (!truth) { throw new InvalidOperationException(string.Empty); } } /// /// If is false, throw an /// with the provided . /// /// The 'truth' to evaluate. /// /// The if /// is false. /// [DebuggerStepThrough] public static void Require(bool truth, string message) { RequireNotNullOrEmpty(message, "message"); if (!truth) { throw new InvalidOperationException(message); } } /// /// If is false, throws /// . /// /// The 'truth' to evaluate. /// /// The to throw if is false. /// [DebuggerStepThrough] public static void Require(bool truth, Exception exception) { RequireNotNull(exception, "exception"); if (!truth) { throw exception; } } /// /// Throws an if the /// provided string is null. /// Throws an if the /// provided string is empty. /// /// The object to test for null and empty. /// The string for the ArgumentException parameter, if thrown. [DebuggerStepThrough] public static void RequireNotNullOrEmpty(string stringParameter, string parameterName) { if (stringParameter == null) { throw new ArgumentNullException(parameterName); } else if (stringParameter.Length == 0) { throw new ArgumentOutOfRangeException(parameterName); } } /// /// Throws an if the /// provided object is null. /// /// The object to test for null. /// The string for the ArgumentNullException parameter, if thrown. [DebuggerStepThrough] public static void RequireNotNull(object obj, string parameterName) { if (obj == null) { throw new ArgumentNullException(parameterName); } } /// /// Throws an if the provided truth is false. /// /// The value assumed to be true. /// The string for , if thrown. [DebuggerStepThrough] public static void RequireArgument(bool truth, string parameterName) { Util.RequireNotNullOrEmpty(parameterName, "parameterName"); if (!truth) { throw new ArgumentException(parameterName); } } /// /// Throws an if the provided truth is false. /// /// The value assumed to be true. /// The paramName for the , if thrown. /// The message for , if thrown. [DebuggerStepThrough] public static void RequireArgument(bool truth, string paramName, string message) { Util.RequireNotNullOrEmpty(paramName, "paramName"); Util.RequireNotNullOrEmpty(message, "message"); if (!truth) { throw new ArgumentException(message, paramName); } } /// /// Throws an if the provided truth is false. /// /// The value assumed to be true. /// The string for , if thrown. [DebuggerStepThrough] public static void RequireArgumentRange(bool truth, string parameterName) { Util.RequireNotNullOrEmpty(parameterName, "parameterName"); if (!truth) { throw new ArgumentOutOfRangeException(parameterName); } } /// /// Throws an if the provided truth is false. /// /// The value assumed to be true. /// The paramName for the , if thrown. /// The message for , if thrown. [DebuggerStepThrough] public static void RequireArgumentRange(bool truth, string paramName, string message) { Util.RequireNotNullOrEmpty(paramName, "paramName"); Util.RequireNotNullOrEmpty(message, "message"); if (!truth) { throw new ArgumentOutOfRangeException(message, paramName); } } #endregion /// /// Wraps /// for atomically setting null fields. /// /// The type of the field to set. /// /// The field that, if null, will be set to . /// /// /// If is null, the object to set it to. /// /// true if was null and has now been set; otherwise, false. public static bool InterlockedSetIfNotNull(ref T location, T value) where T : class { Util.RequireNotNull(value, "value"); // Strictly speaking, this null check is not nessesary, but // while CompareExchange is fast, it's still much slower than a // null check. if (location == null) { // This is a paranoid method. In a multi-threaded environment, it's possible // for two threads to get through the null check before a value is set. // This makes sure than one and only one value is set to field. // This is super important if the field is used in locking, for instance. T valueWhenSet = Interlocked.CompareExchange(ref location, value, null); return (valueWhenSet == null); } else { return false; } } /// /// Returns true if the provided is considered 'critical' /// /// The to evaluate for critical-ness. /// true if the Exception is conisdered critical; otherwise, false. /// /// These exceptions are consider critical: /// /// /// /// /// /// /// public static bool IsCriticalException(Exception exception) { Util.RequireNotNull(exception, "exception"); // Copied with respect from WPF WindowsBase->MS.Internal.CriticalExceptions.IsCriticalException // NullReferencException, SecurityException --> not going to consider these critical return exception is OutOfMemoryException || exception is StackOverflowException || exception is ThreadAbortException || exception is SEHException; } //*** static IsCriticalException } //*** public class Util } //*** namespace J832.Common