The C++ code

We will implement a service named FirstSvc.

For a service can be invoked by others, the header file should be visible globally. We can export the FirstSvc.h header file directly for simplicity. However, we can add a pure virtual class to declare the user APIs. So that we can implement different services with the same APIs. These services can be replaced with each other. This is an useful feature in a framework.

The defination of the pure virtual class is,

// file FirstSvc/IfMySvc.h
#ifndef IF_MY_SVC_H
#define IF_MY_SVC_H

class IfMySvc
{
    public:
        virtual void doSomeThing() = 0;
};
#endif

We put this header file in a seperated directory, and copy it into a directory that can be found by the compiler. (This is guaranteed by a pattern of CMT at present.)

We use a multiple inheritance in the defination of FirstSvc. The code is,

// file FirstSvc.h
#ifndef FIRST_SVC_H
#define FIRST_SVC_H

#include "FirstSvc/IfMySvc.h"
#include "SniperKernel/SvcBase.h"

class FirstSvc : public SvcBase, public IfMySvc
{
    public:
        FirstSvc(const std::string& name);

        bool initialize();
        bool finalize();

        void doSomeThing();
};
#endif

There is no execute() method. Instead, we should implement the method doSomeThing() as the user API.

The source file is,

// file FirstSvc.cc
#include "FirstSvc/FirstSvc.h"
#include "SniperKernel/SvcFactory.h"

DECLARE_SERVICE(FirstSvc);

FirstSvc::FirstSvc(const std::string& name)
    : SvcBase(name)
{
}

bool FirstSvc::initialize()
{
    LogDebug << "in FirstSvc::initialize()" << std::endl;
    return true;
}

bool FirstSvc::finalize()
{
    LogDebug << "in FirstSvc::finalize()" << std::endl;
    return true;
}

void FirstSvc::doSomeThing()
{
    LogInfo << "Do some thing in a service" << std::endl;
}

Like the algorithm, a service must be declared by a macro DECLARE_SERVICE. We can declare properties in the servcie constructor, too. But we will not repeat this here.

In this example, we only print a message in the doSomeThing() API.

results matching ""

    No results matching ""