PageRenderTime 53ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/dotnet/fan/sys/FanObj.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 198 lines | 149 code | 28 blank | 21 comment | 26 complexity | 343d89ddd0dc0ef2b2c35e896d7195c2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 14 Sep 06 Andy Frank Creation
  7. //
  8. using System;
  9. using Fanx.Util;
  10. namespace Fan.Sys
  11. {
  12. ///
  13. /// FanObj is the root class of all classes in Fantom - it is the class
  14. /// representation of Obj which manifests itself as both an interface
  15. /// called Obj and this class.
  16. ///
  17. public class FanObj
  18. {
  19. //////////////////////////////////////////////////////////////////////////
  20. // .NET
  21. //////////////////////////////////////////////////////////////////////////
  22. public override int GetHashCode()
  23. {
  24. long h = hash();
  25. return (int)(h ^ (h >> 32));
  26. }
  27. public override string ToString()
  28. {
  29. return toStr();
  30. }
  31. //////////////////////////////////////////////////////////////////////////
  32. // Identity
  33. //////////////////////////////////////////////////////////////////////////
  34. public static bool equals(object self, object x)
  35. {
  36. return self.Equals(x);
  37. }
  38. public static long compare(object self, object x)
  39. {
  40. if (self is FanObj)
  41. return ((FanObj)self).compare(x);
  42. else if (self is string)
  43. return FanStr.compare((string)self, x);
  44. else if (self is IComparable)
  45. return ((IComparable)self).CompareTo(x);
  46. else
  47. return FanStr.compare(toStr(self), toStr(x));
  48. }
  49. public virtual long compare(object obj)
  50. {
  51. return FanStr.compare(toStr(), toStr(obj));
  52. }
  53. public static long hash(object self)
  54. {
  55. if (self is FanObj)
  56. return ((FanObj)self).hash();
  57. else
  58. return self.GetHashCode();
  59. }
  60. public virtual long hash()
  61. {
  62. return base.GetHashCode();
  63. }
  64. public static string toStr(object self)
  65. {
  66. if (self is FanObj)
  67. return ((FanObj)self).toStr();
  68. else
  69. return self.ToString();
  70. }
  71. public virtual string toStr()
  72. {
  73. return base.ToString();
  74. }
  75. public static bool isImmutable(object self)
  76. {
  77. if (self is FanObj)
  78. return ((FanObj)self).isImmutable();
  79. else if (self == null)
  80. return true;
  81. else
  82. return FanUtil.isDotnetImmutable(self.GetType());
  83. }
  84. public virtual bool isImmutable()
  85. {
  86. return @typeof().isConst();
  87. }
  88. public static object toImmutable(object self)
  89. {
  90. if (self is FanObj)
  91. return ((FanObj)self).toImmutable();
  92. else if (FanUtil.isDotnetImmutable(self.GetType()))
  93. return self;
  94. throw NotImmutableErr.make(self.GetType().ToString()).val;
  95. }
  96. public virtual object toImmutable()
  97. {
  98. if (@typeof().isConst()) return this;
  99. throw NotImmutableErr.make(@typeof().ToString()).val;
  100. }
  101. public static Type @typeof(object self)
  102. {
  103. if (self is FanObj)
  104. return ((FanObj)self).@typeof();
  105. else
  106. return FanUtil.toFanType(self.GetType(), true);
  107. }
  108. public virtual Type @typeof()
  109. {
  110. return Sys.ObjType;
  111. }
  112. public static object with(object self, Func f)
  113. {
  114. if (self is FanObj)
  115. {
  116. return ((FanObj)self).with(f);
  117. }
  118. else
  119. {
  120. f.call(self);
  121. return self;
  122. }
  123. }
  124. public virtual object with(Func f) { f.call(this); return this; }
  125. public static object trap(object self, string name) { return trap(self, name, null); }
  126. public static object trap(object self, string name, List args)
  127. {
  128. if (self is FanObj)
  129. return ((FanObj)self).trap(name, args);
  130. else
  131. return doTrap(self, name, args, @typeof(self));
  132. }
  133. public virtual object trap(string name) { return doTrap(this, name, null, @typeof()); }
  134. public virtual object trap(string name, List args) { return doTrap(this, name, args, @typeof()); }
  135. private static object doTrap(object self, string name, List args, Type type)
  136. {
  137. Slot slot = type.slot(name, true);
  138. if (slot is Method)
  139. {
  140. Method m = (Method)slot;
  141. return m.m_func.callOn(self, args);
  142. }
  143. else
  144. {
  145. Field f = (Field)slot;
  146. int argSize = (args == null) ? 0 : args.sz();
  147. if (argSize == 0)
  148. {
  149. return FanUtil.box(f.get(self));
  150. }
  151. if (argSize == 1)
  152. {
  153. object val = args.get(0);
  154. f.set(self, val);
  155. return FanUtil.box(val);
  156. }
  157. throw ArgErr.make("Invalid number of args to get or set field '" + name + "'").val;
  158. }
  159. }
  160. //////////////////////////////////////////////////////////////////////////
  161. // Utils
  162. //////////////////////////////////////////////////////////////////////////
  163. public static void echo() { echo(""); }
  164. public static void echo(object obj)
  165. {
  166. if (obj == null) obj = "null";
  167. System.Console.WriteLine(toStr(obj));
  168. }
  169. }
  170. }