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 "./common.hpp"
#include <iostream>
#include <memory>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include "./component_spec.hpp"
#include "./io_spec.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 item '{}' already exists", name); }
return *(iter->second.get());
}
std::unordered_map<std::string, std::unique_ptr<IOSpec>>& outputs() { return outputs_; }
template <typename DataT>
IOSpec& output() {
// TODO: implement this
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 item '{}' already exists", name); }
return *(iter->second.get());
}
using ComponentSpec::param;
void param(Parameter<holoscan::IOSpec*>& parameter, const char* key, const char* headline,
const char* description, holoscan::IOSpec* default_value) {
parameter.key_ = key;
parameter.headline_ = headline;
parameter.description_ = description;
parameter.default_value_ = default_value;
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.key_ = key;
parameter.headline_ = headline;
parameter.description_ = description;
parameter.default_value_ = default_value;
auto [_, is_exist] = params_.try_emplace(key, parameter);
if (!is_exist) { HOLOSCAN_LOG_ERROR("Parameter '{}' already exists", key); }
}
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 */