Application
- Object
- Once
- Application
- Once
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.
- #include "So-o.h"
- #include "Once.h"
- 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.
- void defclassApplication() {
- property _i_properties[] = {
- "appName",
- "app",
- 0
- };
- selector _i_messages[] = {
- "init", METHOD(i_init),
- "appName", METHOD(i_appName),
- "doesNotRecognize", METHOD(i_doesNotRecognize),
- 0, 0
- };
- Application = defclass("Application", Once, 1, 0, _i_properties, 0, _i_messages);
- }
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
- static instance i_init(instance self, va_list va) {
- char *appName = va_arg(va, char *);
- instance app = va_arg(va, instance);
- supersend(Application, self, "init");
- if (appName) {
- sendmsg(self, "set", "appName", appName);
- if (app)
- sendmsg(self, "set", "app", app);
- }
- return self;
- }
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
- static char *i_appName(instance self) {
- return sendmsg(self, "get", "appName").p;
- }
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
- static value i_doesNotRecognize(instance self, va_list va) {
- message msg = va_arg(va, message);
- instance app = sendmsg(self, "get", "app").p;
- if (!app)
- return supersend(Application, self, "doesNotRecognize", msg);
- return applymsg(app, msg, va_arg(va, va_list));
- }
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
- #include "So-o.h"
- #include "Application.h"
- #include "Hello.h"
- #include <stdlib.h>
- int main( int argc, char *argv[] ) {
- defclassHello();
- instance hello = (instance)sendmsg(Hello, "new").p;
- sendmsg(hello, "hello");
- defclassApplication();
- instance app=sendmsg(Application, "new", "Hello", hello).p;
- printf("%s\n", (char *)sendmsg(app, "appName").p);
- sendmsg(app, "hello");
- sendmsg(app, "set", "app", 0);
- sendmsg(app, "hello");
- exit( 0 );
- }
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
Comments