D Programming Language
Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

# object

Forms the symbols available to all D programs. Includes Object, which is the root of the class object hierarchy. This module is implicitly imported.
License:
Boost License 1.0.
Authors:
Walter Bright, Sean Kelly

class Object;
  All D class objects inherit from Object.
  string toString();
    Convert Object to a human readable string.
  nothrow @trusted size_t toHash();
    Compute hash function for Object.
  int opCmp(Object o);
    Compare with another Object obj.
    Returns:

    this < obj | < 0
    this == obj | 0
    this > obj | > 0
    this < obj | < 0
    this == obj | 0
    this > obj | > 0

  bool opEquals(Object o);
    Returns !=0 if this object does have the same contents as obj.
  static Object factory(string classname);
    Create instance of class specified by the fully qualified name classname. The class must either have no constructors or have a default constructor.
    Returns:
    null if failed

    Example:

        module foo.bar;

        class C
        {
            this() { x = 10; }
            int x;
        }

        void main()
        {
            auto c = cast(C)Object.factory("foo.bar.C");
            assert(c !is null && c.x == 10);
        }

bool opEquals(const Object lhs, const Object rhs);
  Returns true if lhs and rhs are equal.
struct Interface;
  Information about an interface. When an object is accessed via an interface, an Interface* appears as the first entry in its vtbl.
  TypeInfo_Class classinfo;
    .classinfo for this interface (not for containing class)
  size_t offset;
    offset to Interface 'this' from Object 'this'
alias Classinfo = TypeInfo_Class;
  Runtime type information about a class. Can be retrieved for any class type or instance by using the .classinfo property. A pointer to this appears as the first entry in the class's vtbl[].
struct OffsetTypeInfo;
  Array of pairs giving the offset and type information for each member in an aggregate.
  size_t offset;
    Offset of member from start of object
  TypeInfo ti;
    TypeInfo for this member
class TypeInfo;
  Runtime type information about a type. Can be retrieved for any type using a TypeidExpression.
  const nothrow @trusted size_t getHash(in void* p);
    Returns a hash of the instance of a type.
  const bool equals(in void* p1, in void* p2);
    Compares two instances for equality.
  const int compare(in void* p1, in void* p2);
    Compares two instances for <, ==, or >.
  const pure nothrow @nogc @property @safe size_t tsize();
    Returns size of the type.
  const void swap(void* p1, void* p2);
    Swaps two instances of the type.
  inout pure nothrow @nogc @property inout(TypeInfo) next();
    Get TypeInfo for 'next' type, as defined by what kind of type this is,
    null if none.
  const pure nothrow @nogc @safe const(void)[] init();
    Return default initializer. If the type should be initialized to all zeros,
    an array with a null ptr and a length equal to the type size will be returned.
  const pure nothrow @nogc @property @safe uint flags();
    Get flags for type: 1 means GC should scan for pointers,
    2 means arg of this type is passed in XMM register
  const const(OffsetTypeInfo)[] offTi();
    Get type information on the contents of the type; null if not available
  const void destroy(void* p);
    Run the destructor on the object and all its sub-objects
  const void postblit(void* p);
    Run the postblit on the object and all its sub-objects
  const pure nothrow @nogc @property @safe size_t talign();
    Return alignment of type
  const pure nothrow @nogc @property @safe immutable(void)* rtInfo();
    Return info used by the garbage collector to do precise collection.
class TypeInfo_Class: object.TypeInfo;
  Runtime type information about a class. Can be retrieved from an object instance by using the .classinfo property.
  byte[] init;
    class static initializer (init.length gives size in bytes of class)
  string name;
    class name
  void*[] vtbl;
    virtual function pointer table
  Interface[] interfaces;
    interfaces this class implements
  TypeInfo_Class base;
    base class
  static const(TypeInfo_Class) find(in char[] classname);
    Search all modules for TypeInfo_Class corresponding to classname.
    Returns:
    null if not found
  const Object create();
    Create instance of Object represented by 'this'.
class Throwable;
  The base class of all thrown objects.
  All thrown objects must inherit from Throwable. Class Exception, which derives from this class, represents the category of thrown objects that are safe to catch and handle. In principle, one should not catch Throwable objects that are not derived from Exception, as they represent unrecoverable runtime errors. Certain runtime guarantees may fail to hold when these errors are thrown, making it unsafe to continue execution after catching them.
  string msg;
    A message describing the error.
  string file;
  size_t line;
    The file name and line number of the D source code corresponding with where the error was thrown from.
  TraceInfo info;
    The stack trace of where the error happened. This is an opaque object that can either be converted to string, or iterated over with foreach to extract the items in the stack trace (as strings).
  Throwable next;
    A reference to the next error in the list. This is used when a new Throwable is thrown from inside a catch block. The originally caught Exception will be chained to the new Throwable via this field.
  string toString();
    Overrides Object.toString and returns the error message. Internally this forwards to the toString overload that takes a delegate.
  const void toString(scope void delegate(in char[]) sink);
    The Throwable hierarchy uses a toString overload that takes a delegate to avoid GC allocations, which cannot be performed in certain error situations. Override this toString method to customize the error message.
void rt_setTraceHandler(TraceHandler h);
  Overrides the default trace hander with a user-supplied version.
  Parameters:

  TraceHandler h | The new trace handler. Set to null to use the default handler.

TraceHandler rt_getTraceHandler();
  Return the current trace handler
Throwable.TraceInfo _d_traceContext(void* ptr = null);
  This function will be called when an exception is constructed. The user-supplied trace handler will be called if one has been supplied, otherwise no trace will be generated.
  Parameters:

  void* ptr | A pointer to the location from which to generate the trace, or null if the trace should be generated from within the trace handler itself.

  Returns:
  An object describing the current calling context or null if no handler is supplied.
class Exception: object.Throwable;
  The base class of all errors that are safe to catch and handle.
  In principle, only thrown objects derived from this class are safe to catch inside a catch block. Thrown objects not derived from Exception represent runtime errors that should not be caught, as certain runtime guarantees may not hold, making it unsafe to continue program execution.
  pure nothrow @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null);
    Creates a new instance of Exception. The next parameter is used internally and should always be null when passed by user code. This constructor does not automatically throw the newly-created Exception; the throw statement should be used for that purpose.
class Error: object.Throwable;
  The base class of all unrecoverable runtime errors.
  This represents the category of Throwable objects that are not safe to catch and handle. In principle, one should not catch Error objects, as they represent unrecoverable runtime errors. Certain runtime guarantees may fail to hold when these errors are thrown, making it unsafe to continue execution after catching them.
  pure nothrow @safe this(string msg, Throwable next = null);
    Creates a new instance of Error. The next parameter is used internally and should always be null when passed by user code. This constructor does not automatically throw the newly-created Error; the throw statement should be used for that purpose.
  Throwable bypassedException;
    The first Exception which was bypassed when this Error was thrown,
    or null if no Exceptions were pending.
alias IMonitor = Object.Monitor;

void destroy(T)(T obj) if (is(T == class));
  Destroys the given object and puts it in an invalid state. It's used to destroy an object so that any cleanup which its destructor or finalizer does is done and so that it no longer references any other objects. It does not initiate a GC cycle or free any GC memory.
pure nothrow @property size_t capacity(T)(T[] arr);
  (Property) Get the current capacity of a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.
  If an append must reallocate a slice with no possibility of extension, then 0 is returned. This happens when the slice references a static array, or if another slice references elements past the end of the current slice.

  Note: The capacity of a slice may be impacted by operations on other slices.

  Examples:

      //Static array slice: no capacity
      int[4] sarray = [1, 2, 3, 4];
      int[]  slice  = sarray[];
      assert(sarray.capacity == 0);
      //Appending to slice will reallocate to a new array
      slice ~= 5;
      assert(slice.capacity >= 5);

      //Dynamic array slices
      int[] a = [1, 2, 3, 4];
      int[] b = a[1 .. $];
      int[] c = a[1 .. $ - 1];
      debug(SENTINEL) {} else // non-zero capacity very much depends on the array and GC implementation
      {
          assert(a.capacity != 0);
          assert(a.capacity == b.capacity + 1); //both a and b share the same tail
      }
      assert(c.capacity == 0);              //an append to c must relocate c.

pure nothrow @trusted size_t reserve(T)(ref T[] arr, size_t newcapacity);
  Reserves capacity for a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.
  The return value is the new capacity of the array (which may be larger than the requested capacity).
  Examples:

      //Static array slice: no capacity. Reserve relocates.
      int[4] sarray = [1, 2, 3, 4];
      int[]  slice  = sarray[];
      auto u = slice.reserve(8);
      assert(u >= 8);
      assert(sarray.ptr !is slice.ptr);
      assert(slice.capacity == u);

      //Dynamic array slices
      int[] a = [1, 2, 3, 4];
      a.reserve(8); //prepare a for appending 4 more items
      auto p = a.ptr;
      u = a.capacity;
      a ~= [5, 6, 7, 8];
      assert(p == a.ptr);      //a should not have been reallocated
      assert(u == a.capacity); //a should not have been extended

nothrow ref inout(T[]) assumeSafeAppend(T)(auto ref inout(T[]) arr);
  Assume that it is safe to append to this array. Appends made to this array after calling this function may append in place, even if the array was a slice of a larger array to begin with.
  Use this only when it is certain there are no elements in use beyond the array in the memory block. If there are, those elements will be overwritten by appending to this array.

  Calling this function, and then using references to data located after the given array results in undefined behavior.
  Returns:
  The input is returned.
  Examples:

      int[] a = [1, 2, 3, 4];

      // Without assumeSafeAppend. Appending relocates.
      int[] b = a [0 .. 3];
      b ~= 5;
      assert(a.ptr != b.ptr);

      debug(SENTINEL) {} else
      {
          // With assumeSafeAppend. Appending overwrites.
          int[] c = a [0 .. 3];
          c.assumeSafeAppend() ~= 5;
          assert(a.ptr == c.ptr);
      }

bool _ArrayEq(T1, T2)(T1[] a1, T2[] a2);
  Helper function used to see if two containers of different types have the same contents in the same sequence.
template RTInfo(T)
  Create RTInfo for type T
@property auto dup(T)(T[] a) if (!is(const(T) : T));
@property T[] dup(T)(const(T)[] a) if (is(const(T) : T));
@property @trusted T[] dup(T : void)(const(T)[] a);
  Provide the .dup array property.
@property immutable(T)[] idup(T)(T[] a);
@property immutable(T)[] idup(T : void)(const(T)[] a);
  Provide the .idup array property.

Copyright Digital Mars 2000 - 2011. | Page generated by Ddoc on Sat Apr 25 12:22:47 2015