Program Listing for File operator.hpp
↰ Return to documentation for file (include/holoscan/core/operator.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2022 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_OPERATOR_HPP
#define HOLOSCAN_CORE_OPERATOR_HPP
#include <stdio.h>
#include <iostream>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <unordered_map>
#include "./common.hpp"
#include "./arg.hpp"
#include "./argument_setter.hpp"
#include "./component.hpp"
#include "./condition.hpp"
#include "./forward_def.hpp"
#include "./operator_spec.hpp"
#include "./resource.hpp"
#define HOLOSCAN_OPERATOR_FORWARD_TEMPLATE() \
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>)>>
#define HOLOSCAN_OPERATOR_FORWARD_ARGS(class_name) \
HOLOSCAN_OPERATOR_FORWARD_TEMPLATE() \
class_name(ArgT&& arg, ArgsT&&... args) \
: Operator(std::forward<ArgT>(arg), std::forward<ArgsT>(args)...) {}
#define HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(class_name, super_class_name) \
HOLOSCAN_OPERATOR_FORWARD_TEMPLATE() \
class_name(ArgT&& arg, ArgsT&&... args) \
: super_class_name(std::forward<ArgT>(arg), std::forward<ArgsT>(args)...) {}
namespace holoscan {
class Operator : public Component {
public:
enum class OperatorType {
kNative,
kGXF,
};
HOLOSCAN_OPERATOR_FORWARD_TEMPLATE()
explicit Operator(ArgT&& arg, ArgsT&&... args) {
add_arg(std::forward<ArgT>(arg));
(add_arg(std::forward<ArgsT>(args)), ...);
}
Operator() = default;
~Operator() override = default;
OperatorType operator_type() const { return operator_type_; }
using Component::id;
Operator& id(int64_t id) {
id_ = id;
return *this;
}
using Component::name;
Operator& name(const std::string& name) {
name_ = name;
return *this;
}
using Component::fragment;
Operator& fragment(Fragment* fragment) {
fragment_ = fragment;
return *this;
}
Operator& spec(const std::shared_ptr<OperatorSpec>& spec) {
spec_ = spec;
return *this;
}
OperatorSpec* spec() { return spec_.get(); }
std::shared_ptr<OperatorSpec> spec_shared() { return spec_; }
template <typename ConditionT>
std::shared_ptr<ConditionT> condition(const std::string& name) {
if (auto condition = conditions_.find(name); condition != conditions_.end()) {
return std::dynamic_pointer_cast<ConditionT>(condition->second);
}
return nullptr;
}
std::unordered_map<std::string, std::shared_ptr<Condition>>& conditions() { return conditions_; }
std::unordered_map<std::string, std::shared_ptr<Resource>>& resources() { return resources_; }
using Component::add_arg;
void add_arg(const std::shared_ptr<Condition>& arg) { conditions_[arg->name()] = arg; }
void add_arg(std::shared_ptr<Condition>&& arg) { conditions_[arg->name()] = std::move(arg); }
void add_arg(const std::shared_ptr<Resource>& arg) { resources_[arg->name()] = arg; }
void add_arg(std::shared_ptr<Resource>&& arg) { resources_[arg->name()] = std::move(arg); }
virtual void setup(OperatorSpec& spec) { (void)spec; }
void initialize() override;
virtual void start() {
// Empty default implementation
}
virtual void stop() {
// Empty default implementation
}
virtual void compute(InputContext& op_input, OutputContext& op_output,
ExecutionContext& context) {
(void)op_input;
(void)op_output;
(void)context;
}
template <typename typeT>
static void register_converter() {
register_argument_setter<typeT>();
}
protected:
template <typename typeT>
static void register_argument_setter() {
ArgumentSetter::get_instance().add_argument_setter<typeT>(
[](ParameterWrapper& param_wrap, Arg& arg) {
std::any& any_param = param_wrap.value();
std::any& any_arg = arg.value();
// Note that the type of any_param is Parameter
*, not Parameter
.
auto& param = *std::any_cast<Parameter<typeT>*>(any_param);
const auto& arg_type = arg.arg_type();
(void)param;
auto element_type = arg_type.element_type();
auto container_type = arg_type.container_type();
HOLOSCAN_LOG_DEBUG(
"Registering converter for parameter {} (element_type: {}, container_type: {})",
arg.name(),
(int)element_type,
(int)container_type);
if (element_type == ArgElementType::kYAMLNode) {
auto& arg_value = std::any_cast<YAML::Node&>(any_arg);
typeT new_value;
bool parse_ok = YAML::convert<typeT>::decode(arg_value, new_value);
if (!parse_ok) {
HOLOSCAN_LOG_ERROR("Unable to parse YAML node for parameter '{}'", arg.name());
} else {
param = std::move(new_value);
}
} else {
try {
auto& arg_value = std::any_cast<typeT&>(any_arg);
param = arg_value;
} catch (const std::bad_any_cast& e) {
HOLOSCAN_LOG_ERROR(
"Bad any cast exception caught for argument '{}': {}", arg.name(), e.what());
}
}
});
}
OperatorType operator_type_ = OperatorType::kNative;
std::shared_ptr<OperatorSpec> spec_;
std::unordered_map<std::string, std::shared_ptr<Condition>>
conditions_;
std::unordered_map<std::string, std::shared_ptr<Resource>>
resources_;
};
} // namespace holoscan
#endif/* HOLOSCAN_CORE_OPERATOR_HPP */