PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Otp/Erlang/Object.cs

https://github.com/saleyn/otp.net
C# | 163 lines | 95 code | 14 blank | 54 comment | 0 complexity | 15aafcde75629b3864be6956f755d6b9 MD5 | raw file
  1. /*``The contents of this file are subject to the Erlang Public License,
  2. * Version 1.1, (the "License"); you may not use this file except in
  3. * compliance with the License. You should have received a copy of the
  4. * Erlang Public License along with this software. If not, it can be
  5. * retrieved via the world wide web at http://www.erlang.org/.
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. * the License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. * AB. All Rights Reserved.''
  15. *
  16. * Converted from Java to C# by Vlad Dumitrescu (vlad_Dumitrescu@hotmail.com)
  17. */
  18. namespace Otp.Erlang
  19. {
  20. using System;
  21. public enum TermType
  22. {
  23. Object,
  24. Atom,
  25. Binary,
  26. Boolean,
  27. Byte,
  28. Char,
  29. Double,
  30. Int,
  31. List,
  32. Pid,
  33. Port,
  34. Ref,
  35. String,
  36. Tuple,
  37. Var
  38. }
  39. /*
  40. * Base class of the Erlang data type classes. This class is used to
  41. * represent an arbitrary Erlang term.
  42. **/
  43. [Serializable]
  44. public abstract class Object : Amir_Harel.Cloning.BaseObject
  45. {
  46. public static Erlang.Object Format(
  47. string fmt, params object[] args)
  48. {
  49. int pos = 0, argc = 0;
  50. return Formatter.create(fmt.ToCharArray(), ref pos, ref argc, args);
  51. }
  52. public static T Format<T>(string fmt, params object[] args) where T : Object
  53. {
  54. return Format(fmt, args).Cast<T>();
  55. }
  56. public T Cast<T>() where T: Erlang.Object
  57. {
  58. return (T)this;
  59. }
  60. /*
  61. * Convert the object according to the rules of the Erlang external
  62. * format. This is mainly used for sending Erlang terms in messages,
  63. * however it can also be used for storing terms to disk.
  64. *
  65. * @param buf an output stream to which the encoded term should be
  66. * written.
  67. **/
  68. public abstract void encode(OtpOutputStream buf);
  69. /*
  70. * Read binary data in the Erlang external format, and produce a
  71. * corresponding Erlang data type object. This method is normally
  72. * used when Erlang terms are received in messages, however it
  73. * can also be used for reading terms from disk.
  74. *
  75. * @param buf an input stream containing one or more encoded Erlang
  76. * terms.
  77. *
  78. * @return an object representing one of the Erlang data
  79. * types.
  80. *
  81. * @exception DecodeException if the stream does not
  82. * contain a valid representation of an Erlang term.
  83. **/
  84. public static Object decode(OtpInputStream buf)
  85. {
  86. return buf.read_any();
  87. }
  88. public virtual bool subst(ref Erlang.Object term, Erlang.VarBind binding)
  89. {
  90. return false;
  91. }
  92. public virtual bool match(Erlang.Object pattern, Erlang.VarBind binding)
  93. {
  94. return (pattern is Erlang.Var) ? pattern.match(this, binding) : this.Equals(pattern);
  95. }
  96. /*
  97. * Determine if two Erlang objects are equal. In general, Erlang
  98. * objects are equal if the components they consist of are equal.
  99. *
  100. * @param o the object to compare to.
  101. *
  102. * @return true if the objects are identical.
  103. **/
  104. //public abstract bool Equals(System.Object o);
  105. public virtual System.Object clone()
  106. {
  107. try
  108. {
  109. return base.Clone();
  110. }
  111. catch (System.Exception e)
  112. {
  113. /*cannot happen */
  114. throw new System.ApplicationException(e.ToString());
  115. }
  116. }
  117. public abstract Type Type { get; }
  118. public abstract TermType TermType { get; }
  119. public virtual long longValue() { return this.Cast<Long>().longValue(); }
  120. public virtual int intValue() { return this.Cast<Long>().intValue(); }
  121. public virtual short shortValue() { return this.Cast<Long>().shortValue(); }
  122. public virtual double doubleValue() { return this.Cast<Double>().doubleValue(); }
  123. public virtual string atomValue() { return this.Cast<Atom>().atomValue(); }
  124. public virtual string stringValue() { return this.Cast<String>().stringValue(); }
  125. public virtual char charValue() { return this.Cast<Char>().charValue(); }
  126. public virtual bool boolValue() { return this.Cast<Boolean>().booleanValue(); }
  127. public virtual byte[] binaryValue() { return this.Cast<Binary>().binaryValue(); }
  128. public Pid pidValue() { return this.Cast<Pid>(); }
  129. public Port portValue() { return this.Cast<Port>(); }
  130. public Ref refValue() { return this.Cast<Ref>(); }
  131. public Tuple tupleValue() { return this.Cast<Tuple>(); }
  132. public List listValue() { return this.Cast<List>(); }
  133. public Long AsLong() { return this.Cast<Long>(); }
  134. public Int AsInt() { return this.Cast<Int>(); }
  135. public Short AsShort() { return this.Cast<Short>(); }
  136. public Double AsDouble() { return this.Cast<Double>(); }
  137. public Atom AsAtom() { return this.Cast<Atom>(); }
  138. public String AsString() { return this.Cast<String>(); }
  139. public Char AsChar() { return this.Cast<Char>(); }
  140. public Boolean AsBool() { return this.Cast<Boolean>(); }
  141. public Binary AsBinary() { return this.Cast<Binary>(); }
  142. public Pid AsPid() { return this.Cast<Pid>(); }
  143. public Port AsPort() { return this.Cast<Port>(); }
  144. public Ref AsRef() { return this.Cast<Ref>(); }
  145. public Tuple AsTuple() { return this.Cast<Tuple>(); }
  146. public List AsList() { return this.Cast<List>(); }
  147. public Var AsVar() { return this.Cast<Var>(); }
  148. }
  149. }