Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ ThingSetUserFunction<0x410, 0x0, int, std::string &> xStringLength(getStringLeng
```

Note that, by default, parameters' IDs will be generated by incrementing the function
ID. It is possible to override this.
ID. It is possible to override this. Parameter names will be generated from a combination of the
function name, parameter type and parameter index. This too can be overridden:

```c++
ThingSetNamedUserFunction<0x470, 0x0, "xNamed", ThingSetParameterNames<"uAlpha", "uBeta">, int, uint16_t, float> xNamed([](uint16_t, float) { return 0; });
```

Once you have declared your properties and functions, instantiate a server with an appropriate
transport to expose them to clients.
Expand Down
27 changes: 17 additions & 10 deletions include/thingset++/ThingSetFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,27 @@ struct ThingSetParameterNames
{
static constexpr size_t count = sizeof...(Names);

template <size_t Index>
template <StringLiteral Name, size_t Index, typename ParameterType>
static constexpr auto get()
{
return std::get<Index>(std::tuple{ Names... });
}
};

/// @brief Empty specialisation of ThingSetParameterNames for the
/// default case which auto-generates parameter names.
template <>
struct ThingSetParameterNames<>
{
static constexpr size_t count = 0;

template <StringLiteral Name, size_t Index, typename ParameterType>
static constexpr auto get()
{
return Name + ThingSetType<std::remove_cvref_t<ParameterType>>::name + "_" + to_string_t<1 + Index>();
}
};

/// @brief Represents an executable function.
/// @tparam Id The unique integer ID of the ThingSet node.
/// @tparam ParentId The integer ID of the parent node.
Expand All @@ -66,11 +80,9 @@ struct ThingSetParameterNames
/// @tparam Result The return type of the function.
/// @tparam ...Args The argument types of the function, if any.
template <uint16_t Id, uint16_t ParentId, StringLiteral Name, ThingSetAccess Access, uint16_t FirstArgumentId, typename ParamNames, typename Result, typename... Args>
requires (ParamNames::count == 0 || ParamNames::count == sizeof...(Args))
class ThingSetFunction : public IdentifiableThingSetParentNode<Id, ParentId, Name>, public ThingSetInvocable
{
static_assert(ParamNames::count == 0 || ParamNames::count == sizeof...(Args),
"ThingSetParameterNames must either be empty or name every argument");

private:
template <uint16_t ChildId, StringLiteral ArgName, typename T>
class ThingSetFunctionParameter : public IdentifiableThingSetNode<ChildId, Id, ArgName>
Expand Down Expand Up @@ -108,12 +120,7 @@ class ThingSetFunction : public IdentifiableThingSetParentNode<Id, ParentId, Nam

static constexpr auto argName()
{
if constexpr (ParamNames::count != 0) {
return ParamNames::template get<Index>();
}
else {
return Name + ThingSetType<std::remove_cvref_t<ParameterType>>::name + "_" + to_string_t<1 + Index>();
}
return ParamNames::template get<Name, Index, ParameterType>();
}

typedef ThingSetFunctionParameter<FirstArgumentId + 0 + Index, argName(), ParameterType> type;
Expand Down
Loading