Program Listing for File condition.hpp
↰ Return to documentation for file (include/holoscan/core/condition.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HOLOSCAN_CORE_CONDITION_HPP
#define HOLOSCAN_CORE_CONDITION_HPP
#include <gxf/core/gxf.h>
#include <yaml-cpp/yaml.h>
#include <any>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include "./common.hpp"
#include "./component.hpp"
#include "./gxf/gxf_component.hpp"
#include "./gxf/gxf_utils.hpp"
#include "gxf/std/scheduling_condition.hpp"
#include "gxf/std/scheduling_terms.hpp" // for AsynchronousEventState
#define HOLOSCAN_CONDITION_FORWARD_TEMPLATE() \
template <typename ArgT, \
typename... ArgsT, \
typename = \
std::enable_if_t<!std::is_base_of_v<::holoscan::Condition, std::decay_t<ArgT>> && \
(std::is_same_v<::holoscan::Arg, std::decay_t<ArgT>> || \
std::is_same_v<::holoscan::ArgList, std::decay_t<ArgT>>)>>
#define HOLOSCAN_CONDITION_FORWARD_ARGS(class_name) \
HOLOSCAN_CONDITION_FORWARD_TEMPLATE() \
explicit class_name(ArgT&& arg, ArgsT&&... args) \
: Condition(std::forward<ArgT>(arg), std::forward<ArgsT>(args)...) {}
#define HOLOSCAN_CONDITION_FORWARD_ARGS_SUPER(class_name, super_class_name) \
HOLOSCAN_CONDITION_FORWARD_TEMPLATE() \
explicit class_name(ArgT&& arg, ArgsT&&... args) \
: super_class_name(std::forward<ArgT>(arg), std::forward<ArgsT>(args)...) {}
namespace holoscan {
// Forward declarations
class Operator;
class Resource;
using nvidia::gxf::AsynchronousEventState;
// Note: Update `IOSpec::to_yaml_node()` if you add new condition types
enum class ConditionType {
kNone,
kMessageAvailable,
kDownstreamMessageAffordable,
kCount,
kBoolean,
kPeriodic,
kAsynchronous,
kExpiringMessageAvailable,
kMultiMessageAvailable,
kMultiMessageAvailableTimeout,
};
enum class SchedulingStatusType : int32_t {
kNever,
kReady,
kWait,
kWaitTime,
kWaitEvent,
};
class Condition : public Component {
public:
enum class ConditionComponentType {
kNative,
kGXF,
};
Condition() = default;
Condition(Condition&&) = default;
HOLOSCAN_CONDITION_FORWARD_TEMPLATE()
explicit Condition(ArgT&& arg, ArgsT&&... args) {
add_arg(std::forward<ArgT>(arg));
(add_arg(std::forward<ArgsT>(args)), ...);
}
~Condition() override = default;
virtual void check([[maybe_unused]] int64_t timestamp,
[[maybe_unused]] SchedulingStatusType* status_type,
[[maybe_unused]] int64_t* target_timestamp) const {
// empty implementation (only used for native Conditions)
throw std::logic_error("check method not implemented");
}
virtual void on_execute([[maybe_unused]] int64_t timestamp) {
// empty implementation (only used for native Conditions)
throw std::logic_error("on_execute method not implemented");
}
virtual void update_state([[maybe_unused]] int64_t timestamp) {
// empty implementation (only used for native Conditions)
}
ConditionComponentType condition_type() const { return condition_type_; }
using Component::name;
Condition& name(const std::string& name) & {
name_ = name;
return *this;
}
Condition&& name(const std::string& name) && {
name_ = name;
return std::move(*this);
}
using Component::fragment;
Condition& fragment(Fragment* fragment) {
fragment_ = fragment;
return *this;
}
Condition& spec(const std::shared_ptr<ComponentSpec>& spec) {
spec_ = spec;
return *this;
}
ComponentSpec* spec() { return spec_.get(); }
std::shared_ptr<ComponentSpec> spec_shared() { return spec_; }
using Component::add_arg;
void add_arg(const std::shared_ptr<Resource>& arg);
void add_arg(std::shared_ptr<Resource>&& arg);
std::unordered_map<std::string, std::shared_ptr<Resource>>& resources() { return resources_; }
virtual void setup([[maybe_unused]] ComponentSpec& spec) {}
void initialize() override;
YAML::Node to_yaml_node() const override;
std::optional<std::shared_ptr<Receiver>> receiver(const std::string& port_name);
std::optional<std::shared_ptr<Transmitter>> transmitter(const std::string& port_name);
gxf_uid_t wrapper_cid() const;
protected:
// Add friend classes that can call reset_graph_entites
friend class holoscan::Operator;
using Component::reset_graph_entities;
using ComponentBase::update_params_from_args;
void set_operator(Operator* op) { op_ = op; }
void wrapper_cid(gxf_uid_t cid);
void update_params_from_args();
virtual void set_parameters();
bool is_initialized_ = false;
std::unordered_map<std::string, std::shared_ptr<Resource>>
resources_;
ConditionComponentType condition_type_ =
ConditionComponentType::kNative;
Operator* op_ = nullptr;
gxf_uid_t wrapper_cid_ = 0;
};
} // namespace holoscan
#endif/* HOLOSCAN_CORE_CONDITION_HPP */