Skip to content

Commit 4ec5f64

Browse files
committed
simplify CALL_AND_HANDLE and move it to zend_container.h
1 parent e9487f7 commit 4ec5f64

4 files changed

Lines changed: 26 additions & 18 deletions

File tree

bencode.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
#include "binit.h"
55
#include "php_bencode.h"
66

7+
#define CALL_AND_HANDLE(expr) \
8+
try { \
9+
expr; \
10+
} catch (const std::invalid_argument &ia) { \
11+
zend_throw_exception(NULL, ("Invalid argument: " + std::string(ia.what())).c_str(), BENCODE_ERROR_INVALID_ARGUMENT); \
12+
RETURN_NULL(); \
13+
} catch (const std::out_of_range &oor) { \
14+
zend_throw_exception(NULL, ("Out of Range error: " + std::string(oor.what())).c_str(), BENCODE_ERROR_OUT_OF_RANGE); \
15+
RETURN_NULL(); \
16+
} catch (const std::exception &e) { \
17+
zend_throw_exception(NULL, ("Undefined error: " + std::string(e.what())).c_str(), BENCODE_ERROR_UNDEFINED); \
18+
RETURN_NULL(); \
19+
} catch (...) { \
20+
zend_throw_exception(NULL, "Unknown error", BENCODE_ERROR_UNKNOWN); \
21+
RETURN_NULL(); \
22+
}
23+
724
/**** BITEM *****/
825
PHP_METHOD(bitem, __construct)
926
{

bitem.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
zval *bitem::throw_general_exception(const std::string message)
1010
{
11-
zend_throw_exception(NULL, message.c_str(), 1);
11+
zend_throw_exception(NULL, message.c_str(), BENCODE_ERROR_BITEM);
1212
return bitem::get_zval_bool(false);
1313
}
1414

php_bencode.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,4 @@ PHP_MSHUTDOWN_FUNCTION(bencode);
2020
extern zend_module_entry bencode_module_entry;
2121
#define phpext_bencode_ptr &bencode_module_entry
2222

23-
#define CALL_AND_HANDLE(expr) \
24-
try { \
25-
return (expr); \
26-
} catch (const std::invalid_argument &ia) { \
27-
zend_throw_exception(NULL, std::string("Invalid argument: " + std::string(ia.what())).c_str(), 2); \
28-
RETURN_NULL(); \
29-
} catch (const std::out_of_range &oor) { \
30-
zend_throw_exception(NULL, std::string("Out of Range error: " + std::string(oor.what())).c_str(), 3); \
31-
RETURN_NULL(); \
32-
} catch (const std::exception &e) { \
33-
zend_throw_exception(NULL, std::string("Undefined error: " + std::string(e.what())).c_str(), 4); \
34-
RETURN_NULL(); \
35-
} catch (...) { \
36-
zend_throw_exception(NULL, "Unkown error", 5); \
37-
RETURN_NULL(); \
38-
}
39-
4023
#endif

zend_container.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ extern "C" {
1111
#include "blist.h"
1212
#include "bstr.h"
1313

14+
/* clang-format off */
15+
#define BENCODE_ERROR_BITEM 1
16+
#define BENCODE_ERROR_INVALID_ARGUMENT 2
17+
#define BENCODE_ERROR_OUT_OF_RANGE 3
18+
#define BENCODE_ERROR_UNDEFINED 4
19+
#define BENCODE_ERROR_UNKNOWN 5
20+
/* clang-format on */
21+
1422
#define ZEND_CONTAINER_PRE(bclass) \
1523
typedef struct { \
1624
bclass *bnode_data; \

0 commit comments

Comments
 (0)