Commit 5f2f5cd8 authored by Ryan Pavlik's avatar Ryan Pavlik
Browse files

u/hashset: Wrap all C-ABI, C++-impl functions in function-level try.

parent 60272ee4
Pipeline #12502 failed
......@@ -12,6 +12,7 @@
#include <cstring>
#include <unordered_map>
#include <vector>
#include <exception>
/*
......@@ -25,6 +26,19 @@ struct u_hashset
std::unordered_map<std::string, struct u_hashset_item *> map = {};
};
#define HASHSET_CATCH() \
catch (std::exception const &e) \
{ \
fprintf(stderr, "%s: caught exceptions: %s\n", __func__, \
e.what()); \
return -1; \
} \
catch (...) \
{ \
fprintf(stderr, "%s: caught unrecognized exception\n", \
__func__); \
return -1; \
}
/*
*
......@@ -33,27 +47,26 @@ struct u_hashset
*/
extern "C" int
u_hashset_create(struct u_hashset **out_hashset)
{
u_hashset_create(struct u_hashset **out_hashset) try {
auto hs = new u_hashset;
*out_hashset = hs;
return 0;
}
HASHSET_CATCH()
extern "C" int
u_hashset_destroy(struct u_hashset **hs)
{
u_hashset_destroy(struct u_hashset **hs) try {
delete *hs;
*hs = NULL;
return 0;
}
HASHSET_CATCH()
extern "C" int
u_hashset_find_str(struct u_hashset *hs,
const char *str,
size_t length,
struct u_hashset_item **out_item)
{
struct u_hashset_item **out_item) try {
std::string key = std::string(str, length);
auto search = hs->map.find(key);
......@@ -64,52 +77,52 @@ u_hashset_find_str(struct u_hashset *hs,
return -1;
}
}
HASHSET_CATCH()
extern "C" int
u_hashset_find_c_str(struct u_hashset *hs,
const char *c_str,
struct u_hashset_item **out_item)
{
struct u_hashset_item **out_item) try {
size_t length = strlen(c_str);
return u_hashset_find_str(hs, c_str, length, out_item);
}
HASHSET_CATCH()
extern "C" int
u_hashset_insert_item(struct u_hashset *hs, struct u_hashset_item *item)
{
u_hashset_insert_item(struct u_hashset *hs, struct u_hashset_item *item) try {
std::string key = std::string(item->c_str, item->length);
hs->map[key] = item;
return 0;
}
HASHSET_CATCH()
extern "C" int
u_hashset_erase_item(struct u_hashset *hs, struct u_hashset_item *item)
{
u_hashset_erase_item(struct u_hashset *hs, struct u_hashset_item *item) try {
std::string key = std::string(item->c_str, item->length);
hs->map.erase(key);
return 0;
}
HASHSET_CATCH()
extern "C" int
u_hashset_erase_str(struct u_hashset *hs, const char *str, size_t length)
{
u_hashset_erase_str(struct u_hashset *hs, const char *str, size_t length) try {
std::string key = std::string(str, length);
hs->map.erase(key);
return 0;
}
HASHSET_CATCH()
extern "C" int
u_hashset_erase_c_str(struct u_hashset *hs, const char *c_str)
{
u_hashset_erase_c_str(struct u_hashset *hs, const char *c_str) try {
size_t length = strlen(c_str);
return u_hashset_erase_str(hs, c_str, length);
}
HASHSET_CATCH()
extern "C" void
extern "C" int
u_hashset_clear_and_call_for_each(struct u_hashset *hs,
u_hashset_callback cb,
void *priv)
{
void *priv) try {
std::vector<struct u_hashset_item *> tmp;
tmp.reserve(hs->map.size());
......@@ -122,4 +135,6 @@ u_hashset_clear_and_call_for_each(struct u_hashset *hs,
for (auto n : tmp) {
cb(n, priv);
}
return 0;
}
HASHSET_CATCH()
......@@ -77,7 +77,7 @@ u_hashset_erase_c_str(struct u_hashset *hs, const char *c_str);
*
* @ingroup aux_util
*/
void
int
u_hashset_clear_and_call_for_each(struct u_hashset *hs,
u_hashset_callback cb,
void *priv);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment