ADTF
Loading...
Searching...
No Matches
Example Demo User Defined Properties

Description

Shows how to create own data types.

Prebuilt Binaries

  • ./bin/(debug)/demo_user_defined_properties(.exe)

Source Code

./examples/demo_adtfplugins/demo_user_defined_properties/

Demo Application

demo_user_defined_properties.cpp

#include "stdafx.h"
#include <adtf_base.h>
// example struct to store a limited discrete 2D point to a user defined property
struct cPoint2D
{
cPoint2D()
{
m_x = 0;
m_y = 0;
}
public:
int32_t m_x;
int32_t m_y;
};
// every new property type needs convert functions for serialization and memory handling
struct cConvertPoint2D
{
// This is the general serialization/copy method. ToString is called on the source property and
// FromString is called for the destination property.
static tResult ToString(const cPoint2D& oFromValue, adtf_util::cString& strToString)
{
strToString.Append(adtf_util::cString::FromType(oFromValue.m_x));
strToString.Append(" ; ");
strToString.Append(adtf_util::cString::FromType(oFromValue.m_y));
}
static tResult FromString(const adtf_util::cString& strFromString, cPoint2D& oToValue)
{
adtf_util::cStringList vecStrings;
strFromString.Split(vecStrings, ";");
if (vecStrings.GetItemCount() == 2)
{
adtf_util::cStringUtil::ToType(vecStrings[0], oToValue.m_x);
adtf_util::cStringUtil::ToType(vecStrings[1], oToValue.m_y);
}
else { RETURN_ERROR(ERR_INVALID_ARG); }
}
// this is a shortcut to perform a binary copy of a property of your type to another property of your type.
// If you do not want to or cannot support this mode, simply return ERR_NOERROR and leave oToMem untouched.
// pData is a pointer to the adress of the property value of your type.
static tResult ToRaw(const void* pData, const size_t szDataSize, adtf::base::IRawMemory& oToMem)
{
return oToMem.Set(pData, szDataSize);
}
// this is the counterpart to ToRaw. Use this to initiallize a property of your type from binary data that you provided
// in ToRaw. Once again pData points to your object of your data type.
static tResult FromRaw(const adtf::base::ant::IRawMemory& oFromMem, void* pData, const size_t szDataSize)
{
if (oFromMem.GetSize() == szDataSize)
{
adtf_util::cMemoryBlock::MemCopy(pData, oFromMem.Get(), szDataSize);
}
RETURN_ERROR(ERR_MEMORY);
}
// defines how to convert IPropertyValues (of any type) to a cPoint2D
static tResult Convert(const adtf::base::IPropertyValue& oProperty, cPoint2D& oToValue)
{
adtf_util::cString strTypeOfValue;
RETURN_IF_FAILED(oProperty.GetType(adtf_string_intf(strTypeOfValue)));
if (!strTypeOfValue.Compare("cString",0,7))
{
adtf_util::cString strValue;
RETURN_IF_FAILED(oProperty.ToString(adtf_string_intf(strValue)));
RETURN_IF_FAILED(cConvertPoint2D::FromString(strValue, oToValue));
}
// only cString type supported in example
RETURN_ERROR(ERR_NOT_IMPL);
}
};
//using namespace adtf::base::ant;
namespace adtf
{
namespace base
{
namespace ant
{
// class, convert functions and a unique name have to be combined as specialization of property_type_definition<>
template < >
struct property_type_definition<cPoint2D>
{
static constexpr const tChar* TYPE_NAME = "cPoint2D";
typedef cConvertPoint2D con_type;
};
}}}
int main(int /* argc */, char * /* argv */[])
{
// generate a new object and add it as an property to our property list
cPoint2D oNewPoint2D;
adtf::base::property<cPoint2D> propertyNewPoint2D("TheNameOfTheProperty", oNewPoint2D);
oMyProps.SetProperty(propertyNewPoint2D);
// currently the property has init values (0/0)
// check this by displaying
{
oMyProps.GetProperty("TheNameOfTheProperty", oProperty);
std::cout << std::endl << "Old Point2D: x=" << oProperty.GetValueT().m_x << " / y=" << oProperty.GetValueT().m_y << std::endl;
}
// modify property outside user code i.e. Configuration Editor
// in this example we serialize the property, change its string representation manually and deserialize back to Point2D property
{
// find and get the property by name as an cString (serialized version of data)
oMyProps.GetProperty("TheNameOfTheProperty", oStringProperty);
std::cout << std::endl << "Point2D as cString: \"" << oStringProperty.GetValueT() <<"\"" << std::endl;
// manually modify the string and write it back to property list
oStringProperty = " 23;42 ";
std::cout << std::endl << "Modified cString: \"" << oStringProperty.GetValueT() << "\"" << std::endl ;
oMyProps.SetProperty(oStringProperty);
// remark: the property is now of type adtf_util::cString
}
// retrieve property data for further processing
// here: just display new values
{
// Note: the property was converted to cString above
// by getting property as cPoint2D type
// it will be internally converted using cConvertPoint2D.Convert()
oMyProps.GetProperty("TheNameOfTheProperty", oProperty);
std::cout << std::endl << "New Point2D: x=" << oProperty.GetValueT().m_x << " / y=" << oProperty.GetValueT().m_y << std::endl;
}
return 0;
}
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
virtual tResult ToString(IString &&strIToString) const =0
virtual tResult GetType(IString &&strType) const =0
virtual size_t GetSize() const =0
virtual tResult Set(const void *pValue, size_t szSize)=0
virtual const void * Get() const =0
Definition properties_v2.h:397
Definition properties_v2.h:283
Namespace for all functionality of the ADTF Base SDK provided since v3.0.
Definition adtf_class_version.h:16
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
#define adtf_string_intf(__string__)
Definition string_intf.h:465
Definition property_type_definitions.h:38