2014-02-24 117 views
1

我正在執行一個分析器走的命令樹。爲了允許庫的用戶將他們自己的命令添加到樹中,我提供了一個調用由用戶編碼的處理程序的句柄。處理程序應該是全局還是靜態的?

用戶通過創建要求每個命令的庫函數要註冊的處理程序,通過它的命令和一些細節的句柄命令處理程序本身使用這個。

當分析器,然後遍歷樹就可以找到命令處理程序的處理和調用該函數。

我的問題則應該處理程序是全局可用? (即頭文件中的外部聲明)。因爲我有一個手柄,每一個文件保存在樹上,他們不,嚴格來說,需要是全球性的,但我認爲,在語義上,他們是從他們的定義,所以也許他們應該是文件外叫什麼名字?

一個例子:

圖書館用戶可以有文件myOwnTreeCommands.c

/* This function has its handle passed to the library which is called by the library 
* when building the tree. 
*/ 
int16_t registerUserCommands (void) { 
    /* The user adds the following to register one user command, thus providing a handle 
    * for the command, to the tree structure. 
    */ 
    return registerChild (&cmdHandler); 
} 

/* This function then provides the handler for the registered user command. */ 
int16_t cmdHandler (void) { 
    /* ... Here the user can then add code to make the registered command 
    * perform the action they require. 
    */ 
} 

應該然後cmdHandler()可以通過myOwnTreeCommands.h由全球或作出靜態的,即使它的作品沒有被全球性的?這種情況下是否有標準模式/最佳實踐?由於

回答

1

我會讓他們static,因爲如果可能的話,我認爲這是總是更好。它使接口的「表面積」最小化,這總是很好。

如果功能static,代碼的讀者可以知道,他們從來沒有從外部直接,這是要知道一件好事引用。

相關問題