31#ifndef DW_FRAMEWORK_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
32#define DW_FRAMEWORK_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
36#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
38#include <modern-json/json.hpp>
53 dw::core::StringView
const& name,
54 dw::core::StringView
const& typeName,
56 size_t const arraySize)
noexcept;
68 dw::core::StringView
const&
getName() const noexcept;
76 virtual
void addDefault(nlohmann::ordered_json&
object) const;
80 dw::core::StringView m_name;
82 dw::core::StringView m_typeName;
96 !std::is_enum<DefaultType>::value>* =
nullptr>
98nlohmann::json getDefaultValueForJson(DefaultType defaultValue)
100 return nlohmann::json(defaultValue);
105 typename DefaultType,
107 std::is_enum<DefaultType>::value>* =
nullptr>
109nlohmann::json getDefaultValueForJson(DefaultType defaultValue)
111 StringView name{mapEnumValueToName<DefaultType>(defaultValue)};
112 std::string nameStr{name.data(), name.size()};
113 return nlohmann::json(nameStr);
118 typename DefaultType,
size_t N,
120 std::is_enum<DefaultType>::value>* =
nullptr>
122nlohmann::json getDefaultValueForJson(
const std::array<DefaultType, N>& defaultValues)
124 std::array<std::string, N> nameStrings{};
126 for (
size_t i{0U}; i < N; ++i)
128 StringView name{mapEnumValueToName<DefaultType>(defaultValues[i])};
129 std::string nameStr{name.data(), name.size()};
130 nameStrings[i] = std::move(nameStr);
132 return nlohmann::json(nameStrings);
141template <
typename DefaultType>
148 dw::core::StringView name,
149 dw::core::StringView typeName,
152 DefaultType defaultValue)
154 , m_defaultValue(defaultValue)
168 void addDefault(nlohmann::ordered_json&
object)
const override
171 object[
"default"] = detail::getDefaultValueForJson(m_defaultValue);
176 DefaultType m_defaultValue;
190 const std::shared_ptr<const ParameterDescriptor>&
getDescriptor(
size_t const index)
const;
197 size_t getIndex(dw::core::StringView
const& identifier)
const;
204 const std::shared_ptr<const ParameterDescriptor>&
getDescriptor(dw::core::StringView
const& identifier)
const;
210 bool isValid(dw::core::StringView
const& identifier)
const noexcept;
217 void addDescriptor(
const std::shared_ptr<const ParameterDescriptor>& descriptor);
221 typename NodeT,
size_t ConstructorArgumentIndex,
222 typename std::enable_if_t<ConstructorArgumentIndex == std::tuple_size<decltype(describeParameters<NodeT>())>::value,
void>* =
nullptr>
231 typename NodeT,
size_t ConstructorArgumentIndex,
232 typename std::enable_if_t<ConstructorArgumentIndex<std::tuple_size<decltype(describeParameters<NodeT>())>::value,
void>* =
nullptr>
234 void addDescriptors()
237 auto t = std::get<ConstructorArgumentIndex>(describeParameters<NodeT>());
239 const auto& t2 = std::get<PARAMETER_CONSTRUCTOR_ARGUMENT_DESCRIPTOR>(t);
243 addDescriptors<NodeT, ConstructorArgumentIndex + 1>();
250 void addDescriptors(
const dw::core::Tuple<>& t)
252 static_cast<void>(t);
256 template <
typename THead>
258 void addDescriptors(
const dw::core::Tuple<THead>& t)
260 addDescriptor(t.m_head);
265 typename THead,
typename... TTail,
266 typename std::enable_if_t<
sizeof...(TTail) != 0>* =
nullptr>
268 void addDescriptors(
const dw::core::Tuple<THead, TTail...>& t)
270 addDescriptor(t.m_head);
271 addDescriptors(t.m_tail);
277 typename std::enable_if_t<!ParamT::HAS_DEFAULT, void>* =
nullptr>
278 void addDescriptor(
const ParamT& param)
280 addDescriptor(std::make_shared<const ParameterDescriptor>(
284 ParamT::ARRAY_SIZE));
290 typename std::enable_if_t<ParamT::HAS_DEFAULT, void>* =
nullptr>
291 void addDescriptor(
const ParamT& param)
293 addDescriptor(std::make_shared<
const ParameterDescriptorWithDefault<
decltype(ParamT::defaultValue)>>(
298 param.defaultValue));
302 dw::core::VectorFixed<std::shared_ptr<const ParameterDescriptor>> m_descriptors;
306template <
typename NodeT>
A collection of parameter descriptors.
const std::shared_ptr< const ParameterDescriptor > & getDescriptor(size_t const index) const
Get the parameter descriptor for the passed index.
size_t getSize() const
Get the number of parameter descriptors.
void addDescriptors()
Terminate recursion to add parameter descriptors for each node constructor argument to the collection...
bool isValid(dw::core::StringView const &identifier) const noexcept
Check if the passed parameter name matches a parameter descriptor in the collection.
void addDescriptor(const std::shared_ptr< const ParameterDescriptor > &descriptor)
bool isValid(size_t const index) const
Check if the passed index is valid, which mean is within [0, size()).
const std::shared_ptr< const ParameterDescriptor > & getDescriptor(dw::core::StringView const &identifier) const
ParameterCollectionDescriptor(size_t const capacity)
Constructor.
size_t getIndex(dw::core::StringView const &identifier) const
The description of a parameter with a default value.
auto operator=(ParameterDescriptorWithDefault &&) -> ParameterDescriptorWithDefault &=delete
Move assignment operator.
~ParameterDescriptorWithDefault() override=default
Destructor.
auto operator=(ParameterDescriptorWithDefault const &) -> ParameterDescriptorWithDefault &=delete
Copy assignment operator.
ParameterDescriptorWithDefault(dw::core::StringView name, dw::core::StringView typeName, bool isIndex, size_t arraySize, DefaultType defaultValue)
Constructor.
void addDefault(nlohmann::ordered_json &object) const override
Add the default value to the passed JSON object.
ParameterDescriptorWithDefault(ParameterDescriptorWithDefault &&)=delete
Move constructor.
ParameterDescriptorWithDefault(ParameterDescriptorWithDefault const &)=delete
Copy constructor.
The description of a parameter.
dw::core::StringView const & getTypeName() const noexcept
Get the C++ type name of the parameter.
ParameterDescriptor & operator=(ParameterDescriptor const &) &=delete
Copy assignment operator.
virtual ~ParameterDescriptor()=default
Destructor.
ParameterDescriptor(dw::core::StringView const &name, dw::core::StringView const &typeName, const bool isIndex, size_t const arraySize) noexcept
Constructor.
ParameterDescriptor(ParameterDescriptor const &)=delete
Copy constructor.
dw::core::StringView const & getName() const noexcept
Get the parameter name, can be empty for unnamed parameters.
virtual void addDefault(nlohmann::ordered_json &object) const
Add the default value to the passed JSON object (only used by ParameterDescriptorWithDefault()).
ParameterDescriptor(ParameterDescriptor &&)=delete
Move constructor.
ParameterDescriptor & operator=(ParameterDescriptor &&) &=delete
Move assignment operator.
bool isIndex() const noexcept
Check if parameter is an index.
size_t getArraySize() const noexcept
Get the array size, 0 for non-array parameters.
static ParameterCollectionDescriptor createParameterCollectionDescriptor()
Create a parameter collection descriptor for a give node.