Program Listing for File operator_spec.hpp
↰ Return to documentation for file (include/holoscan/core/operator_spec.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_SPEC_HPP
#define HOLOSCAN_CORE_OPERATOR_SPEC_HPP
#include <iostream>
#include <memory>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#include "./component_spec.hpp"
#include "./io_spec.hpp"
#include "./common.hpp"
namespace holoscan {
class OperatorSpec : public ComponentSpec {
public:
explicit OperatorSpec(Fragment* fragment = nullptr) : ComponentSpec(fragment) {}
std::unordered_map<std::string, std::unique_ptr<IOSpec>>& inputs() { return inputs_; }
template <typename DataT>
IOSpec& input() {
return input<DataT>("__iospec_input");
}
template <typename DataT>
IOSpec& input(std::string name) {
auto spec = std::make_unique<IOSpec>(this, name, IOSpec::IOType::kInput, &typeid(DataT));
auto [iter, is_exist] = inputs_.insert_or_assign(name, std::move(spec));
if (!is_exist) { HOLOSCAN_LOG_ERROR("Input port '{}' already exists", name); }
if (outputs_.find(name) != outputs_.end()) {
HOLOSCAN_LOG_WARN(
"Output port name '{}' conflicts with the input port name '{}'", name, name);
}
return *(iter->second.get());
}
std::unordered_map<std::string, std::unique_ptr<IOSpec>>& outputs() { return outputs_; }
template <typename DataT>
IOSpec& output() {
return output<DataT>("__iospec_output");
}
template <typename DataT>
IOSpec& output(std::string name) {
auto spec = std::make_unique<IOSpec>(this, name, IOSpec::IOType::kOutput, &typeid(DataT));
auto [iter, is_exist] = outputs_.insert_or_assign(name, std::move(spec));
if (!is_exist) { HOLOSCAN_LOG_ERROR("Output port '{}' already exists", name); }
if (inputs_.find(name) != inputs_.end()) {
HOLOSCAN_LOG_WARN(
"Input port name '{}' conflicts with the output port name '{}'", name, name);
}
return *(iter->second.get());
}
using ComponentSpec::param;
void param(Parameter<holoscan::IOSpec*>& parameter, const char* key, const char* headline,
const char* description) {
parameter.key_ = key;
parameter.headline_ = headline;
parameter.description_ = description;
auto [_, is_exist] = params_.try_emplace(key, parameter);
if (!is_exist) { HOLOSCAN_LOG_ERROR("Parameter '{}' already exists", key); }
}
void param(Parameter<holoscan::IOSpec*>& parameter, const char* key, const char* headline,
const char* description, holoscan::IOSpec* default_value) {
parameter.default_value_ = default_value;
param(parameter, key, headline, description);
}
void param(Parameter<std::vector<holoscan::IOSpec*>>& parameter, const char* key,
const char* headline, const char* description) {
parameter.key_ = key;
parameter.headline_ = headline;
parameter.description_ = description;
auto [_, is_exist] = params_.try_emplace(key, parameter);
if (!is_exist) { HOLOSCAN_LOG_ERROR("Parameter '{}' already exists", key); }
}
void param(Parameter<std::vector<holoscan::IOSpec*>>& parameter, const char* key,
const char* headline, const char* description,
std::vector<holoscan::IOSpec*> default_value) {
parameter.default_value_ = default_value;
param(parameter, key, headline, description);
}
template <typename ResourceT, typename StringT, typename... ArgsT,
typename = std::enable_if_t<std::is_constructible_v<std::string, StringT>>>
void resource(const StringT& name, ArgsT&&... args) {
resources_.emplace(name, std::make_shared<ResourceT>(std::forward<ArgsT>(args)...));
}
template <typename... ArgsT>
void resource(ArgsT&&... args) {
resource("", std::forward<ArgsT>(args)...);
}
template <typename ConditionT, typename StringT, typename... ArgsT,
typename = std::enable_if_t<std::is_constructible_v<std::string, StringT>>>
void condition(const StringT& name, ArgsT&&... args) {
conditions_.emplace(name, std::make_shared<ConditionT>(std::forward<ArgsT>(args)...));
}
template <typename ConditionT, typename... ArgsT>
void condition(ArgsT&&... args) {
condition("", std::forward<ArgsT>(args)...);
}
protected:
std::unordered_map<std::string, std::unique_ptr<IOSpec>> inputs_;
std::unordered_map<std::string, std::unique_ptr<IOSpec>> outputs_;
// TODO(gbae): use these for operator 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_SPEC_HPP */