Skip to content

Commit

Permalink
feat: Add better function to modules with Args & Returns abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrno committed Nov 18, 2024
1 parent 86cb956 commit 7b1011f
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 21 deletions.
8 changes: 5 additions & 3 deletions main/include/modules/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ enum class ModuleFunctionScope
class ModuleFunction
{
public:
ModuleFunction(std::function<void(const VortexMaker::Values&)> foo, const std::string& name);
ModuleFunction(std::function<void(ArgumentValues&, ReturnValues&)> foo, const std::string& name);
ModuleFunction(std::function<void(ArgumentValues&)> foo, const std::string& name);
ModuleFunction(std::function<void(ReturnValues &)> foo, const std::string &name);
ModuleFunction(std::function<void()> foo, const std::string& name);

virtual void execute() {};

std::function<void(const VortexMaker::Values&)> m_function;
VortexMaker::Values m_return_values;
std::function<void(ArgumentValues&, ReturnValues&)> m_function;
ArgumentValues m_return_values;
ModuleFunctionScope m_scope;

std::string m_name;
Expand Down
8 changes: 6 additions & 2 deletions main/include/modules/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class ModuleInterface
void AddLogo(const uint8_t* hexa, size_t size);
void AddLogo(const std::string& relative_path);
void AddFunction(std::function<void()> foo, const std::string& name);
void AddFunction(std::function<void(const VortexMaker::Values&)> foo, const std::string& name);
void AddFunction(std::function<void(ArgumentValues&)> foo, const std::string& name);
void AddFunction(std::function<void(ReturnValues&)> foo, const std::string& name);
void AddFunction(std::function<void(ArgumentValues&, ReturnValues&)> foo, const std::string& name);

//void AddFunction(void (*item)(), const std::string& name, Parameters params);
void AddInputEvent(void (*item)(const std::shared_ptr<hArgs>& args), const std::string& name);
Expand All @@ -59,7 +61,9 @@ class ModuleInterface
std::shared_ptr<ModuleInterface> GetInterface();

void ExecFunction(const std::string& name);
void ExecFunction(const std::string& name, const VortexMaker::Values& args);
void ExecFunction(const std::string& name, ArgumentValues& args);
void ExecFunction(const std::string& name, ReturnValues& ret);
void ExecFunction(const std::string& name, ArgumentValues& args, ReturnValues& ret);
// TODO Exec function with call back + Link to the VortexMaker API

void ExecInputEvent(const std::string& name, std::shared_ptr<hArgs> args);
Expand Down
12 changes: 12 additions & 0 deletions main/include/vortex.h
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,9 @@ namespace VortexMaker
std::string GetValue() { return value; };
nlohmann::json GetJsonValue() { return nlohmann::json::parse(value); };

template<typename T>
T GetJsonValue(const std::string& val) { return nlohmann::json::parse(val).get<T>(); };

void SetValue(const std::string &val) { value = val; };
void SetJsonValue(const nlohmann::json &val) { value = val; };

Expand All @@ -1002,4 +1005,13 @@ namespace VortexMaker
};
}

struct ArgumentValues : public VortexMaker::Values {
using VortexMaker::Values::Values;
};

struct ReturnValues : public VortexMaker::Values {
using VortexMaker::Values::Values;
};


#endif // #ifndef VORTEX_DISABLE
43 changes: 39 additions & 4 deletions main/src/modules/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,46 @@
/**
* @brief Constructor for ModuleFunction.
*
* @param foo Pointer to the function.
* @param foo <Arguments, Return> Pointer to the function.
* @param name The name of the function.
*/
ModuleFunction::ModuleFunction(std::function<void(const VortexMaker::Values&)> foo, const std::string& name)
ModuleFunction::ModuleFunction(std::function<void(ArgumentValues &, ReturnValues &)> foo, const std::string &name)
: m_function(foo), m_name(name) {}

ModuleFunction::ModuleFunction(std::function<void()> foo, const std::string& name)
: m_function([foo](const VortexMaker::Values&) { foo(); }), m_name(name) {}
/**
* @brief Constructor for ModuleFunction.
*
* @param foo <Arguments> Pointer to the function.
* @param name The name of the function.
*/
ModuleFunction::ModuleFunction(std::function<void(ArgumentValues &)> foo, const std::string &name)
: m_function([foo](ArgumentValues &args, ReturnValues &ret)
{
ret = ReturnValues();
foo(args); }),
m_name(name) {}
/**
* @brief Constructor for ModuleFunction.
*
* @param foo <Return> Pointer to the function.
* @param name The name of the function.
*/
ModuleFunction::ModuleFunction(std::function<void(ReturnValues &)> foo, const std::string &name)
: m_function([foo](ArgumentValues &, ReturnValues &ret)
{
foo(ret); // Appeler la fonction avec uniquement `ReturnValues`
}),
m_name(name)
{
}

/**
* @brief Constructor for ModuleFunction.
*
* @param foo Pointer to the function.
* @param name The name of the function.
*/
ModuleFunction::ModuleFunction(std::function<void()> foo, const std::string &name)
: m_function([foo](ArgumentValues &, ReturnValues &)
{ foo(); }),
m_name(name) {}
97 changes: 85 additions & 12 deletions main/src/modules/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ void ModuleInterface::AddFunction(std::function<void()> foo, const std::string &
this->m_functions.push_back(p_function);
}

/**
* @brief Adds a function to the ModuleInterface.
*
* This function creates a shared_ptr to a ModuleFunction and adds it to the ModuleInterface's list of functions.
*
* @param item Pointer to the function.
* @param name Name of the function.
*/
void ModuleInterface::AddFunction(std::function<void(ArgumentValues &)> foo, const std::string &name)
{
// Create a shared_ptr to the ModuleFunction
std::shared_ptr<ModuleFunction> p_function = std::make_shared<ModuleFunction>(foo, name);

// Add the shared_ptr to the list of functions
this->m_functions.push_back(p_function);
}

/**
* @brief Adds a function to the ModuleInterface.
Expand All @@ -120,7 +136,7 @@ void ModuleInterface::AddFunction(std::function<void()> foo, const std::string &
* @param item Pointer to the function.
* @param name Name of the function.
*/
void ModuleInterface::AddFunction(std::function<void(const VortexMaker::Values&)> foo, const std::string &name)
void ModuleInterface::AddFunction(std::function<void(ReturnValues &)> foo, const std::string &name)
{
// Create a shared_ptr to the ModuleFunction
std::shared_ptr<ModuleFunction> p_function = std::make_shared<ModuleFunction>(foo, name);
Expand All @@ -129,7 +145,22 @@ void ModuleInterface::AddFunction(std::function<void(const VortexMaker::Values&)
this->m_functions.push_back(p_function);
}

/**
* @brief Adds a function to the ModuleInterface.
*
* This function creates a shared_ptr to a ModuleFunction and adds it to the ModuleInterface's list of functions.
*
* @param item Pointer to the function.
* @param name Name of the function.
*/
void ModuleInterface::AddFunction(std::function<void(ArgumentValues &, ReturnValues &)> foo, const std::string &name)
{
// Create a shared_ptr to the ModuleFunction
std::shared_ptr<ModuleFunction> p_function = std::make_shared<ModuleFunction>(foo, name);

// Add the shared_ptr to the list of functions
this->m_functions.push_back(p_function);
}

/**
* @brief Adds an input event to the ModuleInterface.
Expand All @@ -142,9 +173,9 @@ void ModuleInterface::AddFunction(std::function<void(const VortexMaker::Values&)
void ModuleInterface::AddInputEvent(void (*item)(const std::shared_ptr<hArgs> &args), const std::string &name)
{
// Checking if the event already exist
for(auto existing_events : this->m_input_events)
for (auto existing_events : this->m_input_events)
{
if(name == existing_events->m_name)
if (name == existing_events->m_name)
{
this->LogError("\"" + name + "\" event already registered ! Abording.");
return;
Expand All @@ -158,12 +189,12 @@ void ModuleInterface::AddInputEvent(void (*item)(const std::shared_ptr<hArgs> &a
this->m_input_events.push_back(p_event);
}

void ModuleInterface::AddInputEvent(void (*item)(const std::shared_ptr<hArgs>& args), const std::string& name, DevFlag devflag, const std::string& description, const std::vector<std::tuple<std::string, std::string, std::string>>& args_def, const bool& can_callback)
void ModuleInterface::AddInputEvent(void (*item)(const std::shared_ptr<hArgs> &args), const std::string &name, DevFlag devflag, const std::string &description, const std::vector<std::tuple<std::string, std::string, std::string>> &args_def, const bool &can_callback)
{
// Checking if the event already exist
for(auto existing_events : this->m_input_events)
for (auto existing_events : this->m_input_events)
{
if(name == existing_events->m_name)
if (name == existing_events->m_name)
{
this->LogError("\"" + name + "\" event already registered ! Abording.");
return;
Expand All @@ -173,15 +204,14 @@ void ModuleInterface::AddInputEvent(void (*item)(const std::shared_ptr<hArgs>& a
// Create a shared_ptr to the ModuleInputEvent
std::shared_ptr<ModuleInputEvent> p_event = std::make_shared<ModuleInputEvent>(item, name);

p_event->m_can_callback = can_callback;
p_event->m_can_callback = can_callback;
p_event->m_devflag = devflag;
p_event->m_description = description;
p_event->m_params = args_def;

// Add the shared_ptr to the list of input events
this->m_input_events.push_back(p_event);
}


/**
* @brief Adds an output event to the ModuleInterface.
Expand Down Expand Up @@ -254,7 +284,30 @@ void ModuleInterface::ExecFunction(const std::string &name)
{
if (foo->m_name == name)
{
foo->m_function(VortexMaker::Values());
ArgumentValues empty_args;
ReturnValues empty_ret;
foo->m_function(empty_args, empty_ret);
return; // Exit after executing the function
}
}
}

/**
* @brief Executes a function by name with arguments.
*
* This function searches for a ModuleFunction with the specified name
* and executes its associated function if found.
*
* @param name The name of the function to execute.
*/
void ModuleInterface::ExecFunction(const std::string &name, ReturnValues &ret)
{
for (auto foo : this->m_functions)
{
if (foo->m_name == name)
{
ArgumentValues empty_args;
foo->m_function(empty_args, ret);
return; // Exit after executing the function
}
}
Expand All @@ -268,13 +321,34 @@ void ModuleInterface::ExecFunction(const std::string &name)
*
* @param name The name of the function to execute.
*/
void ModuleInterface::ExecFunction(const std::string& name, const VortexMaker::Values& args)
void ModuleInterface::ExecFunction(const std::string &name, ArgumentValues &args)
{
for (auto foo : this->m_functions)
{
if (foo->m_name == name)
{
foo->m_function(args);
ReturnValues empty_ret;
foo->m_function(args, empty_ret);
return; // Exit after executing the function
}
}
}

/**
* @brief Executes a function by name with arguments.
*
* This function searches for a ModuleFunction with the specified name
* and executes its associated function if found.
*
* @param name The name of the function to execute.
*/
void ModuleInterface::ExecFunction(const std::string &name, ArgumentValues &args, ReturnValues &ret)
{
for (auto foo : this->m_functions)
{
if (foo->m_name == name)
{
foo->m_function(args, ret);
return; // Exit after executing the function
}
}
Expand Down Expand Up @@ -584,7 +658,6 @@ void ModuleInterface::Stop()
}
}


void ModuleInterface::CallInputEvent(const std::shared_ptr<hArgs> &args, const std::string &event_name, const std::string &module_name, void (*callback)(std::shared_ptr<hArgs> _args))
{
VortexMaker::CallModuleEvent(args, event_name, module_name, callback, this->m_name);
Expand Down

0 comments on commit 7b1011f

Please sign in to comment.