#ifndef _SIMPLE_SIGNAL_SOLTS_H_
#define _SIMPLE_SIGNAL_SOLTS_H_
#include "string.h"
typedef void (*SIMPLE_SIGNAL)(void *signal, void *pArg);
typedef void (*SIMPLE_SOLTS) (void *pArg);
#define SIMPLE_SOLTS_T(FuncName) void(FuncName)(void *pArg)
#define SIMPLE_EMIT(signal, arg) if (signal != NULL)signal(&signal, arg)
#define SIMPLE_SIGNAL_SOLTS_MAX_SOLTS 10 //一个信号最多连接槽的数量
#define SIMPLE_SIGNAL_SOLTS_MAX_SIGNAL 10 //信号最多数量
ErrorStatus SimpleSignalSolts_Connect(SIMPLE_SIGNAL *signal, SIMPLE_SOLTS solts);
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/******************************************************************************
* @ File name --> SimpleSignalSolts.c
* @ Author --> By@ LiuYu
* @ Version --> V1.0
* @ Date --> 2017 - 06 - 15
* @ Brief --> 轻量级的信号与槽
* @ --> 一个信号对应多个槽,查表调用
*
* @ Copyright (C) 20**
* @ All rights reserved
*******************************************************************************
*
* File Update
* @ Version --> V1.0
* @ Author -->
* @ Date -->
* @ Revise -->
* @ -->
*
*
******************************************************************************/
#include "SimpleSignalSolts.h"
#include <string.h>
//信号结构
typedef struct
{
void *signleID; //信号的指针的指针,保存信号的指针,根据指针的地址确定是否是唯一
SIMPLE_SOLTS soltsTable[SIMPLE_SIGNAL_SOLTS_MAX_SOLTS];
uint8_t soltsCount;
}SIMPLE_SIGNAL_T;
//信号表结构
typedef struct
{
SIMPLE_SIGNAL_T signalTable[SIMPLE_SIGNAL_SOLTS_MAX_SIGNAL];
uint8_t signalCount;
}SIMPLE_SIGNAL_SOLTS_HANDLE_T;
SIMPLE_SIGNAL_SOLTS_HANDLE_T handle =
{
.signalCount = 0,
};
static void SimpleSignalSolts_Signal(void *signal, void *pArg)
{
uint8_t i, j;
SIMPLE_SOLTS solts;
for (i = 0; i < handle.signalCount; i++) //查找是否是同一个信号
{
if (handle.signalTable[i].signleID == signal) //这里注意,signleID保存的是指针的地址,
{
for (j = 0; j < handle.signalTable[i].soltsCount; j++)
{
solts = handle.signalTable[i].soltsTable[j];
if (solts != NULL)
{
solts(pArg);
}
}
}
}
}
/******************************************************************
* @函数说明: 连接信号与槽
* @输入参数: SIMPLE_SIGNAL *singnal 信号的指针(指针的指针)
SIMPLE_SOLTS solts 槽
* @输出参数: 无
* @返回参数: ErrorStatus
* @修改记录: ----
******************************************************************/
ErrorStatus SimpleSignalSolts_Connect(SIMPLE_SIGNAL *signal, SIMPLE_SOLTS solts)
{
if (signal == NULL || solts == NULL) //查空
{
return ERROR;
}
uint8_t i;
if (handle.signalCount > SIMPLE_SIGNAL_SOLTS_MAX_SIGNAL) //错误
{
handle.signalCount = 0;
}
for (i = 0; i < handle.signalCount; i++) //查找是否是同一个信号
{
if (handle.signalTable[i].signleID == signal) //这里注意,signleID保存的是指针的地址,
{
if (handle.signalTable[i].soltsCount > SIMPLE_SIGNAL_SOLTS_MAX_SOLTS)
{
handle.signalTable[i].soltsCount = 0;
}
if (handle.signalTable[i].soltsCount == SIMPLE_SIGNAL_SOLTS_MAX_SOLTS) //满了
{
return ERROR;
}
else
{
handle.signalTable[i].soltsTable[handle.signalTable[i].soltsCount++] = solts; //保存槽
return SUCCESS; //结束
}
}
}
if (handle.signalCount == SIMPLE_SIGNAL_SOLTS_MAX_SIGNAL) //满了
{
return ERROR;
}
else
{
handle.signalTable[handle.signalCount].signleID = signal; //保存新的信号
handle.signalTable[handle.signalCount].soltsTable[0] = solts; //保存槽
handle.signalTable[handle.signalCount++].soltsCount = 1;
*signal = SimpleSignalSolts_Signal;
return SUCCESS;
}
}
/******************************************************************
* @函数说明: 断开信号槽
* @输入参数: SIMPLE_SIGNAL *singnal 信号的指针(指针的指针)
SIMPLE_SOLTS solts 槽
* @输出参数: 无
* @返回参数: ErrorStatus
* @修改记录: ----
******************************************************************/
ErrorStatus SimpleSignalSolts_Disconnect(SIMPLE_SIGNAL *signal, SIMPLE_SOLTS solts)
{
if (signal == NULL || solts == NULL || handle.signalCount == 0) //查空
{
return ERROR;
}
uint8_t i, j;
for (i = 0; i < handle.signalCount; i++) //查找是否是同一个信号
{
if (handle.signalTable[i].signleID == signal) //这里注意,signleID保存的是指针的地址,
{
for (j = 0; j < handle.signalTable[i].soltsCount; j++)
{
if (handle.signalTable[i].soltsTable[j] == solts) //找到槽 移除
{
memcpy(&handle.signalTable[i].soltsTable[j], &handle.signalTable[i].soltsTable[j +1],
(handle.signalTable[i].soltsCount - j - 1) * sizeof(SIMPLE_SOLTS));
handle.signalTable[i].soltsCount--;
}
}
if (handle.signalTable[i].soltsCount == 0) //此信号没有连接槽了
{
memcpy(&handle.signalTable[i], &handle.signalTable[i + 1], (handle.signalCount - i - 1) * sizeof(SIMPLE_SIGNAL_T));
handle.signalCount--;
}
return SUCCESS;
}
}
return ERROR;
}
————————————————
版权声明:本文为CSDN博主「世上最美的裤头」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u010217338/article/details/73327264