Program Listing for File parameter.hpp
↰ Return to documentation for file (include/holoscan/core/parameter.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_PARAMETER_HPP
#define HOLOSCAN_CORE_PARAMETER_HPP
#include <any>
#include <functional>
#include <iostream>
#include <optional>
#include <string>
#include <typeinfo>
#include <utility>
#include "./common.hpp"
#include "./arg.hpp"
namespace holoscan {
enum class ParameterFlag {
kNone = 0,
kOptional = 1,
kDynamic = 2,
};
class ParameterWrapper {
public:
ParameterWrapper() = default;
template <typename typeT>
explicit ParameterWrapper(Parameter<typeT>& param)
: type_(&typeid(typeT)), arg_type_(ArgType::create<typeT>()), value_(¶m) {}
ParameterWrapper(std::any value, const std::type_info* type, const ArgType& arg_type)
: type_(type), arg_type_(arg_type), value_(std::move(value)) {}
const std::type_info& type() const {
if (type_) { return *type_; }
return typeid(void);
}
const ArgType& arg_type() const { return arg_type_; }
std::any& value() { return value_; }
private:
const std::type_info* type_ = nullptr;
ArgType arg_type_;
std::any value_;
};
template <typename ValueT>
class MetaParameter {
public:
MetaParameter() = default;
explicit MetaParameter(ValueT& value) : value_(value) {}
explicit MetaParameter(ValueT&& value) : value_(std::move(value)) {}
MetaParameter& operator=(const ValueT& value) {
value_ = value;
return *this;
}
MetaParameter&& operator=(ValueT&& value) {
value_ = std::move(value);
return std::move(*this);
}
const std::string& key() const { return key_; }
bool has_value() const { return value_.has_value(); }
ValueT& get() {
if (value_.has_value()) {
return value_.value();
} else {
throw std::runtime_error(fmt::format("MetaParameter: value for '{}' is not set", key_));
}
}
void set_default_value() {
if (!value_.has_value()) { value_ = default_value_; }
}
ValueT& default_value() {
if (default_value_.has_value()) {
return default_value_.value();
} else {
throw std::runtime_error(
fmt::format("MetaParameter: default value for '{}' is not set", key_));
}
}
bool has_default_value() const { return default_value_.has_value(); }
operator ValueT&() { return get(); }
private:
friend class ComponentSpec;
friend class OperatorSpec;
std::optional<ValueT> value_;
std::optional<ValueT> default_value_;
std::string key_;
std::string headline_;
std::string description_;
ParameterFlag flag_ = ParameterFlag::kNone;
};
} // namespace holoscan
#endif/* HOLOSCAN_CORE_PARAMETER_HPP */