PrevNextUpHome SophiaFramework UNIVERSE 5.3

5.2. SophiaFramework Application

In this section, the processing flow of SophiaFramework application from starting up to terminating is described using the code of HelloWorld application automatically generated by the SophiaFramework AppWizard.

5.2.1. Starting up the Application

SophiaFramework application must include the SFCApplet::Boot function and the Factory function.

The SFCApplet::Boot function is the first segment that is executed within the application. This function sets the license code and validates this with the class ID of application. If the class ID parameter matches that of the application class, "AEECLSID_HELLOWORLD" (HelloWorld Application class), the Factory function is returned.

Next, the Factory function is executed, and it calls the constructor of application class to generate the instance of "HelloWorld" application class.

[Note] Note
The license code must correspond to the Class ID of application. But you do not need to set the license code when testing your application on the BREW simulator.

Example 5.1. Boot loader and Factory function

//      HelloWorld.cpp

#include "HelloWorld.hpp"

// Boot loader
SFCApplet::FactorySPP SFCApplet::Boot(AEECLSID id, SFXAnsiStringPtr license)
    *license = "heap://"

    return (id == AEECLSID_HELLOWORLD) ? (&HelloWorld::Factory) : (null);

// Factory function
SFCInvokerPtr HelloWorld::Factory(Void)
    return ::new HelloWorld;

5.2.2. Defining the Application Class

The HelloWorld application uses the SFR GUI framework, and thus its Application class inherits from the SFRApplication class.

[Note] Note
The application using the SFR GUI framework is called SFR applet.

SFMTYPEDEFCLASS is a macro to generate the user-defined types for HelloWorld application automatically.

SFMSEALCOPY is a macro to prevent the instance of application class from being copied (the SFRApplication instance must not be copied).

HANDLER_DECLARE_VOIDRENDER(OnRenderContent) declares the drawing handler.

HANDLER_DECLARE_BOOLEVENT(OnKey) declares the key handler.

Example 5.2. Define the Application class for HelloWorld

//      HelloWorld.hpp


#include <SophiaFramework.hpp>
#include ""

//  HelloWorld application class
class HelloWorld : public SFRApplication {
    static SFCInvokerPtr Factory(Void);
    HelloWorld(Void) static_throws;
    virtual ~HelloWorld(Void);

#endif // __HELLOWORLD_HPP //
[Note] SFC applet

The Application class of SFC applet not using the GUI framework inherits from the SFCApplication class.

5.2.3. Implementing the Constructor

In the constructor of application class, its instance is created and the event handler functions are registered using the RegisterHandler function. Event code, data attached to the event code, timing to call the event handler function, and pointer to the event handler function are specified as the arguments of RegisterHandler function.

Example 5.3. Implement the Constructor

// Constructor
HelloWorld::HelloWorld(Void) static_throws
    if (static_try()) {
                            HANDLER_BEFORE, HANDLER_FUNCTION(OnRenderContent)));
    if (static_try()) {
            RegisterHandler(SFEVT_KEY, HANDLER_AFTER, HANDLER_FUNCTION(OnKey)));

5.2.4. Implementing the Destructor

At the termination of application, the destructor is called to destroy the instance of application class. UI components such as windows and controls are automatically destroyed. (It is unnecessary to write the code for destroying the UI components.)

Example 5.4. Implement the Destructor

// Destructor

5.2.5. Implementing the Event Handler

An event handler is the function to describe the processing when an event occurs.

The drawing handler processing draw events is defined by HANDLER_IMPLEMENT_VOIDRENDER(HelloWorld, OnRenderContent, graphics).

The key handler processing key events is defined by HANDLER_IMPLEMENT_BOOLEVENT(HelloWorld, OnKey, event). Implementing the Drawing Handler

Display the string "HellowWorld" on screen using the graphics->DrawText function.

[Caution] Caution:

* In the SFR applet using the GUI framework, the drawing process must be implemented within the drawing handler.

Example 5.5. Implement the drawing handler

// Drawing handler
HANDLER_IMPLEMENT_VOIDRENDER(HelloWorld, OnRenderContent, graphics)
    graphics->DrawText("Hello World", GetContentWorld());
} Implementing the key handler

The code to end the application when the "Select" key is pressed is as below.

Example 5.6. Implement the Key Handler

// Key handler
    switch (event.GetP16()) {
        case AVK_SELECT: // when "Select" key is pressed
            Terminate(); // application is ended
            return true;
    return false;
[Caution] Caution:

By calling the Terminate function, the destructor is called and the application is ended.

5.2.6. Error Handling

In the constructor, the static_try function is used to verify the error value while the static_throw function is used to set the error value.

The error value can be gotten by calling the static_catch function.

Example 5.7. Code to catch and handle the error value

// code to catch and handle error value
helloworld = ::new HelloWorld();
switch( helloworld->static_catch() ){
     // error handling for a specific error value

The static_exception class has the variable that holds the error value. The type of this variable is template and the SFCError type is usually used.

The merit of static_exception class lies in the fact that it is the general rule of throwing out the error value inside the constructor or operator.