2013年8月30日 星期五

callback functioin

callback機制在系統中運作時機。各層次軟体協同處理時用到。

下層是一個GUI系統,具有繪製介面功能,上層將callback function point傳給下層,而當介面產生事件時,callback function回到上層中,,去執行其行為。

基本:

   1: #include <stdio.h>
   2: #include <stdlib.h>
   3: #include <string.h>
   4: typedef void (*T_SIMPLE_CALLBACK)(void);
   5: void simple_callback(void)
   6: {
   7:     printf("++++simple callback++++++\n");
   8: }
   9:  
  10: void test_simple_callback(T_SIMPLE_CALLBACK p_simple)
  11: {
  12:     printf("=========test the callback function start==\n");
  13:     if(p_simple)//
  14:         (*p_simple)();
  15:     else
  16:         printf("no callback function\n");
  17:     printf("=========test the callback function end==\n");
  18:     return;
  19: }
  20:  
  21: int main()
  22: {
  23: test_simple_callback(simple_callback);
  24: return 0;
  25: }

image


image


由結果看出其,在test_simple_callback function執行中,實際上透過參數傳遞進來的函數指標呼叫了回呼函數simple_callback。


其順序為main->test_simple_callback—>simple_callback。而main及simple_callback為上層模組;而test_simple_callback為下層。故當在test_simple_callback中時,called 屬於上層的simple_callback時。即為callback 。


 


干預的callback


典型callback function,含有二個整數參數及整數的返回值。第一個參數是由下層定義的,第二個參數由下層定義,返回值是經由上層經由callback functiion回授給下層的。



   1: #include <stdio.h>
   2: #include <stdlib.h>
   3: #include <string.h>
   4: typedef void (*T_SIMPLE_CALLBACK)(void);
   5: typedef unsigned int (*T_FULL_CALLBACK)(unsigned int para1,unsigned int para2);
   6: void simple_callback(void)
   7: {
   8:     printf("++++simple callback++++++\n");
   9: }
  10: unsigned int full_callback(unsigned int para1,unsigned int para2)
  11: {
  12:     printf("++++full callback++++++\n");
  13:     printf("+para1 :%d, +para2 :%d\n",para1 ,para2 );
  14:     return para2 +para1 ;
  15: }
  16:  
  17: void test_full_callback(T_FULL_CALLBACK p_full,unsigned int para2 )
  18: {
  19:     unsigned int para_callback;
  20:     unsigned int ret_callback;
  21:     para_callback =100;
  22:     printf("===test_full_call_begin\n");
  23:     if(p_full)
  24:     {
  25:         ret_callback =(*p_full)(para_callback ,para2);
  26:         printf("====ret_callback=%d===\n", ret_callback);
  27:     }
  28:     else
  29:         printf("------no_callback----\n");
  30:     printf("===test_full_callback end====\n");
  31:  
  32:  
  33: }
  34:  
  35: void test_simple_callback(T_SIMPLE_CALLBACK p_simple)
  36: {
  37:     printf("=========test the callback function start==\n");
  38:     if(p_simple)//
  39:         (*p_simple)();
  40:     else
  41:         printf("no callback function\n");
  42:     printf("=========test the callback function end==\n");
  43:     return;
  44: }
  45:  
  46: int main()
  47: {
  48: //test_simple_callback(simple_callback);
  49: test_full_callback(full_callback ,1234);
  50: return 0;
  51: }




image


在full_callback 中,第一參數是由下層給的,代表著下層的狀態;而第二個則是main經由test_full_callback間接再給full_callback用著表示上層的資訊。而full_callback function依這二值去運作,並在full_callback返回(到下層)去確定其執行情況。


"透過callback functiion干預了下層模組的執行"


image


 


callback function 使用


使用C語言中,標準函式庫中的函式qsort(void *base,size_t nmemb,size_t size,int(*compar)(const void *,const void *));


其中



  • base:排序的陣列開頭
  • nmemb:陣列中元素數量
  • size:每一個元素的大小
  • compara:為一個函式指標,用來判斷二個元素間的大小關係,若第一個參數值大於第二個則傳回大於0的值;相等時傳回0及小於時傳回小於0值。


   1: #include <stdio.h>
   2: #include <stdlib.h>
   3: #include <string.h>
   4:  
   5: #define ARRAY_MAX 5
   6: int array_1[ARRAY_MAX]={23,46,19,77,35};
   7: static int sort_int_function(const void *a,const void *b)
   8: {
   9:     return *(int*)a-*(int*)b;
  10: }
  11:  
  12:  
  13: int main()
  14: {
  15:     int i;
  16:     printf("====befor sort===\n");
  17:     for(  i = 0; i < ARRAY_MAX; i += 1)
  18:     {
  19:         printf("array[%d]:%d\n", i, array_1[i]);
  20:  
  21:     }
  22:     qsort((void*)array_1,ARRAY_MAX,sizeof(int),sort_int_function);
  23:     printf("====after sort===\n");
  24:     for(  i = 0; i < ARRAY_MAX; i += 1)
  25:     {
  26:         printf("array[%d]:%d\n", i, array_1[i]);
  27:  
  28:     }
  29:  
  30: return 0;
  31: }

image

沒有留言:

張貼留言