Blame view

mailbox2/observer.c 3.02 KB
Alain Magloire authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/sys/observer.h>

int
28
observer_ref (observer_t observer)
Alain Magloire authored
29 30
{
  if (observer == NULL || observer->vtable == NULL
31
      || observer->vtable->ref == NULL)
Alain Magloire authored
32
    return MU_ERROR_NOT_SUPPORTED;
33
  return observer->vtable->ref (observer);
Alain Magloire authored
34 35
}

36 37
void
observer_destroy (observer_t *pobserver)
Alain Magloire authored
38
{
39 40 41 42 43 44 45
  if (pobserver && *pobserver)
    {
      observer_t observer = *pobserver;
      if (observer->vtable || observer->vtable->destroy)
	observer->vtable->destroy (pobserver);
      *pobserver = NULL;
    }
Alain Magloire authored
46 47 48
}

int
49
observer_action (observer_t observer, struct event evt)
Alain Magloire authored
50 51 52 53 54 55 56 57 58 59
{
  if (observer == NULL || observer->vtable == NULL
      || observer->vtable->action == NULL)
    return MU_ERROR_NOT_SUPPORTED;
  return observer->vtable->action (observer, evt);
}



static int
60
_dobserver_ref (observer_t observer)
Alain Magloire authored
61 62
{
  struct _dobserver *dobserver = (struct _dobserver *)observer;
63
  return mu_refcount_inc (dobserver->refcount);
Alain Magloire authored
64 65
}

66 67
static void
_dobserver_destroy (observer_t *pobserver)
Alain Magloire authored
68
{
69 70
  struct _dobserver *dobserver = (struct _dobserver *)*pobserver;
  if (mu_refcount_dec (dobserver->refcount) == 0)
Alain Magloire authored
71
    {
72 73
      mu_refcount_destroy (&dobserver->refcount);
      free (dobserver);
Alain Magloire authored
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    }
}

static int
_dobserver_action (observer_t observer, struct event evt)
{
  struct _dobserver *dobserver = (struct _dobserver *)observer;
  if (dobserver->action)
    return dobserver->action (dobserver->arg, evt);
  return 0;
}

static struct _observer_vtable _dobserver_vtable =
{
88
  _dobserver_ref,
Alain Magloire authored
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  _dobserver_destroy,

  _dobserver_action
};

/* Implement a default observer.  */
int
observer_create (observer_t *pobserver,
		 int (*action) (void *arg, struct event), void *arg)
{
  struct _dobserver * dobserver;

  if (pobserver)
    return MU_ERROR_INVALID_PARAMETER;

  dobserver = calloc (1, sizeof *dobserver);
  if (dobserver)
    return MU_ERROR_NO_MEMORY;

108 109 110 111 112 113
  mu_refcount_create (&dobserver->refcount);
  if (dobserver->refcount == NULL)
    {
      free (dobserver);
      return MU_ERROR_NO_MEMORY;
    }
Alain Magloire authored
114 115
  dobserver->arg = arg;
  dobserver->action = action;
116
  dobserver->base.vtable = &_dobserver_vtable;
Alain Magloire authored
117 118 119
  *pobserver = &dobserver->base;
  return 0;
}