/* $Id$
*
* Project: Swicli.Library - Two Way Interface for .NET and MONO to SWI-Prolog
* Author: Douglas R. Miles
* Uwe Lesta (SbsSW.SwiPlCs classes)
* E-mail: logicmoo@gmail.com
* WWW: http://www.logicmoo.com
* Copyright (C): 2008, Uwe Lesta SBS-Softwaresysteme GmbH,
* 2010-2012 LogicMOO Developement
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*********************************************************/
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Security.Permissions;
// Exception implementation
// SecurityPermissionAttribute for GetObjectData
namespace SbsSW.DesignByContract
{
///
/// Design By Contract Checks.
///
/// Each method generates an exception or
/// a trace assertion statement if the contract is broken.
///
///
/// This example shows how to call the Require method.
///
/// public void Test(int x)
/// {
/// try
/// {
/// Check.Require(x > 1, "x must be > 1");
/// }
/// catch (System.Exception ex)
/// {
/// PrologCLR.ConsoleTrace(ex.ToString());
/// }
/// }
///
///
/// You can direct output to a Trace listener. For example, you could insert
///
/// Trace.Cogbot.Clear();
/// Trace.Cogbot.Add(new TextWriterTraceListener(Console.Out));
///
///
/// or direct output to a file or the Event Log.
///
/// (Note: For ASP.NET clients use the Cogbot collection
/// of the Debug, not the Trace, object and, for a Release build, only exception-handling
/// is possible.)
///
///
public sealed class Check
{
#region Interface
///
/// Precondition check - should run regardless of preprocessor directives.
///
public static void Require(bool assertion, string message) {
if (UseExceptions) {
if (!assertion)
throw new PreconditionException(message);
}
else {
Trace.Assert(assertion, "Precondition: " + message);
}
}
///
/// Precondition check - should run regardless of preprocessor directives.
///
public static void Require(bool assertion, string message, Exception inner) {
if (UseExceptions) {
if (!assertion) throw new PreconditionException(message, inner);
}
else {
Trace.Assert(assertion, "Precondition: " + message);
}
}
///
/// Precondition check - should run regardless of preprocessor directives.
///
public static void Require(bool assertion) {
if (UseExceptions) {
if (!assertion) throw new PreconditionException("Precondition failed.");
}
else {
Trace.Assert(assertion, "Precondition failed.");
}
}
///
/// Postcondition check.
///
public static void Ensure(bool assertion, string message) {
if (UseExceptions) {
if (!assertion) throw new PostConditionException(message);
}
else {
Trace.Assert(assertion, "Postcondition: " + message);
}
}
///
/// Postcondition check.
///
public static void Ensure(bool assertion, string message, Exception inner) {
if (UseExceptions) {
if (!assertion) throw new PostConditionException(message, inner);
}
else {
Trace.Assert(assertion, "Postcondition: " + message);
}
}
///
/// Postcondition check.
///
public static void Ensure(bool assertion) {
if (UseExceptions) {
if (!assertion) throw new PostConditionException("Postcondition failed.");
}
else {
Trace.Assert(assertion, "Postcondition failed.");
}
}
///
/// Invariant check.
///
public static void Invariant(bool assertion, string message) {
if (UseExceptions) {
if (!assertion) throw new InvariantException(message);
}
else {
Trace.Assert(assertion, "Invariant: " + message);
}
}
///
/// Invariant check.
///
public static void Invariant(bool assertion, string message, Exception inner) {
if (UseExceptions) {
if (!assertion) throw new InvariantException(message, inner);
}
else {
Trace.Assert(assertion, "Invariant: " + message);
}
}
///
/// Invariant check.
///
public static void Invariant(bool assertion) {
if (UseExceptions) {
if (!assertion) throw new InvariantException("Invariant failed.");
}
else {
Trace.Assert(assertion, "Invariant failed.");
}
}
///
/// Assertion check.
///
public static void Assert(bool assertion, string message) {
if (UseExceptions) {
if (!assertion) throw new AssertionException(message);
}
else {
Trace.Assert(assertion, "Assertion: " + message);
}
}
///
/// Assertion check.
///
public static void Assert(bool assertion, string message, Exception inner) {
if (UseExceptions) {
if (!assertion) throw new AssertionException(message, inner);
}
else {
Trace.Assert(assertion, "Assertion: " + message);
}
}
///
/// Assertion check.
///
public static void Assert(bool assertion) {
if (UseExceptions) {
if (!assertion) throw new AssertionException("Assertion failed.");
}
else {
Trace.Assert(assertion, "Assertion failed.");
}
}
///
/// Set this if you wish to use Trace Assert statements
/// instead of exception handling.
/// (The Check class uses exception handling by default.)
///
public static bool UseAssertions {
get {
return useAssertions;
}
set {
useAssertions = value;
}
}
#endregion // Interface
#region Implementation
// No creation
private Check() { }
///
/// Is exception handling being used?
///
private static bool UseExceptions {
get {
return !useAssertions;
}
}
// Are trace assertion statements being used?
// Default is to use exception handling.
private static bool useAssertions; // init by default with = false;
#endregion // Implementation
} // End Check
#region Exceptions
///
/// Exception raised when a contract is broken.
/// Catch this exception type if you wish to differentiate between
/// any DesignByContract exception and other runtime exceptions.
///
///
[Serializable]
public class DesignByContractException : Exception
{
///
public DesignByContractException() { }
///
public DesignByContractException(string message) : base(message) { }
///
public DesignByContractException(string message, Exception inner) : base(message, inner) { }
#region implementation of ISerializable
// ISerializable Constructor
///
protected DesignByContractException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
if (info == null)
throw new ArgumentNullException("info");
}
// see http://msdnwiki.microsoft.com/en-us/mtpswiki/f1d0010b-14fb-402f-974f-16318f0bc19f.aspx
///
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new ArgumentNullException("info");
base.GetObjectData(info, context);
}
#endregion implementation of ISerializable
}
///
/// Exception raised when a precondition fails.
///
[Serializable]
public class PreconditionException : DesignByContractException
{
///
/// Precondition Exception.
///
public PreconditionException() { }
///
/// Precondition Exception.
///
public PreconditionException(string message) : base(message) { }
///
/// Precondition Exception.
///
public PreconditionException(string message, Exception inner) : base(message, inner) { }
#region implementation of ISerializable
///
protected PreconditionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
///
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
#endregion implementation of ISerializable
}
///
/// Exception raised when a postcondition fails.
///
[Serializable]
public class PostConditionException : DesignByContractException
{
///
/// Postcondition Exception.
///
public PostConditionException() { }
///
/// Postcondition Exception.
///
public PostConditionException(string message) : base(message) { }
///
/// Postcondition Exception.
///
public PostConditionException(string message, Exception inner) : base(message, inner) { }
#region implementation of ISerializable
///
protected PostConditionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
///
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
#endregion implementation of ISerializable
}
///
/// Exception raised when an invariant fails.
///
[Serializable]
public class InvariantException : DesignByContractException
{
///
/// Invariant Exception.
///
public InvariantException() { }
///
/// Invariant Exception.
///
public InvariantException(string message) : base(message) { }
///
/// Invariant Exception.
///
public InvariantException(string message, Exception inner) : base(message, inner) { }
#region implementation of ISerializable
///
protected InvariantException(SerializationInfo info, StreamingContext context) : base(info, context) { }
///
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
#endregion implementation of ISerializable
}
///
/// Exception raised when an assertion fails.
///
[Serializable]
public class AssertionException : DesignByContractException
{
///
/// Assertion Exception.
///
public AssertionException() { }
///
/// Assertion Exception.
///
public AssertionException(string message) : base(message) { }
///
/// Assertion Exception.
///
public AssertionException(string message, Exception inner) : base(message, inner) { }
#region implementation of ISerializable
///
protected AssertionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
///
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
#endregion implementation of ISerializable
}
#endregion // Exception classes
}