ADTF  3.18.2
Demo User Defined Property
Description
Implements a simple data type for configuration of filters.
Location
./src/examples/src/adtf/properties/user_defined/
Namespace for entire ADTF SDK.
Build Environment
To see how to set up the build environment have a look at ADTF CMake Environment
this implementation shows:
  • how to create your own data type
  • how to add your own type to a list of properties
  • how to modify data without knowing its real type from outside (i.e. ADTF Configuration Editor)
  • how to import data back to your type
Remarks
  • Every type of a property can be implemented by yourself
  • Properties are defined using the adtf::base::ant::IPropertyValue interface
  • Display or editing properties uses serialization and deserialization to/from adtf_util::cString so it can be manipulated simply in a text box
  • User defined properties must implement the following conversion functions as described in adtf::base::ant::property_type_default_conversion :
    1. Convert (const IPropertyValue &oProp, TYPE &oValue)
    2. ToString (const TYPE &oValue, adtf_util::cString &strValue)
    3. FromString (const adtf_util::cString &strValue, TYPE &oValue)
    4. ToRaw (const void *pData, const size_t szSizeOfData, IRawMemory &oToMem)
    5. FromRaw (const IRawMemory &oFromMem, void *pData, const size_t szSizeOfData)
  • Properties can be added to a adtf::base::ant::cProperties configuration object.
  • See adtf::base::ant::cPropertiesHelper for further details
Implementation
#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))
{
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;
}
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
The IPropertyValue interface provides methods for getting and setting property values.
Definition: property_intf.h:60
virtual tResult ToString(IString &&strIToString) const =0
Implement to serialize the value to a textfile and/or to show it on a display.
virtual tResult GetType(IString &&strType) const =0
Retrieves the string for the property value type.
The IRawMemory interface provides methods for getting and setting memory values through abstract inte...
virtual tResult Set(const void *pValue, size_t szSize)=0
Sets the Raw pointer memory.
virtual size_t GetSize() const =0
Returns the size in bytes of the memory.
virtual const void * Get() const =0
Returns the raw memory pointer.
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2778
#define adtf_string_intf(__string__)
The adtf_string_intf Macro helps to easily create a rvalue reference of a adtf::util::cString.
Definition: string_intf.h:371