z3-z3-4.12.6.src.api.dotnet.Symbol.cs Maven / Gradle / Ivy
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
Symbol.cs
Abstract:
Z3 Managed API: Symbols
Author:
Christoph Wintersteiger (cwinter) 2012-03-16
Notes:
--*/
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Microsoft.Z3
{
///
/// Symbols are used to name several term and type constructors.
///
public class Symbol : Z3Object
{
///
/// The kind of the symbol (int or string)
///
protected Z3_symbol_kind Kind
{
get { return (Z3_symbol_kind)Native.Z3_get_symbol_kind(Context.nCtx, NativeObject); }
}
///
/// Indicates whether the symbol is of Int kind
///
public bool IsIntSymbol()
{
return Kind == Z3_symbol_kind.Z3_INT_SYMBOL;
}
///
/// Indicates whether the symbol is of string kind.
///
public bool IsStringSymbol()
{
return Kind == Z3_symbol_kind.Z3_STRING_SYMBOL;
}
///
/// A string representation of the symbol.
///
public override string ToString()
{
if (IsIntSymbol())
return ((IntSymbol)this).Int.ToString();
else if (IsStringSymbol())
return ((StringSymbol)this).String;
else
throw new Z3Exception("Unknown symbol kind encountered");
}
///
/// Equality overloading.
///
public static bool operator ==(Symbol s1, Symbol s2)
{
return Object.ReferenceEquals(s1, s2) ||
(!Object.ReferenceEquals(s1, null) &&
!Object.ReferenceEquals(s2, null) &&
s1.NativeObject == s2.NativeObject);
}
///
/// Equality overloading.
///
public static bool operator !=(Symbol s1, Symbol s2)
{
return !(s1 == s2);
}
///
/// Object comparison.
///
public override bool Equals(object o)
{
Symbol casted = o as Symbol;
if (casted == null) return false;
return this == casted;
}
///
/// The Symbol's hash code.
///
/// A hash code
public override int GetHashCode()
{
if (IsIntSymbol())
return ((IntSymbol)this).Int;
else
return ((StringSymbol)this).String.GetHashCode();
}
#region Internal
///
/// Symbol constructor
///
internal protected Symbol(Context ctx, IntPtr obj) : base(ctx, obj)
{
Debug.Assert(ctx != null);
}
internal static Symbol Create(Context ctx, IntPtr obj)
{
Debug.Assert(ctx != null);
switch ((Z3_symbol_kind)Native.Z3_get_symbol_kind(ctx.nCtx, obj))
{
case Z3_symbol_kind.Z3_INT_SYMBOL: return new IntSymbol(ctx, obj);
case Z3_symbol_kind.Z3_STRING_SYMBOL: return new StringSymbol(ctx, obj);
default:
throw new Z3Exception("Unknown symbol kind encountered");
}
}
#endregion
}
}