所学均来自百问网
目录
1.页面系统
2. 数据结构的抽象
#ifndef _PAGE_MANAGER_H
#define _PAGE_MANAGER_H
// 对页面结构的抽象
typedef struct PageAction{
char *name; //名称
void (*Run)(void *pParams); //运行函数
struct PageAction *ptNext; //链表
}PageAction,*PPageAction;
void PageRegister(PPageAction ptPageAction);
PPageAction Page(char *name);
void PagesRegister(void);
#endif
3. 底层管理
#include <page_manager.h>
#include <common.h>
#include <string.h>
static PPageAction g_ptPages = NULL; //链表
// 将单个页面放入链表
void PageRegister(PPageAction ptPageAction)
{
ptPageAction->ptNext = g_ptPages;
g_ptPages = ptPageAction;
}
// 查找页面
PPageAction Page(char *name)
{
PPageAction ptTmp = g_ptPages;
while(ptTmp)
{
if(strcmp(name,ptTmp->name) == 0)
return ptTmp;
ptTmp = ptTmp->ptNext; // 找到就指向它
}
// 找不到返回NULL
return NULL;
}
// 注册多个页面
void PagesRegister(void)
{
extern void MainPageRegister(void)
MainPageRegister();
}
4. 抽象实现
#include <page_manager.h>
#include <stdio.h>
void MainPageRun(void *pParams)
{
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
}
static PageAction g_tMainPage = {
.name = "main",
.Run = MainPageRun,
};
void MainPageRegister(void)
{
PageRegister(&g_tMainPage);
}
5. 测试
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <page_manager.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
PagesRegister();
Page("main")->Run(NULL);
return 0;
}