Wednesday, 25 July 2018

Simple C to create an event (pre/on/post)


#include <stdio.h>
/*
#define STRINGIZE(x)           #x
#define STRINGIZE_VALUE_OF(x)  STRINGIZE(x)
*/

struct _inparm_t;
struct _outparm_t;
typedef struct _inparm_t inparm_t;
typedef struct _outparm_t outparm_t;
typedef long (*fnl_ppapi)(const inparm_t*, outparm_t*);
typedef long (*fnl_api)(const inparm_t*, outparm_t*, fnl_ppapi, fnl_ppapi);

struct abc_parm
{
  long a;
  long b;
  long res;
};

long abc_preadd(const inparm_t* in, outparm_t* out)
{
  long rc = 0;
  if (!in)
  {
    printf("no in param\n");
  }
  if (!out)
  {
    printf("no out param\n");
  }
  return rc;
}

long abc_postadd(const inparm_t* in, outparm_t* out)
{
  long rc = 0;
  struct abc_parm* inp = 0;
  struct abc_parm* outp = 0;
  if (in)
  {
    inp = (struct abc_parm*)in;
  }
  if (out)
  {
    outp = (struct abc_parm*)out;
    if (inp)
    {
      printf("%ld + %ld = %ld\n",
        inp->a, inp->b, outp->res);
    }
  }
  return rc;
}

long abc_add(const inparm_t* in, outparm_t* out, fnl_ppapi pre, fnl_ppapi post)
{
  long rc = 0;
  struct abc_parm* inp = 0;
  struct abc_parm* outp = 0;

  if (pre && (rc = pre(in, out)) != 0)
  {
    return rc;
  }

  /* do something here */
  if (in)
  {
    inp = (struct abc_parm*)in;
  }
  if (out)
  {
    outp = (struct abc_parm*)out;
    if (inp)
    {
      outp->res = (inp->a + inp->b);
    }
  }

  if (post && (rc = post(in, out)) != 0)
  {
    return rc;
  }
  return rc;
}


int main(int argc, char* argv[])
{
  /*printf("version: %s\n", STRINGIZE_VALUE_OF(VERSION));*/
  struct abc_parm parm = { 1, 2, 0 };
  abc_add((const inparm_t*)&parm, (outparm_t*)&parm, abc_preadd, abc_postadd);


  abc_add(0, 0, abc_preadd, abc_postadd);
  return 0;
}

No comments:

Post a Comment