Example access to other components#
Access to other components is really simple.
#include <iostream>
#include <stdexcept>
#include "example_interfaces.h"
#include <support/component_impl.h>
class CAccessComponent
: public sdv::CSdvObject
, public IShowExample
{
public:
CAccessComponent()
{
m_HelloInterface = sdv::core::GetObject("Hello_Component").GetInterface<ISayHello>();
sdv::TInterfaceAccessPtr helloComp = sdv::core::GetObject("Hello_Component");
m_GoodbyeInterface = helloComp.GetInterface<ISayGoodbye>();
if (!m_HelloInterface || !m_GoodbyeInterface)
{
throw std::runtime_error("Could not get all needed interfaces!");
}
}
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(IShowExample)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_NAME("Access_Component")
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::BasicService);
/**
* @brief Show messages, implements the function of IShowExample
*/
void Show() override
{
m_HelloInterface->SayHello();
m_GoodbyeInterface->SayGoodbye();
}
private:
ISayHello * m_HelloInterface = nullptr;
ISayGoodbye * m_GoodbyeInterface = nullptr;
};
DEFINE_SDV_OBJECT(CAccessComponent)
#include <iostream>
#include <stdexcept>
#include "example_interfaces.h"
#include <support/component_impl.h>
class CAccessComponent
: public sdv::CSdvObject
, public IShowExample
{
public:
CAccessComponent()
{
m_HelloInterface = sdv::core::GetObject("Hello_Component").GetInterface<ISayHello>();
sdv::TInterfaceAccessPtr helloComp = sdv::core::GetObject("Hello_Component");
m_GoodbyeInterface = helloComp.GetInterface<ISayGoodbye>();
if (!m_HelloInterface || !m_GoodbyeInterface)
{
throw std::runtime_error("Could not get all needed interfaces!");
}
}
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(IShowExample)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_NAME("Access_Component")
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::BasicService);
/**
* @brief Show messages, implements the function of IShowExample
*/
void Show() override
{
m_HelloInterface->SayHello();
m_GoodbyeInterface->SayGoodbye();
}
private:
ISayHello * m_HelloInterface = nullptr;
ISayGoodbye * m_GoodbyeInterface = nullptr;
};
DEFINE_SDV_OBJECT(CAccessComponent)
When interfaces are needed for more than a one-time use it is a good idea to store them in member variables to reduce the overhead of fetching the interface.
#include <iostream>
#include <stdexcept>
#include "example_interfaces.h"
#include <support/component_impl.h>
class CAccessComponent
: public sdv::CSdvObject
, public IShowExample
{
public:
CAccessComponent()
{
m_HelloInterface = sdv::core::GetObject("Hello_Component").GetInterface<ISayHello>();
sdv::TInterfaceAccessPtr helloComp = sdv::core::GetObject("Hello_Component");
m_GoodbyeInterface = helloComp.GetInterface<ISayGoodbye>();
if (!m_HelloInterface || !m_GoodbyeInterface)
{
throw std::runtime_error("Could not get all needed interfaces!");
}
}
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(IShowExample)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_NAME("Access_Component")
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::BasicService);
/**
* @brief Show messages, implements the function of IShowExample
*/
void Show() override
{
m_HelloInterface->SayHello();
m_GoodbyeInterface->SayGoodbye();
}
private:
ISayHello * m_HelloInterface = nullptr;
ISayGoodbye * m_GoodbyeInterface = nullptr;
};
DEFINE_SDV_OBJECT(CAccessComponent)
With the sdv::core::GetObject
function one gets the sdv::core::IInterfaceAccessPtr
of the object with the given name (see the Object Name highlight at How to create a general VAPI Component).
#include <iostream>
#include <stdexcept>
#include "example_interfaces.h"
#include <support/component_impl.h>
class CAccessComponent
: public sdv::CSdvObject
, public IShowExample
{
public:
CAccessComponent()
{
m_HelloInterface = sdv::core::GetObject("Hello_Component").GetInterface<ISayHello>();
sdv::TInterfaceAccessPtr helloComp = sdv::core::GetObject("Hello_Component");
m_GoodbyeInterface = helloComp.GetInterface<ISayGoodbye>();
if (!m_HelloInterface || !m_GoodbyeInterface)
{
throw std::runtime_error("Could not get all needed interfaces!");
}
}
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(IShowExample)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_NAME("Access_Component")
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::BasicService);
/**
* @brief Show messages, implements the function of IShowExample
*/
void Show() override
{
m_HelloInterface->SayHello();
m_GoodbyeInterface->SayGoodbye();
}
private:
ISayHello * m_HelloInterface = nullptr;
ISayGoodbye * m_GoodbyeInterface = nullptr;
};
DEFINE_SDV_OBJECT(CAccessComponent)
With the GetInterface
templated function one can get access to the interfaces of an IInterfaceAccessPtr
object.
#include <iostream>
#include <stdexcept>
#include "example_interfaces.h"
#include <support/component_impl.h>
class CAccessComponent
: public sdv::CSdvObject
, public IShowExample
{
public:
CAccessComponent()
{
m_HelloInterface = sdv::core::GetObject("Hello_Component").GetInterface<ISayHello>();
sdv::TInterfaceAccessPtr helloComp = sdv::core::GetObject("Hello_Component");
m_GoodbyeInterface = helloComp.GetInterface<ISayGoodbye>();
if (!m_HelloInterface || !m_GoodbyeInterface)
{
throw std::runtime_error("Could not get all needed interfaces!");
}
}
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(IShowExample)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_NAME("Access_Component")
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::BasicService);
/**
* @brief Show messages, implements the function of IShowExample
*/
void Show() override
{
m_HelloInterface->SayHello();
m_GoodbyeInterface->SayGoodbye();
}
private:
ISayHello * m_HelloInterface = nullptr;
ISayGoodbye * m_GoodbyeInterface = nullptr;
};
DEFINE_SDV_OBJECT(CAccessComponent)
Since GetInterface
returns nullptr
if the given interface is not part of the object or if the object could not be found, it is recommended to check for nullptr
s before continuing.
#include <iostream>
#include <stdexcept>
#include "example_interfaces.h"
#include <support/component_impl.h>
class CAccessComponent
: public sdv::CSdvObject
, public IShowExample
{
public:
CAccessComponent()
{
m_HelloInterface = sdv::core::GetObject("Hello_Component").GetInterface<ISayHello>();
sdv::TInterfaceAccessPtr helloComp = sdv::core::GetObject("Hello_Component");
m_GoodbyeInterface = helloComp.GetInterface<ISayGoodbye>();
if (!m_HelloInterface || !m_GoodbyeInterface)
{
throw std::runtime_error("Could not get all needed interfaces!");
}
}
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(IShowExample)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_NAME("Access_Component")
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::BasicService);
/**
* @brief Show messages, implements the function of IShowExample
*/
void Show() override
{
m_HelloInterface->SayHello();
m_GoodbyeInterface->SayGoodbye();
}
private:
ISayHello * m_HelloInterface = nullptr;
ISayGoodbye * m_GoodbyeInterface = nullptr;
};
DEFINE_SDV_OBJECT(CAccessComponent)
To use an interface one just calls the appropriate functions defined by it, since it is no more than a pointer to its type.
Note
For objects to be accessible they need to be declared in the Config File which has to be loaded by sdv::core:CVAPIControl::Initialize
in a VAPI Core Process. See also the Example of a Config File.