Program Listing for File network_context.hpp
↰ Return to documentation for file (include/holoscan/core/network_context.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-2024 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_NETWORK_CONTEXT_HPP
#define HOLOSCAN_CORE_NETWORK_CONTEXT_HPP
#include <stdio.h>
#include <iostream>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include "./parameter.hpp"
#include "./type_traits.hpp"
#include "./arg.hpp"
#include "./component.hpp"
#include "./forward_def.hpp"
#include "./resource.hpp"
#define HOLOSCAN_NETWORK_CONTEXT_FORWARD_TEMPLATE() \
template <typename ArgT, \
typename... ArgsT, \
typename = std::enable_if_t<!std::is_base_of_v<NetworkContext, std::decay_t<ArgT>> && \
(std::is_same_v<Arg, std::decay_t<ArgT>> || \
std::is_same_v<ArgList, std::decay_t<ArgT>>)>>
#define HOLOSCAN_NETWORK_CONTEXT_FORWARD_ARGS(class_name) \
HOLOSCAN_NETWORK_CONTEXT_FORWARD_TEMPLATE() \
class_name(ArgT&& arg, ArgsT&&... args) \
: NetworkContext(std::forward<ArgT>(arg), std::forward<ArgsT>(args)...) {}
#define HOLOSCAN_NETWORK_CONTEXT_FORWARD_ARGS_SUPER(class_name, super_class_name) \
HOLOSCAN_NETWORK_CONTEXT_FORWARD_TEMPLATE() \
class_name(ArgT&& arg, ArgsT&&... args) \
: super_class_name(std::forward<ArgT>(arg), std::forward<ArgsT>(args)...) {}
namespace holoscan {
// TODO: NetworkContext is identical in implementation to Scheduler, so put the functionality in
// a common base class.
class NetworkContext : public Component {
public:
NetworkContext() = default;
NetworkContext(NetworkContext&&) = default;
HOLOSCAN_NETWORK_CONTEXT_FORWARD_TEMPLATE()
explicit NetworkContext(ArgT&& arg, ArgsT&&... args) {
add_arg(std::forward<ArgT>(arg));
(add_arg(std::forward<ArgsT>(args)), ...);
}
~NetworkContext() override = default;
using Component::id;
NetworkContext& id(int64_t id) {
id_ = id;
return *this;
}
using holoscan::Component::name;
NetworkContext& name(const std::string& name) & {
name_ = name;
return *this;
}
NetworkContext&& name(const std::string& name) && {
name_ = name;
return std::move(*this);
}
using holoscan::Component::fragment;
NetworkContext& fragment(Fragment* fragment) {
fragment_ = fragment;
return *this;
}
NetworkContext& 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) {
if (resources_.find(arg->name()) != resources_.end()) {
HOLOSCAN_LOG_ERROR(
"Resource '{}' already exists in the network context. Please specify a unique "
"name when creating a Resource instance.",
arg->name());
} else {
resources_[arg->name()] = arg;
}
}
void add_arg(std::shared_ptr<Resource>&& arg) {
if (resources_.find(arg->name()) != resources_.end()) {
HOLOSCAN_LOG_ERROR(
"Resource '{}' already exists in the network context. Please specify a unique "
"name when creating a Resource instance.",
arg->name());
} else {
resources_[arg->name()] = std::move(arg);
}
}
std::unordered_map<std::string, std::shared_ptr<Resource>>& resources() { return resources_; }
virtual void setup(ComponentSpec& spec) { (void)spec; }
void initialize() override;
YAML::Node to_yaml_node() const override;
protected:
void reset_graph_entities() override;
std::unordered_map<std::string, std::shared_ptr<Resource>>
resources_;
};
} // namespace holoscan
#endif/* HOLOSCAN_CORE_NETWORK_CONTEXT_HPP */