ADTF
Loading...
Searching...
No Matches
Example Demo String Handling

Description

Shows how to transmit and receive samples containing strings.

Prebuilt Binaries

Source Code

./examples/demo_adtfplugins/demo_string_handling/

Receiver

demo_string_receiving_filter.h

#pragma once
#ifndef ADTF_EXAMPLES_CID
#define ADTF_EXAMPLES_CID ".local.cid"
#endif
class cDemoStringReceiving final : public adtf::filter::cFilter
{
public:
ADTF_CLASS_ID_NAME(cDemoStringReceiving,
"demo_string_receiving.filter" ADTF_EXAMPLES_CID,
"Demo String Receiving");
public:
cDemoStringReceiving();
~cDemoStringReceiving() override;
//override the process method to react on the data trigger
private:
// reader for an input string
adtf::streaming::ISampleReader* m_pReaderString = nullptr;
};
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define ADTF_CLASS_ID_NAME(_class, _strcid, _strclabel)
Definition class_id.h:33
Definition filter.h:289
virtual tResult ProcessInput(base::flash::tNanoSeconds tmTrigger, streaming::flash::ISampleReader *pReader)
Definition samplestreamer_intf.h:188
ant::iobject_ptr< T > iobject_ptr
Alias always bringing the latest version of ant::iobject_ptr into scope.
Definition object_ptr_intf.h:437

demo_string_receiving_filter.cpp

#include "demo_string_receiving_filter.h"
using namespace adtf::util;
using namespace adtf::ucom;
using namespace adtf::base;
using namespace adtf::streaming;
using namespace adtf::filter;
cDemoStringReceiving::cDemoStringReceiving()
{
SetDescription("Use this filter to receive samples with string content via input pin. It used the stream_meta_type_string.");
// create a pin to receive the content of a std::string
m_pReaderString = CreateInputPin("in_string", stream_type_string<std::string>());
SetDescription("in_string", "Handles an incoming adtf/string type");
// set help link to jump to documentation from ADTF Configuration Editor
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_string_receiving.html");
}
cDemoStringReceiving::~cDemoStringReceiving() = default;
//this function will be executed each time a data trigger occured
tResult cDemoStringReceiving::ProcessInput(ISampleReader* pReader,
{
if (pReader == m_pReaderString)
{
LOG_INFO("Received string: %s", sample_data<std::string>(pSample)->c_str());
}
}
#define LOG_INFO(...)
Logs an info message.
Definition log.h:388
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
Definition sample_data.h:429
Generator template to create an instance of a ant::IStreamType class for penguin::stream_meta_type_st...
Definition streammetatypestring.h:277
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
Namespace for the ADTF Filter SDK.
Definition configurable_runner.h:18
Namespace for the ADTF Streaming SDK.
Definition bindingproxyinport.h:14
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324

Sender

demo_string_sending_filter.h

#pragma once
#ifndef ADTF_EXAMPLES_CID
#define ADTF_EXAMPLES_CID ".local.cid"
#endif
class cDemoStringSending final : public adtf::filter::cFilter
{
public:
ADTF_CLASS_ID_NAME(cDemoStringSending,
"demo_string_sending.filter" ADTF_EXAMPLES_CID,
"Demo String Sending");
public:
cDemoStringSending();
~cDemoStringSending() override;
adtf::streaming::IRunner* pRunner) override;
private:
// writer to an output pin
adtf::streaming::sample_writer<std::string>* m_pWriterString = nullptr;
adtf::streaming::sample_writer<std::u16string>* m_pWriterU16String = nullptr;
};
virtual tResult Process(base::flash::tNanoSeconds tmTrigger, streaming::ant::IRunner *pRunner)
Definition runner_intf.h:24
Definition samplewriter.h:499
A timestamp with nanosecond precision.
Definition chrono.h:23

demo_string_sending_filter.cpp

#include "demo_string_sending_filter.h"
#include <codecvt>
#include <iostream>
#include <locale>
#include <sstream>
using namespace adtf::util;
using namespace adtf::ucom;
using namespace adtf::base;
using namespace adtf::streaming;
using namespace adtf::filter;
cDemoStringSending::cDemoStringSending()
{
SetDescription("Use this filter to send samples with string content via output pin. It uses the stream_type_string.");
// create the pin to send the content of a std::string
m_pWriterString = CreateOutputPin<pin_writer<std::string>>("out_string", stream_type_string<std::string>());
SetDescription("out_string", "Provides a string with the current time");
// create the pin to send the content of a std::wstring
m_pWriterU16String
= CreateOutputPin<pin_writer<std::u16string>>("out_16string", stream_type_string<std::u16string>());
SetDescription("out_16string", "Provides a u16string with the current time");
// create a trigger function that can be connected to an active runner.
CreateRunner("string_generator_function", cTimerTriggerHint(std::chrono::seconds {1}));
SetDescription("string_generator_function", "Runner to periodically trigger the function which generates the output data for all pins");
// set help link to jump to documentation from ADTF Configuration Editor
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_string_sending.html");
}
cDemoStringSending::~cDemoStringSending() = default;
namespace
{
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
std::u16string convert_to_u16string(const std::string& strValue)
{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> oConverter;
return oConverter.from_bytes(strValue);
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
template<typename T>
std::u16string to_u16string(const T& value)
{
return convert_to_u16string(std::to_string(value));
}
} // namespace
//this function will be executed each time a trigger occured
tResult cDemoStringSending::Process(tNanoSeconds tmTimeOfTrigger,
IRunner* /*pRunner*/)
{
{
std::stringstream strTimeToSend;
strTimeToSend << "Hello, current trigger time is : " << tmTimeOfTrigger.nCount << " nanoseconds";
RETURN_IF_FAILED(m_pWriterString->Write(tmTimeOfTrigger, strTimeToSend.str()));
}
{
std::u16string strTimeToSend = u"Hello, current trigger time is : " + to_u16string(tmTimeOfTrigger.nCount) + u" nanoseconds"
+ u" with umlauts :" + u"\u00e4" + u" " + u"\u00f6" + u" " + u"\u00fc";
RETURN_IF_FAILED(m_pWriterU16String->Write(tmTimeOfTrigger, strTimeToSend));
}
}

Plugin

plugin.cpp

#include "demo_string_sending_filter.h"
#include "demo_string_receiving_filter.h"
ADTF_PLUGIN("Demo String Handling Filters Plugin",
cDemoStringSending,
cDemoStringReceiving);
#define ADTF_PLUGIN(__plugin_identifier,...)
Definition adtf_plugin.h:29