Using global variables

The use of global variables is not recommended.

A preferred solution would be to create variables in your main() (or similar) function and to pass them via reference to other functions as required.

void main()
{
        dca::DcaInstance myDca;
        myDca = dca::DcaInstance::create( myInitData );
        ...
        // put all necessary instances to subsequent functions via reference...
        DoWork( myDca, myLicense, myUrlDbClassifier );

}

This ensures that once the DoWork() function returns, the DcaInstance goes out of scope, and all variables are destructed in the reverse creation order.

The object instances can be placed in a separate function as well as the deinit code.

For more information on this please refer to Startup/Shutdown sequence.

If you're using many more variables than in our example, you may also use a so called context:

class MyDcaContext
{
public:

        MyDcaContext() : inShutDown (false)
        { }

        virtual ~MyDcaContext()
        {
                // signal DCA worker threads to shutdown
                inShutDown = true;

                 // this cancels the download or update task if it is currently running
                updateModule.cancelUpdate();

                // this cancels all long-lasting functions and forces them to return immediately
                // such as database merge processes...
                dcaInstance.signal( DCA_SIG_ABORT );

                // ...
                // wait for worker threads to finish
                // ...

                // all other DCA objects are destructed in reverse construction order
        }

        // initialize the DCA objects
        virtual bool Init();

        // Construction order is important!
        // the DCA instance must always be created first.
        dca::DcaInstance dcaInstance;
        dca::InitData initData;
        dca::License license;
        dca::UpdateModule updateModule;
        dca::DbConnectionData connectData;
        dca::DbConnection dbConnection;
        dca::UrlClassification urlClassification;
        dca::UrlDbClassifier urlDbClassifier;

        volatile bool inShutDown;
        ...
};
void main ()
{
        MyDcaContext myContext;
        myContext.dcaInstance = dca::DcaInstance::create( myInitData );
        ...
        // put all necessary instances to subsequent functions via reference...
        DoWork( myContext );
}

So it's very easy to add more global-like variables to your context and create them in the scope of your main() function.

Since the assignment operators of all classes have been overloaded, you will not need to deal with checks for visibility, where to destruct variables or even where to addref or release an object.

This is all done automatically by the wrapper layer of the SCA API.


Generated on 26 Sep 2016 for dca_interface by  doxygen 1.6.1