![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |
The HelloWorld source code is used as an example to explain the basic structure and processing flow of SophiaFramework application.
Example 8.1. Hello World : .hpp file (class definition)
// // HelloWorld.hpp // // This source code was automatically // generated by SophiaFramework 4.1. // #ifndef __HELLOWORLD_HPP #define __HELLOWORLD_HPP #include <SophiaFramework.hpp> #include "HelloWorld.bid" 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) }; #endif // __HELLOWORLD_HPP //
Example 8.2. Hello World: .cpp file (Boot Loader and Factory function)
// // HelloWorld.cpp // // This source code was automatically // generated by SophiaFramework 4.1. // #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; } // Constructor HelloWorld::HelloWorld(Void) static_throws { if (static_try()) { static_throw(RegisterHandler(SREVT_RESPONDER_RENDER, SRP16_RENDER_CONTENT, HANDLER_BEFORE, HANDLER_FUNCTION(OnRenderContent))); } return; } // Destructor HelloWorld::~HelloWorld(Void) { return; } // Drawing handler HANDLER_IMPLEMENT_VOIDRENDER(HelloWorld, OnRenderContent, graphics) { // draw the string "Hello World" to the screen graphics->DrawText("Hello World", GetContentWorld()); return; }
The Application class of HelloWorld that uses GUI framework inherits from SFRApplication class.
![]() |
Note |
|---|---|
In case GUI framework is not used, it inherits from SFCApplication. | |
SFMTYPEDEFCLASS is the macro function that automatically generates convenient names related with user-defined types.
Since the instance of Application class must not be copied, the copy operation is prohibited by using the SFMSEALCOPY macro function.
The factory function, the constructor, the destructor, and the drawing handler are defined in the HelloWorld class.
The so-called "Boot Loader" SFCApplet::Boot() function starts the execution of the SophiaFramework application. In this function, the factory function corresponding to ClassID and Lisence Code is returned. The factory function generates the instance of the HelloWorld application class.
The macro function HANDLER_DECLARE_VOIDRENDER(OnRenderContent) in the header file declares the drawing handler to draw "Hello World".
In the constructor, register the drawing handler with the drawing event by using the RegisterHandler function. The drawing handler is implemented by using the HANDLER_IMPLEMENT_VOIDRENDER(HelloWorld, OnRenderContent, graphics) macro function.
The DrawText function in the drawing handler draws the string "Hello World". The "graphics" variable is a pointer to the instance of SFXGraphics class. When the handler for a certain event is implemented in the child class, the handler of parent class for the same event will not be called. The following code describes how to call the handler of parent class.
Example 8.3. Call the handler of parent class from child class
HANDLER_IMPLEMENT_VOIDRENDER(HelloWorld, OnRenderContent, graphics)
{
// call drawing handler of parent class
SFRApplication::ContentHandler(graphics);
// draw string on screen
graphics->DrawText("Hello World",
GetContentWorld(),
SFXRGBColor(0x00, 0x00, 0x00, 0x00));
return;
}
In the above code, the whole screen is painted in white, after the drawing handler ContentHandler(graphics) of SFRApplication class (HelloWorld's parent class) is called, the text color is set to black, and the string "Hello World" is drawn.
The static_try function verifies the error value that may occur in the constructor, while the static_throw function sets an error value in the constructor. The error value set by the static_throw function can be caught by using the static_catch function. In general, the static_catch function is called in the class that uses the class instance throwing an error value by the static_throw function.
Example 8.4. How to use the static_catch function
// code to catch and handle an error value helloworld = ::new HelloWorld(); switch( helloworld->static_catch() ){ // handle the error according to its 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.
|
Copyright (C) 2002 - 2009 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|