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://"
      "TIXDRQXNU5WHU8Y3Z9WOHWQR6Z3VPSDHDV5CR1S4XASPWLUHWAS7Z5Z2TGS3XMSAT3UPUQTLTARCYPSF"
      "UEJZ6ROSJWGUQSEYKR6V2U4VESMTQLHKZ6X7Y2VKXHWIX3XBU0Z7VHWHXIZBSGT5SPU3XLX0Z1Y4R3TC"
      "U6WGT9WHWIVNYHYCUCR9T3SMTEWPRNVAX1Y4VPW2YCY9YQV5R7Z9UIVHT6SDUPU2SIW6VCRCWBR2S4WQ"
      "UPYFWCYGT4VIT1WHXGYPTQSFYPWNV3ULRNWFW7RBRFVKUKS2YQSQYHW1TPUPXBZ6UEY2WOYKR7S3TAU4"
      "TQS6UHVFVEVLU3R5SDSKW7RPTNTPVQU2T4R8Z4VLUGEW3U98TLDR8/";

    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
//

#ifndef __HELLOWORLD_HPP
#define __HELLOWORLD_HPP

#include <SophiaFramework.hpp>
#include "HelloWorld.bid"

//
//  HelloWorld application class
//
SFMTYPEDEFCLASS(HelloWorld)
class HelloWorld : public SFRApplication {
    SFMSEALCOPY(HelloWorld)
public:
    static SFCInvokerPtr Factory(Void);
private:
    HelloWorld(Void) static_throws;
    virtual ~HelloWorld(Void);
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
};

#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()) {
        static_throw(
            RegisterHandler(SREVT_RESPONDER_RENDER, SRP16_RENDER_CONTENT,
                            HANDLER_BEFORE, HANDLER_FUNCTION(OnRenderContent)));
    }
    if (static_try()) {
        static_throw(
            RegisterHandler(SFEVT_KEY, HANDLER_AFTER, HANDLER_FUNCTION(OnKey)));
    }
    return;
}

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
HelloWorld::~HelloWorld(Void)
{
    return;
}

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).

5.2.5.1. 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());
    return;
}

5.2.5.2. 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
HANDLER_IMPLEMENT_BOOLEVENT(HelloWorld, OnKey, event)
{
    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.