Files
pvxs/documentation/typedef.rst
T
2023-03-20 09:41:41 -07:00

1.8 KiB

Defining Custom Types

Interface through which type definitions are created and extended for use with pvxs::Value. :

#include <pvxs/data.h>
namespace pvxs { ... }

The type system of the PVA protocol consists of a number of primitive and compound types, as well as arrays of either. The pvxs::TypeCode::code_t enum is an exhaustive list of all valid types, as well as Null.

Any valid type may be instanciate directly.

Value v = TypeDef{TypeCode::String}.create();
v = "works, but not very interesting";

In almost all cases, custom type definitions will be for Structures. When defining structures, the functions of the pvxs::members namespace can help to keep definitions concise.

namespace M = pvxs::members;

Value v = TypeDef(TypeCode::Struct, {
    M::Int32("ifield"),
    M::Float64("fval"),
    M::Struct("substruct", {
        M::String("desc"),
    }),
}).create();

It is also possible to extend definitions.

namespace M = pvxs::members;

TypeDef def(TypeCode::Struct, {
    M::Int32("ifield"),
    M::Struct("substruct", {
        M::String("desc"),
    }),
});

def += {M::Float64("fval")};
Value v(def.create());

This also applies to the ntapi allowing fields to be added. eg. adding a string field "display.notes".

namespace M = pvxs::members;

TypeDef def = nt::NTScalar{TypeCode::UInt32, true}.build();

def += {M::Struct("display", {M::String("notes")})};
Value v(def.create());

pvxs::TypeCode

pvxs::TypeDef

pvxs::Member

pvxs::members

pvxs::Kind