Juce Source Code Analysis (ix) Application base class ApplicationBase

Source: Internet
Author: User

In the previous articles, the analysis of the Juce library inside the core module of the memory part, in addition to the Ashes level C + + enthusiasts, it seems that everyone is not very interested in these. I believe you want to know more about how juce is used in product development, but not very interested in its composition. Write some articles such as memory, pointers, threads, etc. every day. Skilla is tired of it. This time to analyze Juce's upper application framework.

The following code snippet from the previous demo

Class Jucedemoapplication:public Juceapplication{public:jucedemoapplication () {}//=============================        ================================================= void initialise (const string& commandLine) override {        if (Invokechildprocessdemo (commandLine)) return;        Desktop::getinstance (). setorientationsenabled (desktop::allorientations);        Do your application ' s initialisation code here.    MainWindow = new Mainappwindow ();        } void Shutdown () override {//Do your application ' s shutdown code here.    MainWindow = nullptr; }//============================================================================== void SystemRequestedQuit () Override {//This gets called when the OS wants we app to quit. Want to//ask the user-to-save documents, close windows, etc here, if this/case we ' ll just C All quit (), which tells the message loop to stop and//allows the app to (asynchronously) exit.    Quit (); }//============================================================================== Const String GetApplicationName    () Override {return "Jucedemo";    } const String Getapplicationversion () override {return projectinfo::versionstring;    } bool Morethanoneinstanceallowed () override {return true; } void anotherinstancestarted (const string&/*commandline*/) override {}private:scopedpointer<mainap Pwindow> mainwindow;};/ /==============================================================================//This macro generates the main () Routine that starts the app. Start_juce_application (jucedemoapplication)

If you have seen the demo inside Juce, you must be familiar with this class, yes. This is the application class in Jucedemo. What is the first thing you should think about when you see it for the first time? This is what we should be able to think, after learning the WIN32 when the MFC scene, a very painful problem is: How to execute this thing, where is its WinMain function? The first time to find MFC's WinMain function when the feeling. Believe that we have experienced, keep pressing F11, as if through a few centuries, see is a strange "face", the last toilet of kung fu back can forget all, analyze the source code feeling let people feel hesitant, but very exciting. In contrast JUCE's irritation is much smaller, to Start_juce_application press F11 once saw WinMain.

#if juce_windows &&! Defined (_console)  #define juce_main_function       int __stdcall WinMain (struct hinstance__*, struct hinstance__*, char*, int)  #define Juce_main_function_args #else  #define juce_main_function       int MAIN (int argc, char* argv [])  #define Juce_main_function_args  argc, (const char**) argv #endif #define Start_juce_application (Appclass)     static juce::juceapplicationbase* juce_createapplication () {return new Appclass ();}     extern "C" juce_main_function     {         juce::juceapplicationbase::createinstance = &juce_CreateApplication;         Return Juce::juceapplicationbase::main (Juce_main_function_args);     } #endif


Ignoring other platforms, simply looking at the Windows section, start_juce_application equivalent to the following code

Static juce::juceapplicationbase* Juce_createapplication () {return new jucedemoapplication ();} extern "C"  int __ stdcall WinMain (struct hinstance__*, struct hinstance__*, char*, int) {juce::juceapplicationbase::createinstance = &juce_CreateApplication; return Juce::juceapplicationbase::main (); }

The WinMain function actually executes the static member function inside the main ()

int Juceapplicationbase::main () {    Scopedjuceinitialiser_gui libraryinitialiser;    Jassert (CreateInstance! = nullptr);    Const scopedpointer<juceapplicationbase> App (CreateInstance ());    Jassert (App! = nullptr);    if (! App->initialiseapp ())        return App->getapplicationreturnvalue ();    Juce_try    {        //loop until a quit message is received..        Messagemanager::getinstance ()->rundispatchloop ();    }    Juce_catch_exception    return App->shutdownapp ();}
Scopedjuceinitialiser_gui is used to create a MessageManager (message manager) Singleton, but at the same time also shoulders the responsibility to deconstruct it, under Scopedpointer management. The inside of the local object app is also to fend for itself. App->initialiseapp () is used for initialization of the application, the initialization of the general application layer, and the creation of forms are implemented here.

The following sentence code is messagemanager::getinstance ()->rundispatchloop ();. This code is written in the shortest possible way. However, it is the longest execution time, and it is the message loop that we are most familiar with. The final Shutdownapp is the shutdown member function of the child class that is processed and called, and as a notification of the end of the application, Shutdownapp returns to the end of the WinMain function. Then Messagemanage's singleton and app objects are destroyed by the scope. This shows that the design is reasonable. In this chapter we mainly analyze Initialiseapp () to see how the application is initialized.

bool Juceapplicationbase::initialiseapp () {#if juce_handle_multiple_instances if ((! mo Rethanoneinstanceallowed ()) && sendcommandlinetopreexistinginstance ()) {DBG ("another instance is Runni        Ng-quitting ... ");    return false;    } #endif/Let the app does its setting-up.    Initialise (Getcommandlineparameters ());    Stillinitialising = false;   if (Messagemanager::getinstance ()->hasstopmessagebeensent ()) return false; #if juce_handle_multiple_instances if (multipleinstancehandler! = nullptr) messagemanager::getinstance ()->reg   Isterbroadcastlistener (Multipleinstancehandler); #endif return true;} 
The purpose of the most initial if inference statement is to infer whether the application agrees to multiple instances at the same time and to see if there are currently instances executing, since some software does not agree to "double open" or "multiple open", where it is possible to overload morethanoneinstanceallowed () Method.  Initialise (Getcommandlineparameters ()), the function is to get command line parameters, passed to the subclass and complete the initialization of the subclass.  if (Messagemanager::getinstance ()->hasstopmessagebeensent ()) return false;   This sentence is inferred whether the message manager received an exit message. if (multipleinstancehandler! = nullptr)
Messagemanager::getinstance ()->registerbroadcastlistener (multipleinstancehandler); This sentence is the listener for the process communication.

Once these operations are complete, the message loop is over.


In ApplicationBase's member function main (), we see that the author skillfully utilizes the stack object's own active destructor to manage the life cycle of the MessageManager and ApplicationBase two objects, making it a reasonable time to initialize. The timing of the destruction is also reasonable, which is worthy of reference.







Juce Source Code Analysis (ix) Application base class ApplicationBase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.