【发布时间】:2019-02-27 15:11:37
【问题描述】:
我可能过于复杂了..
我正在尝试用 C on Arduino 为嵌入式应用程序制作一个可重复使用的分层菜单系统。我有结构来表示不同类型的菜单项,包括那些是子菜单的,以及这些的联合作为通用菜单项。菜单条目数组是菜单的一个级别。一个子菜单项有一个指向另一个菜单项数组的指针。
问题是,子菜单项是联合体的成员,所以需要在联合体之前定义。但是它有一个指向这个联合的实例数组的指针,这会导致编译错误,因为联合尚未定义。
是否有一种类型安全的方式来处理这个问题,或者我在子菜单条目中的指针是否必须是 void*?
代码:
typedef enum {UI_STATE_HOME, UI_STATE_MENU, UI_STATE_DIALOG} te_UIState;
typedef enum {UI_ENTRY_SUBMENU, UI_ENTRY_NUMERIC_INT, UI_ENTRY_NUMERIC_FLOAT, UI_ENTRY_BOOL, UI_ENTRY_DISCRETE} te_UIEntryType;
/* Typedefs for the different types of entry
*
*/
typedef struct {
char* entryName; // pointer to one of our string entries, eg MNU_Light
te_UIEntryType entryType;
} tsEntry;
typedef struct {
char* entryName;
te_UIEntryType entryType;
int (* finalIntHandler)(int selectedValue); //The function that should be called when a number has been selected
int (* initialIntValue) (); //The function that should be called to obtain the starting value for the selector
} ts_EntryInt;
typedef struct {
char* entryName;
te_UIEntryType entryType;
int (* finalIntHandler)(float selectedValue);
float (* initialSingleValue) ();
} ts_EntrySingle;
typedef struct {
char* entryName;
te_UIEntryType entryType;
int (* finalIntHandler)(bool selectedValue);
bool (* initialBoolValue) ();
} ts_EntryBool;
typedef struct {
char* entryName;
te_UIEntryType entryType;
int (* handler)();
char* (* optionalEntryNamePtrFunction)(); //If this points to a function, it'll be called to determine what text to display as the entry name.
// This is for things like enable/disable where the text changes depending on the present state.
} ts_EntryDiscrete;
typedef struct {
char* entryName;
const te_UIEntryType entryType = UI_ENTRY_SUBMENU;
void *entries[];
} ts_EntrySubmenu;
typedef union
{
tsEntry entry;
ts_EntryInt entryInt;
ts_EntrySingle entrySingle;
ts_EntryBool entryBool;
ts_EntryDiscrete entryDiscrete;
ts_EntrySubmenu entrySubmenu;
} tuEntry;
【问题讨论】: