23

Application

  1. Object
    1. Once
      1. Application

An Application class has only one instance. An Application instance can have a name. It can be associated to another instance which will interpret all the messages it receives but which it doesn't implement.

  1. #include "So-o.h"
  2.  
  3. #include "Once.h"
  4.  
  5. class Application;

Loads the declarations of the data types and functions of So-o. Loads the declarations of the Once class. Defines the class Application.

  1. void defclassApplication() {
  2.     property _i_properties[] = {
  3.         "appName",
  4.         "app",
  5.         0
  6.     };
  7.     selector _i_messages[] = {
  8.         "init",             METHOD(i_init),
  9.         "appName",          METHOD(i_appName),
  10.         "doesNotRecognize", METHOD(i_doesNotRecognize),
  11.         0, 0
  12.     };
  13.  
  14.     Application = defclass("Application", Once, 1, 0, _i_properties, 0, _i_messages);
  15. }

Defines defclassApplication, the constructeur of the class Application.

The Application class inherits from the Once class. The instance properties appName and app contain the name of an Application instance and the reference to its proxy. The Application class redefines the instance messages init and doesNotRecognize. It adds the instance message appName.

INSTANCE METHODS

init
SYNOPSIS

instance init(instance self, char *appName, instance app)

DESCRIPTION

init initializes the name and the proxy of self with appName and app. appName and app can be NULL.

CODE
  1. static instance i_init(instance self, va_list va) {
  2.     char *appName = va_arg(va, char *);
  3.     instance app = va_arg(va, instance);
  4.  
  5.     supersend(Application, self, "init");
  6.  
  7.     if (appName) {
  8.         sendmsg(self, "set", "appName", appName);
  9.         if (app)
  10.             sendmsg(self, "set", "app", app);
  11.     }
  12.  
  13.     return self;
  14. }

Extracts the parameters appName and app from the argument list va. Runs the init method of the superclass of the Application class. Initializes the instance properties appName if appName isn't NULL then app if app isn't NULL .

appName
SYNOPSIS

char *appName(instance self)

DESCRIPTION

appName returns the name of self. If self has no name, appName returns a NULL.

CODE
  1. static char *i_appName(instance self) {
  2.     return sendmsg(self, "get", "appName").p;
  3. }

Returns the appName property of self.

doesNotRecognize
SYNOPSIS

value doesNotRecognize(instance self, message msg)

DESCRIPTION

An Application instance intercepts the message doesNotRecognize which is automatically sent to a class or an instance which receives a message it doesn't implement.

doesNotRecognize returns the result of forwarding msg and its parameters to the proxy of self, the instance which was passed as a parameter to the new message to create self.

If self has no proxy, doesNotRecognize executes the message doesNotRecognize in the context of the superclass of the Application class, which will run the doesNotRecognize method of the Object class, which triggers an error.

CODE
  1. static value i_doesNotRecognize(instance self, va_list va) {
  2.     message msg = va_arg(va, message);
  3.  
  4.     instance app = sendmsg(self, "get", "app").p;
  5.  
  6.     if (!app)
  7.         return supersend(Application, self, "doesNotRecognize", msg);
  8.  
  9.     return applymsg(app, msg, va_arg(va, va_list));
  10. }

Extracts the parameter msg from the argument list va. Initializes app to the value of the app property of self. If app is NULL, returns the result of executing the message doesNotRecognize by the superclass of the Application class. Otherwise, if self has a proxy, returns the result of sending msg and its parameters to app.

EXAMPLE
  1. #include "So-o.h"
  2.  
  3. #include "Application.h"
  4. #include "Hello.h"
  5.  
  6. #include <stdlib.h>
  7.  
  8. int main( int argc, char *argv[] ) {
  9.     defclassHello();
  10.  
  11.     instance hello = (instance)sendmsg(Hello, "new").p;
  12.     sendmsg(hello, "hello");
  13.  
  14.     defclassApplication();
  15.  
  16.     instance app=sendmsg(Application, "new", "Hello", hello).p;
  17.     printf("%s\n", (char *)sendmsg(app, "appName").p);
  18.     sendmsg(app, "hello");
  19.  
  20.     sendmsg(app, "set", "app", 0);
  21.     sendmsg(app, "hello");
  22.  
  23.     exit( 0 );
  24. }

The class Echo has one instance message, echo, which displays the text passed as a parameter followed by a newline.

Builds the class Echo. Creates an instance of Echo. Sends it the message echo with the string Adieu ! in argument. Builds the class Application. Creates an instance of Application called Echo with the instance of Echo as a proxy. Displays the name of the application. Sends the message echo with the string Adieu ! in argument to the application. The message is interpreted by the instance of Echo. Sends the message hello to the application. The proxy displays an error message. Removes the proxy from the application. Sends the message echo to the application. The message triggers an error.

$ gcc -I/usr/local/include/so-o -O -c test-Application.c
$ gcc test-Application.o -lso-o -o test-Application
$ test-Application 
Adieu !
Echo
Adieu !
Echo::hello Invalid instance message
Application::echo Invalid instance message
SEE ALSO

Once, Responder

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].