# Class Operator

## Class Documentation

class Operator : public holoscan::Component

Base class for all operators.

An operator is the most basic unit of work in Holoscan SDK. An Operator receives streaming data at an input port, processes it, and publishes it to one of its output ports.

This class is the base class for all operators. It provides the basic functionality for all operators.

Note

This class is not intended to be used directly. Inherit from this class to create a new operator.

Subclassed by holoscan::ops::GXFOperator

Public Types

enum class OperatorType

Operator type used by the executor.

Values:

enumerator kNative

Native operator.

enumerator kGXF

GXF operator.

Public Functions

template<typename ArgT, typename ...ArgsT, typename = std::enable_if_t<!std::is_base_of_v<Operator, std::decay_t<ArgT>> && (std::is_same_v<Arg, std::decay_t<ArgT>> || std::is_same_v<ArgList, std::decay_t<ArgT>> || std::is_base_of_v<holoscan::Condition, typename holoscan::type_info<ArgT>::derived_type> || std::is_base_of_v<holoscan::Resource, typename holoscan::type_info<ArgT>::derived_type>)>>
inline explicit Operator(ArgT &&arg, ArgsT&&... args)

Construct a new Operator object.

Parameters

args – The arguments to be passed to the operator.

Operator() = default

~Operator() override = default

inline OperatorType operator_type() const

Get the operator type.

Returns

The operator type.

inline Operator &id(int64_t id)

Set the Operator ID.

Parameters

id – The ID of the operator.

Returns

The reference to this operator.

inline Operator &name(const std::string &name)

Set the name of the operator.

Parameters

name – The name of the operator.

Returns

The reference to this operator.

inline Operator &fragment(Fragment *fragment)

Set the fragment of the operator.

Parameters

fragment – The pointer to the fragment of the operator.

Returns

The reference to this operator.

inline Operator &spec(const std::shared_ptr<OperatorSpec> &spec)

Set the operator spec.

Parameters

spec – The operator spec.

Returns

The reference to this operator.

inline OperatorSpec *spec()

Get the operator spec.

Returns

The operator spec.

inline std::shared_ptr<OperatorSpec> spec_shared()

Get the shared pointer to the operator spec.

Returns

The shared pointer to the operator spec.

template<typename ConditionT>
inline std::shared_ptr<ConditionT> condition(const std::string &name)

Get a shared pointer to the Condition object.

Parameters

name – The name of the condition.

Returns

The reference to the Condition object. If the condition does not exist, return the nullptr.

inline std::unordered_map<std::string, std::shared_ptr<Condition>> &conditions()

Get the conditions of the operator.

Returns

The conditions of the operator.

inline std::unordered_map<std::string, std::shared_ptr<Resource>> &resources()

Get the resources of the operator.

Returns

The resources of the operator.

Add a condition to the operator.

Parameters

arg – The condition to add.

Add a condition to the operator.

Parameters

arg – The condition to add.

Add a resource to the operator.

Parameters

arg – The resource to add.

Add a resource to the operator.

Parameters

arg – The resource to add.

inline virtual void setup(OperatorSpec &spec)

Define the operator specification.

Parameters

spec – The reference to the operator specification.

virtual void initialize() override

Initialize the operator.

This function is called after the operator is created by holoscan::Fragment::make_operator().

inline virtual void start()

Implement the startup logic of the operator.

This method is called multiple times over the lifecycle of the operator according to the order defined in the lifecycle, and used for heavy initialization tasks such as allocating memory resources.

inline virtual void stop()

Implement the shutdown logic of the operator.

This method is called multiple times over the lifecycle of the operator according to the order defined in the lifecycle, and used for heavy deinitialization tasks such as deallocation of all resources previously assigned in start.

inline virtual void compute(InputContext &op_input, OutputContext &op_output, ExecutionContext &context)

Implement the compute method.

This method is called by the runtime multiple times. The runtime calls this method until the operator is stopped.

Parameters
• op_input – The input context of the operator.

• op_output – The output context of the operator.

• context – The execution context of the operator.

inline int64_t id() const

Get the identifier of the component.

By default, the identifier is set to -1. It is set to a valid value when the component is initialized.

With the default executor (GXFExecutor), the identifier is set to the GXF component ID.

Returns

The identifier of the component.

inline const std::string &name() const

Get the name of the component.

Returns

The name of the component.

inline Fragment *fragment()

Get a pointer to Fragment object.

Returns

The Pointer to Fragment object.

Add an argument to the component.

Parameters

arg – The argument to add.

Add an argument to the component.

Parameters

arg – The argument to add.

Add a list of arguments to the component.

Parameters

arg – The list of arguments to add.

Add a list of arguments to the component.

Parameters

arg – The list of arguments to add.

Public Static Functions

template<typename typeT>
static inline void register_converter()

Register the argument setter for the given type.

If the operator has an argument with a custom type, the argument setter must be registered using this method.

The argument setter is used to set the value of the argument from the YAML configuration.

This method can be called in the initialization phase of the operator (e.g., initialize()). The example below shows how to register the argument setter for the custom type (Vec3):

Copy
Copied!

void MyOp::initialize() {
register_converter<Vec3>();
}


It is assumed that YAML::convert<T>::encode and YAML::convert<T>::decode are implemented for the given type. You need to specialize the YAML::convert<> template class.

For example, suppose that you had a Vec3 class with the following members:

Copy
Copied!

struct Vec3 {
// make sure you have overloaded operator==() for the comparison
double x, y, z;
};


You can define the YAML::convert<Vec3> as follows in a ‘.cpp’ file:

Copy
Copied!

namespace YAML {
template<>
struct convert<Vec3> {
static Node encode(const Vec3& rhs) {
Node node;
node.push_back(rhs.x);
node.push_back(rhs.y);
node.push_back(rhs.z);
return node;
}

static bool decode(const Node& node, Vec3& rhs) {
if(!node.IsSequence() || node.size() != 3) {
return false;
}

rhs.x = node[0].as<double>();
rhs.y = node[1].as<double>();
rhs.z = node[2].as<double>();
return true;
}
};
}


Please refer to the yaml-cpp documentation for more details.

Template Parameters

typeT – The type of the argument to register.

Protected Attributes

OperatorType operator_type_ = OperatorType::kNative

The type of the operator.

std::shared_ptr<OperatorSpec> spec_

The operator spec of the operator.

std::unordered_map<std::string, std::shared_ptr<Condition>> conditions_

The conditions of the operator.

std::unordered_map<std::string, std::shared_ptr<Resource>> resources_

The resources used by the operator.

Protected Static Functions

template<typename typeT>
static inline void register_argument_setter()

Register the argument setter for the given type.

Please refer to the documentation of register_converter() for more details.

Template Parameters

typeT – The type of the argument to register.