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 "./common.hpp"
#include <any>
#include <functional>
#include <iostream>
#include <optional>
#include <typeinfo>
#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("MetaParameter: value is not set");
}
}
void set_default_value() {
if (!value_.has_value()) { value_ = default_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 */