Program Listing for File fragment.hpp
↰ Return to documentation for file (include/holoscan/core/fragment.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2023 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_FRAGMENT_HPP
#define HOLOSCAN_CORE_FRAGMENT_HPP
#include <iostream> // for std::cout
#include <memory> // for std::shared_ptr
#include <set> // for std::set
#include <string> // for std::string
#include <type_traits> // for std::enable_if_t, std::is_constructible
#include <utility> // for std::pair
#include "common.hpp"
#include "config.hpp"
#include "executor.hpp"
#include "graph.hpp"
namespace holoscan {
class Fragment {
public:
Fragment() = default;
virtual ~Fragment() = default;
Fragment(Fragment&&) = default;
Fragment& operator=(Fragment&&) = default;
Fragment& name(const std::string& name) &;
Fragment&& name(const std::string& name) &&;
const std::string& name() const;
Fragment& application(Application* app);
void config(const std::string& config_file, const std::string& prefix = "");
Config& config();
Graph& graph();
Executor& executor();
ArgList from_config(const std::string& key);
template <typename OperatorT, typename StringT, typename... ArgsT,
typename = std::enable_if_t<std::is_constructible_v<std::string, StringT>>>
std::shared_ptr<OperatorT> make_operator(const StringT& name, ArgsT&&... args) {
HOLOSCAN_LOG_DEBUG("Creating operator '{}'", name);
auto op = std::make_shared<OperatorT>(std::forward<ArgsT>(args)...);
op->name(name);
op->fragment(this);
auto spec = std::make_shared<OperatorSpec>(this);
op->setup(*spec.get());
op->spec(spec);
op->initialize();
return op;
}
template <typename OperatorT, typename... ArgsT>
std::shared_ptr<OperatorT> make_operator(ArgsT&&... args) {
HOLOSCAN_LOG_DEBUG("Creating operator");
auto op = make_operator<OperatorT>("", std::forward<ArgsT>(args)...);
return op;
}
template <typename ResourceT, typename StringT, typename... ArgsT,
typename = std::enable_if_t<std::is_constructible_v<std::string, StringT>>>
std::shared_ptr<ResourceT> make_resource(const StringT& name, ArgsT&&... args) {
HOLOSCAN_LOG_DEBUG("Creating resource '{}'", name);
auto resource = std::make_shared<ResourceT>(std::forward<ArgsT>(args)...);
resource->name(name);
resource->fragment(this);
auto spec = std::make_shared<ComponentSpec>(this);
resource->setup(*spec.get());
resource->spec(spec);
resource->initialize();
return resource;
}
template <typename ResourceT, typename... ArgsT>
std::shared_ptr<ResourceT> make_resource(ArgsT&&... args) {
HOLOSCAN_LOG_DEBUG("Creating resource");
auto resource = make_resource<ResourceT>("", std::forward<ArgsT>(args)...);
return resource;
}
template <typename ConditionT, typename StringT, typename... ArgsT,
typename = std::enable_if_t<std::is_constructible_v<std::string, StringT>>>
std::shared_ptr<ConditionT> make_condition(const StringT& name, ArgsT&&... args) {
HOLOSCAN_LOG_DEBUG("Creating condition '{}'", name);
auto condition = std::make_shared<ConditionT>(std::forward<ArgsT>(args)...);
condition->name(name);
condition->fragment(this);
auto spec = std::make_shared<ComponentSpec>(this);
condition->setup(*spec.get());
condition->spec(spec);
// Skip initialization. `condition->initialize()` is done in GXFOperator::initialize()
return condition;
}
template <typename ConditionT, typename... ArgsT>
std::shared_ptr<ConditionT> make_condition(ArgsT&&... args) {
HOLOSCAN_LOG_DEBUG("Creating condition");
auto condition = make_condition<ConditionT>("", std::forward<ArgsT>(args)...);
return condition;
}
virtual void add_operator(const std::shared_ptr<Operator>& op);
virtual void add_flow(const std::shared_ptr<Operator>& upstream_op,
const std::shared_ptr<Operator>& downstream_op);
virtual void add_flow(const std::shared_ptr<Operator>& upstream_op,
const std::shared_ptr<Operator>& downstream_op,
std::set<std::pair<std::string, std::string>> port_pairs);
virtual void compose();
virtual void run();
protected:
template <typename ConfigT, typename... ArgsT>
std::unique_ptr<Config> make_config(ArgsT&&... args) {
return std::make_unique<ConfigT>(std::forward<ArgsT>(args)...);
}
template <typename GraphT>
std::unique_ptr<Graph> make_graph() {
return std::make_unique<GraphT>();
}
template <typename ExecutorT>
std::unique_ptr<Executor> make_executor() {
return std::make_unique<ExecutorT>(this);
}
template <typename ExecutorT, typename... ArgsT>
std::unique_ptr<Executor> make_executor(ArgsT&&... args) {
return std::make_unique<ExecutorT>(std::forward<ArgsT>(args)...);
}
std::string name_;
Application* app_ = nullptr;
std::unique_ptr<Config> config_;
std::unique_ptr<Graph> graph_;
std::unique_ptr<Executor> executor_;
};
} // namespace holoscan
#endif/* HOLOSCAN_CORE_FRAGMENT_HPP */