> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/holoscan/sdk-user-guide/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/holoscan/sdk-user-guide/_mcp/server.

# holoscan::OutputContext

> Class to hold the output context.

Class to hold the output context.

This class provides the interface to send data to the output ports of the operator.

```cpp showLineNumbers={false}
#include <holoscan/io_context.hpp>
```

---

## Constructors

### OutputContext \[#outputcontext]

#### Construct a new \`OutputContext\` object

```cpp showLineNumbers={false}
holoscan::OutputContext::OutputContext(
    ExecutionContext *execution_context,
    Operator *op
)
```

Construct a new `OutputContext` object.

outputs for the `OutputContext` will be set to op->spec()->outputs()

**Parameters**

**`execution_context`** `ExecutionContext *`

The pointer to the execution context.

---

**`op`** `Operator *`

The pointer to the operator that this context is associated with.

---

#### Construct a new \`OutputContext\` object (with outputs)

```cpp showLineNumbers={false}
holoscan::OutputContext::OutputContext(
    ExecutionContext *execution_context,
    Operator *op,
    std::unordered_map<std::string, std::shared_ptr<IOSpec>> &outputs
)
```

Construct a new `OutputContext` object.

**Parameters**

**`execution_context`** `ExecutionContext *`

The pointer to the execution context.

---

**`op`** `Operator *`

The pointer to the operator that this context is associated with.

---

**`outputs`** `std::unordered_map<std::string, std::shared_ptr<IOSpec>> &`

The references to the map of the output specs.

---

### Destructor \[#destructor]

### \~OutputContext

```cpp showLineNumbers={false}
virtual holoscan::OutputContext::~OutputContext() = default
```

---

## Methods

### execution\_context \[#executioncontext]

```cpp showLineNumbers={false}
ExecutionContext * holoscan::OutputContext::execution_context() const
```

Get pointer to the execution context.

**Returns:** The pointer to the execution context.

### op \[#op]

```cpp showLineNumbers={false}
Operator * holoscan::OutputContext::op() const
```

Return the operator that this context is associated with.

**Returns:** The pointer to the operator.

### outputs \[#outputs]

```cpp showLineNumbers={false}
std::unordered_map<std::string, std::shared_ptr<IOSpec>> & holoscan::OutputContext::outputs() const
```

Return the reference to the map of the output specs.

**Returns:** The reference to the map of the output specs.

### emit \[#emit]

#### Single item (1)

inline

```cpp showLineNumbers={false}
template <typename DataT,
          typename = std::enable_if_t<holoscan::is_one_of_derived_v<DataT, nvidia::gxf::Entity>>>
void holoscan::OutputContext::emit(
    DataT &data,
    const char *name = nullptr,
    const int64_t acq_timestamp = -1
)
```

Send message data (GXF Entity) to the output port with the given name.

This method is for interoperability with the GXF Codelet.

The object to be sent must be an object with [`holoscan::gxf::Entity`](../namespaces/gxf/classes/entity) type and the output port with the given name must exist.

If the operator has a single output port, the output port name can be omitted.

Example:

**Template parameters**

**`DataT`** `typename`

The type of the data to send. It should be [`holoscan::gxf::Entity`](../namespaces/gxf/classes/entity).

---

**Parameters**

**`data`** `DataT &`

The entity object to send ([`holoscan::gxf::Entity`](../namespaces/gxf/classes/entity)).

---

**`name`** `const char *` — default: nullptr

The name of the output port.

---

**`acq_timestamp`** `const int64_t` — default: -1

The time when the message is acquired. For instance, this would generally be the timestamp of the camera when it captures an image.

---

**Example**

```cpp showLineNumbers={false}
class PingTxOp : public holoscan::ops::GXFOperator {
 public:
  HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(PingTxOp, holoscan::ops::GXFOperator)

  PingTxOp() = default;

  void setup(OperatorSpec& spec) override {
    spec.input<holoscan::gxf::Entity>("in");
    spec.output<holoscan::gxf::Entity>("out");
  }

  void compute(InputContext& op_input, OutputContext& op_output,
               [[maybe_unused]] ExecutionContext& context) override
  {
    // The type of `in_message` is 'holoscan::gxf::Entity'.
    auto in_message = op_input.receive<holoscan::gxf::Entity>("in");
    // The type of `tensor` is 'std::shared_ptr<holoscan::Tensor>'.
    auto tensor = in_message.get<Tensor>();

    // Process with 'tensor' here.
    // ...

    // Create a new message (Entity)
    auto out_message = holoscan::gxf::Entity::New(&context);
    out_message.add(tensor, "tensor");

    // Send the processed message.
    op_output.emit(out_message, "out");
  }
};
```

#### Single item (2)

inline

```cpp showLineNumbers={false}
template <typename DataT,
          typename = std::enable_if_t<!holoscan::is_one_of_derived_v<DataT, nvidia::gxf::Entity>>>
void holoscan::OutputContext::emit(
    DataT data,
    const char *name = nullptr,
    const int64_t acq_timestamp = -1
)
```

Send the message data (std::any) to the output port with the given name.

This method is for interoperability with arbitrary data types.

The object to be sent can be any type except GXF Entity ([holoscan::gxf::Entity](../namespaces/gxf/classes/entity)), and the output port with the given name must exist.

If the operator has a single output port, the output port name can be omitted.

Example:

**Template parameters**

**`DataT`** `typename`

The type of the data to send. It can be any type except GXF Entity ([holoscan::gxf::Entity](../namespaces/gxf/classes/entity)).

---

**Parameters**

**`data`** `DataT`

The entity object to send (as `std::any`).

---

**`name`** `const char *` — default: nullptr

The name of the output port.

---

**`acq_timestamp`** `const int64_t` — default: -1

The time when the message is acquired. For instance, this would generally be the timestamp of the camera when it captures an image.

---

**Example**

```cpp showLineNumbers={false}
class PingTxOp : public holoscan::ops::GXFOperator {
 public:
  HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(PingTxOp, holoscan::ops::GXFOperator)

  PingTxOp() = default;

  void setup(OperatorSpec& spec) override {
    spec.input<holoscan::gxf::Entity>("in");
    spec.output<std::shared_ptr<holoscan::Tensor>>("out");
  }

  void compute(InputContext& op_input, OutputContext& op_output,
               [[maybe_unused]] ExecutionContext& context) override
  {
    // The type of `in_message` is 'holoscan::gxf::Entity'.
    auto in_message = op_input.receive<holoscan::gxf::Entity>("in");
    // The type of `tensor` is 'std::shared_ptr<holoscan::Tensor>'.
    auto tensor = in_message.get<Tensor>();  // type: std::shared_ptr<holoscan::Tensor>

    // Process with 'tensor' here.
    // ...

    // Send the processed tensor.
    op_output.emit(tensor, "out");
  }
};
```

#### Single item (3)

```cpp showLineNumbers={false}
void holoscan::OutputContext::emit(
    holoscan::TensorMap &data,
    const char *name = nullptr,
    const int64_t acq_timestamp = -1
)
```

Send the message data ([holoscan::TensorMap](tensormap)) to the output port with the given name.

This method is for interoperability with [holoscan::TensorMap](tensormap) type.

The output port with the given name must exist.

**Parameters**

**`data`** `holoscan::TensorMap &`

The tensor map object to send (as [`holoscan::TensorMap`](tensormap)).

---

**`name`** `const char *` — default: nullptr

The name of the output port.

---

**`acq_timestamp`** `const int64_t` — default: -1

The time when the message is acquired. For instance, this would generally be the timestamp of the camera when it captures an image.

---

#### Single item (4)

```cpp showLineNumbers={false}
void holoscan::OutputContext::emit(
    const std::shared_ptr<holoscan::Tensor> &data,
    const char *name = nullptr,
    const int64_t acq_timestamp = -1
)
```

Send the message data (std::shared\_ptr\<holoscan::Tensor>) to the output port with the given name.

This method does a conversion to std::shared\_ptr\<nvidia::gxf::Tensor> (without copying the tensor's data). Using nvidia::gxf::Tensor is necessary for serialization of tensors when sending a tensor between fragments of a distributed application.

The output port with the given name must exist.

**Parameters**

**`data`** `const std::shared_ptr<holoscan::Tensor> &`

Shared pointer to [holoscan::Tensor](tensor)

---

**`name`** `const char *` — default: nullptr

The name of the output port.

---

**`acq_timestamp`** `const int64_t` — default: -1

The time when the message is acquired. For instance, this would generally be the timestamp of the camera when it captures an image.

---

### set\_cuda\_stream \[#setcudastream]

```cpp showLineNumbers={false}
virtual void holoscan::OutputContext::set_cuda_stream(
    const cudaStream_t stream,
    const char *output_port_name = nullptr
)
```

Set a CUDA stream to be emitted on a given output port.

When using `receive_cuda_stream`, output ports are automatically configured to emit the operator's internal stream, so this method is typically not needed. Use this method when:

* Using `allocate_cuda_stream` to allocate a stream for a root operator
* Using `receive_cuda_streams` for manual stream handling

This method must be called **before** the corresponding `emit()` call for the port.

**Parameters**

**`stream`** `const cudaStream_t`

The CUDA stream to emit. Must be a Holoscan-managed stream (one returned by `receive_cuda_stream`, `receive_cuda_streams`, or `allocate_cuda_stream`).

---

**`output_port_name`** `const char *` — default: nullptr

The name of the output port. Can be omitted if the operator has only a single output port.

---

### cuda\_object\_handler \[#cudaobjecthandler]

#### Overload 1

```cpp showLineNumbers={false}
std::shared_ptr<CudaObjectHandler> holoscan::OutputContext::cuda_object_handler()
```

Get the CUDA stream/event handler used by this input context.

This [`CudaObjectHandler`](cudaobjecthandler) class is designed primarily for internal use and is not guaranteed to have a stable API. [Application](application) authors should instead rely on the public [`set_cuda_stream`](#setcudastream) method.

#### Overload 2

```cpp showLineNumbers={false}
void holoscan::OutputContext::cuda_object_handler(
    std::shared_ptr<CudaObjectHandler> handler
)
```

Set the CUDA stream handler used by this output context.

### stream\_to\_emit \[#streamtoemit]

```cpp showLineNumbers={false}
virtual std::optional<cudaStream_t> holoscan::OutputContext::stream_to_emit(
    const char *output_port_name = nullptr
)
```

Get the CUDA stream to be emitted on the specified output port.

This method retrieves the CUDA stream that has been configured for emission on the given output port via [`set_cuda_stream`](#setcudastream). The base implementation returns std::nullopt. Derived classes should override this method to provide actual stream information.

**Returns:** Optional CUDA stream. std::nullopt if no stream is configured.

**Parameters**

**`output_port_name`** `const char *` — default: nullptr

The name of the output port.

---

### emit\_impl \[#emitimpl]

```cpp showLineNumbers={false}
virtual void holoscan::OutputContext::emit_impl(
    std::any data,
    const char *name = nullptr,
    OutputType out_type = OutputType::kAny,
    const int64_t acq_timestamp = -1,
    bool omit_data_logging = false,
    bool skip_stream_propagation = false,
    bool is_new_entity = false
)
```

The implementation of the [`emit`](#emit) method.

Depending on the type of the data, this method wraps the data with a message and sends it to the output port with the given name.

**Parameters**

**`data`** `std::any`

The data to send.

---

**`name`** `const char *` — default: nullptr

The name of the output port.

---

**`out_type`** `OutputType` — default: OutputType::kAny

The type of the message data.

---

**`acq_timestamp`** `const int64_t` — default: -1

The timestamp to publish in the output message. The default value of -1 does not publish a timestamp.

---

**`omit_data_logging`** `bool` — default: false

If true, data will not be logged via the [DataLogger](datalogger) interface.

---

**`skip_stream_propagation`** `bool` — default: false

If true, skip propagating CUDA stream to entity memory buffers. Used when the caller has already set the stream on tensors.

---

**`is_new_entity`** `bool` — default: false

If true, the entity was just created (not forwarded), allowing optimizations like skipping checks for existing components.

---

### log\_tensor \[#logtensor]

```cpp showLineNumbers={false}
bool holoscan::OutputContext::log_tensor(
    const std::shared_ptr<Tensor> &tensor,
    const std::string &unique_id,
    const char *port_name
)
```

### log\_tensormap \[#logtensormap]

```cpp showLineNumbers={false}
bool holoscan::OutputContext::log_tensormap(
    const holoscan::TensorMap &tensor_map,
    const std::string &unique_id,
    const char *port_name
)
```

---

## Types

### OutputType

The output data type.

| Name         | Value | Description                               |
| ------------ | ----- | ----------------------------------------- |
| `kGXFEntity` |       | The message data to send is a GXF entity. |
| `kAny`       |       | The message data to send is a std::any.   |

---

## Member variables

| Name                   | Type                                                             | Description                                        |
| ---------------------- | ---------------------------------------------------------------- | -------------------------------------------------- |
| `execution_context_`   | `ExecutionContext *`                                             | The execution context that is associated with.     |
| `op_`                  | `Operator *`                                                     | The operator that this context is associated with. |
| `outputs_`             | `std::unordered_map< std::string, std::shared_ptr< IOSpec > > &` | The outputs.                                       |
| `cuda_object_handler_` | `std::shared_ptr< CudaObjectHandler >`                           |                                                    |