Example Component with Initialization#

Note

A VAPI Component can have its own configuration in a TOML-like structure.

#include <iostream>
#include <support/toml.h>
#include <support/component_impl.h>
#include "example_interfaces.h"

class CTestComponentWithInitialization
    : public sdv::CSdvObject
    , public ISayHello
    , public ISayGoodbye
{
public:
    CTestComponentWithInitialization()
    {
        std::cout << "Entering CTestComponentWithInitialization constructor..." << std::endl;
    }
    ~CTestComponentWithInitialization() override
    {
        std::cout << "Entering CTestComponentWithInitialization destructor..." << std::endl;
    }

    BEGIN_SDV_INTERFACE_MAP()
        SDV_INTERFACE_ENTRY(ISayHello)
        SDV_INTERFACE_ENTRY(ISayGoodbye)
    END_SDV_INTERFACE_MAP()

    DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::device)
    DECLARE_OBJECT_CLASS_NAME("Hello_Component_With_Initialization")

    // Parameter map
    BEGIN_SDV_PARAM_MAP()
        SDV_PARAM_ENTRY(m_Number, "number", -1, "", "A number")
    END_SDV_PARAM_MAP()

    /**
     * @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
     * @return Returns 'true' when the initialization was successful, 'false' when not.
     */
    virtual bool OnInitialize() override
    {
        return true;
    }

    /**
     * @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
     */
    virtual void OnShutdown() override
    {}

    /**
    * @brief Show messages, implements the function of IShowExample
    */
    void SayHello() override
    {
        std::cout << "Hello from Hello_Component_With_Initialization ... " << std::to_string(m_Number) << std::endl;
    }

    /**
    * @brief Show messages, implements the function of ISayGoodbye
    */
    void SayGoodbye() override
    {
        std::cout << "Goodbye from Hello_Component_With_Initialization ... " << std::to_string(m_Number) << std::endl;
    }

private:
    int32_t m_Number = -1;
};

DEFINE_SDV_OBJECT(CTestComponentWithInitialization)
#include <iostream>
#include <support/toml.h>
#include <support/component_impl.h>
#include "example_interfaces.h"

class CTestComponentWithInitialization
    : public sdv::CSdvObject
    , public ISayHello
    , public ISayGoodbye
{
public:
    CTestComponentWithInitialization()
    {
        std::cout << "Entering CTestComponentWithInitialization constructor..." << std::endl;
    }
    ~CTestComponentWithInitialization() override
    {
        std::cout << "Entering CTestComponentWithInitialization destructor..." << std::endl;
    }

    BEGIN_SDV_INTERFACE_MAP()
        SDV_INTERFACE_ENTRY(ISayHello)
        SDV_INTERFACE_ENTRY(ISayGoodbye)
    END_SDV_INTERFACE_MAP()

    DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::device)
    DECLARE_OBJECT_CLASS_NAME("Hello_Component_With_Initialization")

    // Parameter map
    BEGIN_SDV_PARAM_MAP()
        SDV_PARAM_ENTRY(m_Number, "number", -1, "", "A number")
    END_SDV_PARAM_MAP()

    /**
     * @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
     * @return Returns 'true' when the initialization was successful, 'false' when not.
     */
    virtual bool OnInitialize() override
    {
        return true;
    }

    /**
     * @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
     */
    virtual void OnShutdown() override
    {}

    /**
    * @brief Show messages, implements the function of IShowExample
    */
    void SayHello() override
    {
        std::cout << "Hello from Hello_Component_With_Initialization ... " << std::to_string(m_Number) << std::endl;
    }

    /**
    * @brief Show messages, implements the function of ISayGoodbye
    */
    void SayGoodbye() override
    {
        std::cout << "Goodbye from Hello_Component_With_Initialization ... " << std::to_string(m_Number) << std::endl;
    }

private:
    int32_t m_Number = -1;
};

DEFINE_SDV_OBJECT(CTestComponentWithInitialization)

The easiest way is to add a parameter map. The variable value from the configuration file is automatically set. The parameter can be defined as SDV_PARAM_ENABLE_LOCKING(). In that case the parameter is protected against writing after initialization. It’s also possible to define the parameter as SDV_PARAM_SET_READONLY().

#include <iostream>
#include <support/toml.h>
#include <support/component_impl.h>
#include "example_interfaces.h"

class CTestComponentWithInitialization
    : public sdv::CSdvObject
    , public ISayHello
    , public ISayGoodbye
{
public:
    CTestComponentWithInitialization()
    {
        std::cout << "Entering CTestComponentWithInitialization constructor..." << std::endl;
    }
    ~CTestComponentWithInitialization() override
    {
        std::cout << "Entering CTestComponentWithInitialization destructor..." << std::endl;
    }

    BEGIN_SDV_INTERFACE_MAP()
        SDV_INTERFACE_ENTRY(ISayHello)
        SDV_INTERFACE_ENTRY(ISayGoodbye)
    END_SDV_INTERFACE_MAP()

    DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::device)
    DECLARE_OBJECT_CLASS_NAME("Hello_Component_With_Initialization")

    // Parameter map
    BEGIN_SDV_PARAM_MAP()
        SDV_PARAM_ENTRY(m_Number, "number", -1, "", "A number")
    END_SDV_PARAM_MAP()

    /**
     * @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
     * @return Returns 'true' when the initialization was successful, 'false' when not.
     */
    virtual bool OnInitialize() override
    {
        return true;
    }

    /**
     * @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
     */
    virtual void OnShutdown() override
    {}

    /**
    * @brief Show messages, implements the function of IShowExample
    */
    void SayHello() override
    {
        std::cout << "Hello from Hello_Component_With_Initialization ... " << std::to_string(m_Number) << std::endl;
    }

    /**
    * @brief Show messages, implements the function of ISayGoodbye
    */
    void SayGoodbye() override
    {
        std::cout << "Goodbye from Hello_Component_With_Initialization ... " << std::to_string(m_Number) << std::endl;
    }

private:
    int32_t m_Number = -1;
};

DEFINE_SDV_OBJECT(CTestComponentWithInitialization)

In OnInitialize() the configuration file can be parsed if required. For details see Example of the config files.

Attention

See Example of the config files to write a valid TOML-like structure for the object initialization.

Note

# Version of the Configuration for the application
[Configuration]
Version = 100

# Configuration including the example number = 7
[[Component]]
Path = "example_general_component_with_initialization.sdv"
Class = "Hello_Component_With_Initialization"
[Component.Parameters]
number = 7