Skip to content

Commit

Permalink
Core : Add function with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrno committed May 15, 2024
1 parent d0bfbb8 commit afb08bc
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 21 deletions.
Binary file modified build/bin/vortex
Binary file not shown.
2 changes: 1 addition & 1 deletion main/builtint/vortex.modules.builtin.toolchains
4 changes: 3 additions & 1 deletion main/include/modules/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ class ModuleFunction
{
public:
ModuleFunction(void(*foo)(), const std::string& name);
ModuleFunction(void(*m_args_foo)(const std::shared_ptr<hArgs>& args), const std::string& name);

virtual void execute() {};

void(*m_foo)();
void(*m_args_foo)(const std::shared_ptr<hArgs>& args);
std::string m_name;
//std::shared_ptr<Parameters> m_params;
std::shared_ptr<hArgs> m_args;
std::vector<std::string> m_params_def;
};

Expand Down
12 changes: 12 additions & 0 deletions main/include/modules/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ModuleInterface
void AddLogo(const std::string& relative_path);
void AddFunction(void (*item)(), const std::string& name);
void AddFunction(void (*item)(), const std::string& name, const std::string& description);
void AddFunction(void (*item)(const std::shared_ptr<hArgs>& args), const std::string& name, const std::string& description);

//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,9 +60,20 @@ class ModuleInterface
std::shared_ptr<ModuleInterface> GetInterface();

void ExecFunction(const std::string& name);
void ExecFunction(const std::string& name, std::shared_ptr<hArgs> args);
void ExecInputEvent(const std::string& name, std::shared_ptr<hArgs> args);
void ExecOutputEvent(const std::string& name, std::shared_ptr<hArgs> args);

/*
Finir les functions
Trouver un moyen d'init direct un hArg,
tout porter en fonction vortex.
Faire un toolchaine fonctionnelle
*/

template<typename T>
void AddModuleItemParam(const void *item, std::pair<std::string, T> parameter);

Expand Down
39 changes: 21 additions & 18 deletions main/include/vortex.h
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,27 @@ struct hString
//=============================================================================
class hArgs
{
public:
private:
class ArgumentBase
{
public:
virtual ~ArgumentBase() = default;
};

template <typename T>
class ArgumentHolder : public ArgumentBase
{
public:
ArgumentHolder(T value) : storedValue(value) {}
T getValue() const { return storedValue; }

private:
T storedValue;
};

public:
hArgs(){};

template <typename T>
void add(const hString &tag, T value)
{
Expand Down Expand Up @@ -934,23 +954,6 @@ class hArgs

hVector<hString> registered_arguments;

private:
class ArgumentBase
{
public:
virtual ~ArgumentBase() = default;
};

template <typename T>
class ArgumentHolder : public ArgumentBase
{
public:
ArgumentHolder(T value) : storedValue(value) {}
T getValue() const { return storedValue; }

private:
T storedValue;
};

hMap<hString, ArgumentBase *> arguments;
};
Expand Down
4 changes: 4 additions & 0 deletions main/src/modules/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
*/
ModuleFunction::ModuleFunction(void (*foo)(), const std::string& name)
: m_foo(foo), m_name(name) {}


ModuleFunction::ModuleFunction(void(*foo)(const std::shared_ptr<hArgs>& args), const std::string& name)
: m_args_foo(foo), m_name(name) {}
43 changes: 42 additions & 1 deletion main/src/modules/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ void ModuleInterface::AddFunction(void (*item)(), const std::string &name)
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(void (*item)(const std::shared_ptr<hArgs>& args), const std::string &name, const std::string& description)
{
// Create a shared_ptr to the ModuleFunction
std::shared_ptr<ModuleFunction> p_function = std::make_shared<ModuleFunction>(item, 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 Down Expand Up @@ -149,7 +169,7 @@ void ModuleInterface::AddInputEvent(void (*item)(const std::shared_ptr<hArgs>& a
return;
}
}

// Create a shared_ptr to the ModuleInputEvent
std::shared_ptr<ModuleInputEvent> p_event = std::make_shared<ModuleInputEvent>(item, name);

Expand Down Expand Up @@ -240,6 +260,27 @@ void ModuleInterface::ExecFunction(const std::string &name)
}
}

/**
* @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, std::shared_ptr<hArgs> args)
{
for (auto foo : this->m_functions)
{
if (foo->m_name == name)
{
foo->m_args = args;
foo->m_args_foo(args);
return; // Exit after executing the function
}
}
}

/**
* @brief Executes an input event by name.
*
Expand Down

0 comments on commit afb08bc

Please sign in to comment.