Skip to content
Draft
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
183 changes: 141 additions & 42 deletions c/interactor_c_api.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,15 @@ void f3d_interactor_init_commands(f3d_interactor_t* interactor)
}

f3d::interactor* cpp_interactor = reinterpret_cast<f3d::interactor*>(interactor);
cpp_interactor->initCommands();

try
{
cpp_interactor->initCommands();
}
catch (const std::exception& e)
{
f3d::log::warn("Failed to add some of the default commands to the interactor: ", e.what());
}
}

//----------------------------------------------------------------------------
Expand All @@ -382,7 +390,14 @@ void f3d_interactor_add_command(f3d_interactor_t* interactor, const char* action
callback(c_args.data(), static_cast<int>(c_args.size()), user_data);
};

cpp_interactor->addCommand(action, cpp_callback);
try
{
cpp_interactor->addCommand(action, cpp_callback);
}
catch (const std::exception& e)
{
f3d::log::warn(e.what());
}
}

//----------------------------------------------------------------------------
Expand All @@ -406,15 +421,24 @@ char** f3d_interactor_get_command_actions(f3d_interactor_t* interactor, int* cou
return nullptr;
}

char** result = new char*[actions.size()];
try
{
char** result = new char*[actions.size()];

for (size_t i = 0; i < actions.size(); ++i)
{
result[i] = new char[actions[i].length() + 1];
std::strcpy(result[i], actions[i].c_str());
}

for (size_t i = 0; i < actions.size(); ++i)
return result;
}
catch (const std::bad_alloc& e)
{
result[i] = new char[actions[i].length() + 1];
std::strcpy(result[i], actions[i].c_str());
f3d::log::error("Failed to allocate memory for char**: ", e.what());
}

return result;
return nullptr;
}

//----------------------------------------------------------------------------
Expand All @@ -439,7 +463,17 @@ int f3d_interactor_trigger_command(
}

f3d::interactor* cpp_interactor = reinterpret_cast<f3d::interactor*>(interactor);
return cpp_interactor->triggerCommand(command, keep_comments != 0) ? 1 : 0;

try
{
return cpp_interactor->triggerCommand(command, keep_comments != 0) ? 1 : 0;
}
catch (const std::exception& e)
{
f3d::log::warn(e.what());
}

return 0;
}

//----------------------------------------------------------------------------
Expand All @@ -451,7 +485,15 @@ void f3d_interactor_init_bindings(f3d_interactor_t* interactor)
}

f3d::interactor* cpp_interactor = reinterpret_cast<f3d::interactor*>(interactor);
cpp_interactor->initBindings();

try
{
cpp_interactor->initBindings();
}
catch (const std::exception& e)
{
f3d::log::warn("Failed to add some default bindings to the interactor: ", e.what());
}
}

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -479,8 +521,16 @@ void f3d_interactor_add_binding(f3d_interactor_t* interactor, const f3d_interact

std::string cpp_group = group ? group : "";

cpp_interactor->addBinding(cpp_bind, cpp_commands, cpp_group, nullptr,
static_cast<f3d::interactor::BindingType>(type), notify != 0);
try
{

cpp_interactor->addBinding(cpp_bind, cpp_commands, cpp_group, nullptr,
static_cast<f3d::interactor::BindingType>(type), notify != 0);
}
catch (const std::exception& e)
{
f3d::log::warn(e.what());
}
}

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -519,15 +569,24 @@ char** f3d_interactor_get_bind_groups(f3d_interactor_t* interactor, int* count)
return nullptr;
}

char** result = new char*[groups.size()];
try
{
char** result = new char*[groups.size()];

for (size_t i = 0; i < groups.size(); ++i)
{
result[i] = new char[groups[i].length() + 1];
std::strcpy(result[i], groups[i].c_str());
}

for (size_t i = 0; i < groups.size(); ++i)
return result;
}
catch (const std::bad_alloc& e)
{
result[i] = new char[groups[i].length() + 1];
std::strcpy(result[i], groups[i].c_str());
f3d::log::error("Failed to allocate memory for char**: ", e.what());
}

return result;
return nullptr;
}

//----------------------------------------------------------------------------
Expand All @@ -543,25 +602,39 @@ f3d_interaction_bind_t* f3d_interactor_get_binds_for_group(
return nullptr;
}

const f3d::interactor* cpp_interactor = reinterpret_cast<f3d::interactor*>(interactor);
std::vector<f3d::interaction_bind_t> binds = cpp_interactor->getBindsForGroup(group);

*count = static_cast<int>(binds.size());
if (binds.empty())
try
{
return nullptr;
}
const f3d::interactor* cpp_interactor = reinterpret_cast<f3d::interactor*>(interactor);
std::vector<f3d::interaction_bind_t> binds = cpp_interactor->getBindsForGroup(group);

*count = static_cast<int>(binds.size());
if (binds.empty())
{
return nullptr;
}

f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()];

f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()];
for (size_t i = 0; i < binds.size(); ++i)
{
result[i].mod = static_cast<f3d_interaction_bind_modifier_keys_t>(binds[i].mod);
std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1);
result[i].inter[sizeof(result[i].inter) - 1] = '\0';
}

for (size_t i = 0; i < binds.size(); ++i)
return result;
}
catch (const f3d::interactor::does_not_exists_exception& e)
{
result[i].mod = static_cast<f3d_interaction_bind_modifier_keys_t>(binds[i].mod);
std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1);
result[i].inter[sizeof(result[i].inter) - 1] = '\0';
f3d::log::warn(e.what());
}
catch (const std::bad_alloc& e)
{
f3d::log::error("Failed to allocate memory for f3d_interaction_bind_t*: ", e.what());
}

return result;
*count = 0;
return nullptr;
}

//----------------------------------------------------------------------------
Expand All @@ -585,16 +658,25 @@ f3d_interaction_bind_t* f3d_interactor_get_binds(f3d_interactor_t* interactor, i
return nullptr;
}

f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()];
try
{
f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()];

for (size_t i = 0; i < binds.size(); ++i)
{
result[i].mod = static_cast<f3d_interaction_bind_modifier_keys_t>(binds[i].mod);
std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1);
result[i].inter[sizeof(result[i].inter) - 1] = '\0';
}

for (size_t i = 0; i < binds.size(); ++i)
return result;
}
catch (const std::bad_alloc& e)
{
result[i].mod = static_cast<f3d_interaction_bind_modifier_keys_t>(binds[i].mod);
std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1);
result[i].inter[sizeof(result[i].inter) - 1] = '\0';
f3d::log::error("Failed to allocate memory for f3d_interaction_bind_t[]: ", e.what());
}

return result;
return nullptr;
}

//----------------------------------------------------------------------------
Expand All @@ -611,13 +693,20 @@ void f3d_interactor_get_binding_documentation(f3d_interactor_t* interactor,
cpp_bind.mod = static_cast<f3d::interaction_bind_t::ModifierKeys>(bind->mod);
cpp_bind.inter = bind->inter;

auto [doc_str, value_str] = cpp_interactor->getBindingDocumentation(cpp_bind);
try
{
auto [doc_str, value_str] = cpp_interactor->getBindingDocumentation(cpp_bind);

std::strncpy(doc->doc, doc_str.c_str(), sizeof(doc->doc) - 1);
doc->doc[sizeof(doc->doc) - 1] = '\0';
std::strncpy(doc->doc, doc_str.c_str(), sizeof(doc->doc) - 1);
doc->doc[sizeof(doc->doc) - 1] = '\0';

std::strncpy(doc->value, value_str.c_str(), sizeof(doc->value) - 1);
doc->value[sizeof(doc->value) - 1] = '\0';
std::strncpy(doc->value, value_str.c_str(), sizeof(doc->value) - 1);
doc->value[sizeof(doc->value) - 1] = '\0';
}
catch (const std::exception& e)
{
f3d::log::warn(e.what());
}
}

//----------------------------------------------------------------------------
Expand All @@ -633,8 +722,18 @@ f3d_interactor_binding_type_t f3d_interactor_get_binding_type(
f3d::interaction_bind_t cpp_bind;
cpp_bind.mod = static_cast<f3d::interaction_bind_t::ModifierKeys>(bind->mod);
cpp_bind.inter = bind->inter;
f3d::interactor::BindingType cpp_type = cpp_interactor->getBindingType(cpp_bind);
return static_cast<f3d_interactor_binding_type_t>(cpp_type);

try
{
f3d::interactor::BindingType cpp_type = cpp_interactor->getBindingType(cpp_bind);
return static_cast<f3d_interactor_binding_type_t>(cpp_type);
}
catch (const std::exception& e)
{
f3d::log::warn(e.what());
}

return f3d_interactor_binding_type_t::F3D_INTERACTOR_BINDING_OTHER;
}

//----------------------------------------------------------------------------
Expand Down
15 changes: 8 additions & 7 deletions c/interactor_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ extern "C"
*
* @param interactor Interactor handle.
* @param count Output parameter for number of actions.
* @return Array of action strings. Caller must free the array with
* @return Array of action strings or NULL on failure. Caller must free the array with
* f3d_interactor_free_string_array().
*/
F3D_EXPORT char** f3d_interactor_get_command_actions(f3d_interactor_t* interactor, int* count);
Expand Down Expand Up @@ -234,8 +234,8 @@ extern "C"
* @brief Get all bind groups.
*
* @param interactor Interactor handle.
* @param count Output parameter for number of groups.
* @return Array of group strings. Caller must free the array with
* @param count Output parameter for number of groups. It will be set to 0 on failure.
* @return Array of group strings or NULL on failure. Caller must free the array with
* f3d_interactor_free_string_array().
*/
F3D_EXPORT char** f3d_interactor_get_bind_groups(f3d_interactor_t* interactor, int* count);
Expand All @@ -245,8 +245,8 @@ extern "C"
*
* @param interactor Interactor handle.
* @param group Group name.
* @param count Output parameter for number of binds.
* @return Array of binds. Caller must free the array with
* @param count Output parameter for number of binds. It will be set to 0 on failure.
* @return Array of binds or NULL on failure. Caller must free the array with
* f3d_interactor_free_bind_array().
*/
F3D_EXPORT f3d_interaction_bind_t* f3d_interactor_get_binds_for_group(
Expand All @@ -257,7 +257,7 @@ extern "C"
*
* @param interactor Interactor handle.
* @param count Output parameter for number of binds.
* @return Array of binds. Caller must free the array with
* @return Array of binds or NULL on failure. Caller must free the array with
* f3d_interactor_free_bind_array().
*/
F3D_EXPORT f3d_interaction_bind_t* f3d_interactor_get_binds(
Expand Down Expand Up @@ -287,7 +287,8 @@ extern "C"
*
* @param interactor Interactor handle.
* @param bind Interaction bind.
* @return Binding type.
* @return Binding type. Will return F3D_INTERACTOR_BINDING_OTHER on failure or when one of
* the parameters are NULL.
*/
F3D_EXPORT f3d_interactor_binding_type_t f3d_interactor_get_binding_type(
f3d_interactor_t* interactor, const f3d_interaction_bind_t* bind);
Expand Down
Loading