-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deuglified function registry helpers!
- Loading branch information
1 parent
ca27a5c
commit 7ade60f
Showing
10 changed files
with
159 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// CallFunctionHelper.h | ||
#pragma once | ||
#include <vector> | ||
#include <tuple> | ||
|
||
// Helper function to cast and unpack arguments from the vector | ||
template <typename Function, typename Tuple, size_t... Is> | ||
auto callFunctionHelper(Function function, const std::vector<float>& args, std::index_sequence<Is...>) | ||
-> decltype(function(static_cast<typename std::tuple_element<Is, Tuple>::type>(args[Is])...)) | ||
{ | ||
return function(static_cast<typename std::tuple_element<Is, Tuple>::type>(args[Is])...); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// RegisterFunctions.h | ||
#pragma once | ||
#include <cassert> | ||
#include <tuple> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "CallFunctionHelper.h" | ||
#include "FunctionRegistry/FunctionRegistry.h" | ||
#include "FunctionTraits.h" | ||
#include "Peripherals/OperationResult.h" | ||
|
||
// Template function to register functions with automatically deduced number of | ||
// arguments | ||
template <typename Function> | ||
void registerMemberFunction(Function function, const String& commandName) { | ||
using Traits = FunctionTraits<Function>; | ||
constexpr size_t argCountSizeT = Traits::arity; | ||
static_assert(argCountSizeT <= 8, | ||
"Too many arguments. Maximum supported is 8."); | ||
constexpr int argCount = static_cast<int>( | ||
argCountSizeT); // Ensure it matches FunctionRegistry's expected type | ||
|
||
using ArgsTuple = typename Traits::args_tuple; | ||
|
||
// Create a lambda that matches the expected signature for FunctionRegistry | ||
auto wrapper = [function](const std::vector<float>& args) -> OperationResult { | ||
// Ensure the number of arguments matches | ||
assert(args.size() == Traits::arity && | ||
"Incorrect number of arguments provided."); | ||
|
||
// Call the helper to invoke the function with unpacked and casted arguments | ||
return callFunctionHelper<Function, ArgsTuple>( | ||
function, args, std::make_index_sequence<Traits::arity>{}); | ||
}; | ||
|
||
// Register the function with FunctionRegistry | ||
FunctionRegistry::registerFunction( | ||
commandName, wrapper, | ||
argCount // Ensure this matches the expected type (int) | ||
); | ||
} | ||
|
||
// Template function to register functions that accept a vector of arguments | ||
// directly | ||
template <typename Function> | ||
void registerMemberFunctionVector(Function function, | ||
const String& commandName) { | ||
FunctionRegistry::registerFunction(commandName, function, | ||
-1 // Use -1 or another sentinel value to | ||
// indicate variable argument count | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// FunctionTraits.h | ||
#pragma once | ||
#include <tuple> | ||
#include <type_traits> | ||
|
||
// Primary template for FunctionTraits (handles general callables) | ||
template <typename T> | ||
struct FunctionTraits; | ||
|
||
// Specialization for function pointers | ||
template <typename ReturnType, typename... Args> | ||
struct FunctionTraits<ReturnType(*)(Args...)> { | ||
using return_type = ReturnType; | ||
using args_tuple = std::tuple<Args...>; | ||
static constexpr size_t arity = sizeof...(Args); | ||
}; | ||
|
||
// Specialization for member function pointers | ||
template <typename ClassType, typename ReturnType, typename... Args> | ||
struct FunctionTraits<ReturnType(ClassType::*)(Args...)> { | ||
using return_type = ReturnType; | ||
using args_tuple = std::tuple<Args...>; | ||
static constexpr size_t arity = sizeof...(Args); | ||
}; | ||
|
||
// Specialization for const member function pointers | ||
template <typename ClassType, typename ReturnType, typename... Args> | ||
struct FunctionTraits<ReturnType(ClassType::*)(Args...) const> { | ||
using return_type = ReturnType; | ||
using args_tuple = std::tuple<Args...>; | ||
static constexpr size_t arity = sizeof...(Args); | ||
}; | ||
|
||
// Specialization for functors (e.g., lambdas) | ||
template <typename Functor> | ||
struct FunctionTraits : public FunctionTraits<decltype(&Functor::operator())> {}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters