提交 1f25cac3 编写于 作者: S Shengliang Guan

func

上级 68f3de61
...@@ -12,45 +12,45 @@ ...@@ -12,45 +12,45 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _TD_UTIL_FUNCTIONAL_H #ifndef _TD_UTIL_FUNCTIONAL_H_
#define _TD_UTIL_FUNCTIONAL_H #define _TD_UTIL_FUNCTIONAL_H_
#include "os.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "os.h" // TODO: hard to use, trying to rewrite it using va_list
//TODO: hard to use, trying to rewrite it using va_list
typedef void* (*GenericVaFunc)(void* args[]); typedef void* (*GenericVaFunc)(void* args[]);
typedef int32_t (*I32VaFunc) (void* args[]); typedef int32_t (*I32VaFunc)(void* args[]);
typedef void (*VoidVaFunc) (void* args[]); typedef void (*VoidVaFunc)(void* args[]);
typedef struct GenericSavedFunc { typedef struct GenericSavedFunc {
GenericVaFunc func; GenericVaFunc func;
void * args[]; void* args[];
} tGenericSavedFunc; } tGenericSavedFunc;
typedef struct I32SavedFunc { typedef struct I32SavedFunc {
I32VaFunc func; I32VaFunc func;
void * args[]; void* args[];
} tI32SavedFunc; } tI32SavedFunc;
typedef struct VoidSavedFunc { typedef struct VoidSavedFunc {
VoidVaFunc func; VoidVaFunc func;
void * args[]; void* args[];
} tVoidSavedFunc; } tVoidSavedFunc;
tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int numOfArgs); tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int32_t numOfArgs);
tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int numOfArgs); tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int32_t numOfArgs);
tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int numOfArgs); tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int32_t numOfArgs);
void* genericInvoke(tGenericSavedFunc* const pSavedFunc); void* genericInvoke(tGenericSavedFunc* const pSavedFunc);
int32_t i32Invoke(tI32SavedFunc* const pSavedFunc); int32_t i32Invoke(tI32SavedFunc* const pSavedFunc);
void voidInvoke(tVoidSavedFunc* const pSavedFunc); void voidInvoke(tVoidSavedFunc* const pSavedFunc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /*_TD_UTIL_FUNCTIONAL_H*/ #endif /*_TD_UTIL_FUNCTIONAL_H_*/
...@@ -13,37 +13,34 @@ ...@@ -13,37 +13,34 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _DEFAULT_SOURCE
#include "tfunctional.h" #include "tfunctional.h"
tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int numOfArgs) { tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int32_t numOfArgs) {
tGenericSavedFunc* pSavedFunc = malloc(sizeof(tGenericSavedFunc) + numOfArgs * (sizeof(void*))); tGenericSavedFunc* pSavedFunc = malloc(sizeof(tGenericSavedFunc) + numOfArgs * (sizeof(void*)));
if(pSavedFunc == NULL) return NULL; if (pSavedFunc == NULL) return NULL;
pSavedFunc->func = func; pSavedFunc->func = func;
return pSavedFunc; return pSavedFunc;
} }
tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int numOfArgs) { tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int32_t numOfArgs) {
tI32SavedFunc* pSavedFunc = malloc(sizeof(tI32SavedFunc) + numOfArgs * sizeof(void *)); tI32SavedFunc* pSavedFunc = malloc(sizeof(tI32SavedFunc) + numOfArgs * sizeof(void*));
if(pSavedFunc == NULL) return NULL; if (pSavedFunc == NULL) return NULL;
pSavedFunc->func = func; pSavedFunc->func = func;
return pSavedFunc; return pSavedFunc;
} }
tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int numOfArgs) { tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int32_t numOfArgs) {
tVoidSavedFunc* pSavedFunc = malloc(sizeof(tVoidSavedFunc) + numOfArgs * sizeof(void*)); tVoidSavedFunc* pSavedFunc = malloc(sizeof(tVoidSavedFunc) + numOfArgs * sizeof(void*));
if(pSavedFunc == NULL) return NULL; if (pSavedFunc == NULL) return NULL;
pSavedFunc->func = func; pSavedFunc->func = func;
return pSavedFunc; return pSavedFunc;
} }
FORCE_INLINE void* genericInvoke(tGenericSavedFunc* const pSavedFunc) { FORCE_INLINE void* genericInvoke(tGenericSavedFunc* const pSavedFunc) { return pSavedFunc->func(pSavedFunc->args); }
return pSavedFunc->func(pSavedFunc->args);
}
FORCE_INLINE int32_t i32Invoke(tI32SavedFunc* const pSavedFunc) { FORCE_INLINE int32_t i32Invoke(tI32SavedFunc* const pSavedFunc) { return pSavedFunc->func(pSavedFunc->args); }
return pSavedFunc->func(pSavedFunc->args);
}
FORCE_INLINE void voidInvoke(tVoidSavedFunc* const pSavedFunc) { FORCE_INLINE void voidInvoke(tVoidSavedFunc* const pSavedFunc) {
if(pSavedFunc) pSavedFunc->func(pSavedFunc->args); if (pSavedFunc) pSavedFunc->func(pSavedFunc->args);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册