Next: , Previous: , Up: Classes   [Index]


Symbol

Symbol Class

Symbol class objects represent memory locations. They can refer to objects, C variables, buffers, I/O ports, and other data located in memory.

As an alternative, if you receive an Unimplemented C type warning, you can store various types of C data in Symbol objects. See Objects in Function Arguments.

A unary ‘*’ operator behaves similarly to the C operator. That is, it refers to the value of the Symbol object, not the object itself. The following example might make this clearer.


int main () {

  Symbol new sym1;
  Symbol new sym2;
  Symbol new sym3;
  Integer new i;
  
  i = 2;

  sym3 = sym1;   /* Save the original value of sym1. */

  sym1 = i;
  printf ("%d\n", sym1);

  *sym2 = i;
  printf ("%d\n", *sym2);

  sym1 = sym3;  /* Restore sym1 to the original object. */

  i = 4;
  
  *sym1 = i;

  printf ("%d\n", *sym1);
}

Instance Variables

value

The value of a Symbol is a hexadecimal memory address.

Instance Methods

* (void)

The *. A shortcut for the getValue method, below.

= (void *v)

Assign a reference to the argument as the value of the receiver. If v is also a Symbol object, simply copy the reference. This method is a synonym for symbolReference, below. If you want to use multiple levels of object references and dereferences, see the addressOf method in Object class See Object, and the deref method, below.

asAddrString (void)

Returns a String object with the formatted hexadecimal address of the object pointed to by the receiver.

asString (void)

Returns the receiver’s value as a String object with the value of the char string that the Symbol receiver object points to. The result object has the same name as the receiver and the class of String.

basicObject (char *name, char *classname, char *superclassname, char *value_expr)

Create a basic object and make the receiver’s value point to it. The new object has the scope ‘LOCAL_VAR|VAR_REF_OBJECT’ and a reference count of 1.

Referencing the object with a Symbol makes it easy to create objects and then assign them to C variables in the calling method or function. For example,


  Symbol new s;
  OBJECT *int_object;

  s basicObject "new_int", "Integer", "Magnitude", "10";

  int_object = s getValue;

  printf ("%d\n", int_object value);

deref (void)

Return the object referred to by the receiver. This method is functionally equivalent to the C ‘*’ operator.

getValue (void)

Return the object that the receiver’s value (previously set by = or symbolReference) refers to. If the address doesn’t refer to an object, returns the receiver and generates an exception, which the program can handle in whatever manner is necessary. In some cases, the internals of Ctalk’s object-to-C routines can also generate an warning message.

Note that when assigning a non-object data address to a C variable like a void *, Ctalk allows both of these expressions:


 void *myVoidPtr;

 myVoidPtr = mySymbol;

 myVoidPtr = *mySymbol;   /* Generates an exception. */

name (void)

Return a new String object containing the receiver’s name.

removeValue (void)

Remove the reference to the target object from the receiver. Delete the target object if there are no further references to it.

symbolReference (void *v)

Return a new String object containing the receiver’s name.


Next: , Previous: , Up: Classes   [Index]