logo

const abstract class

sys::Enum

sys::Obj
  sys::Enum
  1  //
  2  // Copyright (c) 2006, Brian Frank and Andy Frank
  3  // Licensed under the Academic Free License version 3.0
  4  //
  5  // History:
  6  //   16 Apr 06  Brian Frank  Creation
  7  //
  8  
  9  **
 10  ** Enum is the base class for enum classes defined using the 'enum'
 11  ** keyword.  An enum models a fixed range of discrete values.  Each
 12  ** value has an Int ordinal and a Str name.
 13  **
 14  ** Every enum class implicitly has the following slots auto-generated
 15  ** by the compiler:
 16  **   - a static const field for each name in the enum's range.
 17  **   - a static field called "values" which contains the list of
 18  **     discrete values indexed by ordinal.
 19  **   - a static method called "fromStr" which maps an enum name
 20  **     to an enum instance
 21  **
 22  ** See `docLang::Enums` for details.
 23  **
 24  abstract const class Enum
 25  {
 26  
 27  //////////////////////////////////////////////////////////////////////////
 28  // Constructor
 29  //////////////////////////////////////////////////////////////////////////
 30  
 31    **
 32    ** Protected constructor - for compiler use only.
 33    **
 34    protected new make(Int ordinal, Str name)
 35  
 36    **
 37    ** Protected fromStr implementation - for compiler use only.
 38    ** A public static fromStr method is always auto-generated
 39    ** by the compiler for each enum.
 40    **
 41    protected static Enum doFromStr(Type t, Str name, Bool checked)
 42  
 43  //////////////////////////////////////////////////////////////////////////
 44  // Obj Overrides
 45  //////////////////////////////////////////////////////////////////////////
 46  
 47    **
 48    ** Enums are only equal if same instance using ===.
 49    **
 50    override Bool equals(Obj obj)
 51  
 52    **
 53    ** Compare based on ordinal value.
 54    **
 55    override Int compare(Obj obj)
 56  
 57    **
 58    ** Always returns name().
 59    **
 60    override Str toStr()
 61  
 62  /////////////////////////////////////////////////////////////////////////
 63  // Access
 64  //////////////////////////////////////////////////////////////////////////
 65  
 66    **
 67    ** Get the programatic name for this enum.
 68    **
 69    Str name()
 70  
 71    **
 72    ** Return ordinal value which is a zero based index into values.
 73    **
 74    Int ordinal()
 75  
 76  }