首页 > 解决方案 > 快速将结构数据从类发送到实例

问题描述

我用 C 语言制作了一个静态库。该库的功能如下。

  1. 将回调函数点保存在全局变量中
  2. 回调函数有一个struct结构的参数。
  3. 库通过在特定时刻调用回调函数将结构数据传输给库用户。

其来源是:

  1. 静态库源
// ---------------------------------------------
// Static Library
// ---------------------------------------------
typedef struct
{
  unsigned long m_Data1;
  unsigned long m_Data2;
  unsigned long m_Data3;
} SEND_DATA;

//
typedef int (*ReportCallbackProc)(SEND_DATA *pSendData);

//
static ReportCallbackProc gReportFunc = NULL;
static Period = 0;

// Set callback function pointer to gloval variable
int SetReport(ReportCallbackProc func, int period)
{
  gReportFunc = func;
  Period = period;

  return(1);
}

// Send data to Application of library
void Report()
{
   .............
   SEND_DATA sendData;
   sendData.m_Data1 = 1;
   sendData.m_Data2 = 2;
   sendData.m_Data3 = 3;

   int rc = gReportFunc(&sendData);
   if (rc != 1)
   {
      printf("gRealtimeReportFunc failed: status=[%d]\n", rc);
   }

    .....
}
  1. 这个库的应用
// ---------------------------------------------
// Application of Static Library
// ---------------------------------------------
void handle_report (SEND_DATA *pSendData)
{
   printf("Report called");
   printf("m_Data1 = %ld ", pSendData->m_Data1);
   printf("m_Data2 = %ld ", pSendData->m_Data2);
   printf("m_Data3 = %ld ", pSendData->m_Data3);
}

void main()
{
    SetReport (&handle_report, period);
}

我想迅速制作上述来源。目前,我正在尝试在学习 swift 时实施。但是,这不是一个简单的问题,需要帮助。我尝试创建和实现闭包用法和类,但这绝非易事。

你能提供类似的快速代码吗?你能告诉我如何实现它吗?

标签: swiftstatic

解决方案


推荐阅读