trigger.c

MudOSa4DGD/mudlib/dgd/lib/sys/efun/trigger.c · 2.2 KB

From the archive MudOSa4DGD-30.06.98.tar.gz

Download the archive   « Back to the archive contents

/*
 * trigger.c
 *
 * Support trigger-function as replacement for add_action.
 * Add_action is emulated through macros using triggers.
 *
 * (C) Frank Schmidt, Jesus@NorseMUD
 *
 */

/* mapping for the triggers in object */
private static mapping __triggers;



/* add a trigger to this object */
void add_trigger(mixed verb, mixed func) {
  if (!arrayp(verb)) {
    mixed *t;
    /* is <func> a string (function in this object)? */
    if (stringp(func))
      func = _FP(this_object(), func);
    /* make a mapping? */
    if (!mapp(__triggers))
      __triggers = ([ ]);
    /* make a verb-instance? */
    if (!arrayp(t=__triggers[verb]) || !::sizeof(t))
      __triggers[verb] = ({ func });
    else {
      /* add verb instance with <func> */
      t -= ({ func });
      t += ({ func });
    }
  }
  else {
    /* do this function for all <verb>s */
    map_array(verb, "add_trigger", this_object(), func);
  }
}


/* remove a trigger in this object */
varargs int remove_trigger(mixed verb, mixed func) {
  if (mapp(__triggers)) {
    if (!arrayp(verb)) {
      mixed *t;
      /* do we have a verb-instance? */
      if (arrayp(t=__triggers[verb]) && ::sizeof(t)) {
	if (func) {
	  /* is <func> a string (function in this object)? */
	  if (stringp(func))
	    func = _FP(this_object(), func);
	  /* remove verb instance with <func> */
	  if (!::sizeof(t -= ({ func }))) {
	    /* remove map instance completely */
	    __triggers = map_delete(__triggers, verb);
	  }
	  return 1;
	}
	else {
	  if (::sizeof(t))
	    /* remove last added verb instance */
	    t = array_delete(t, ::sizeof(t)-1);
	  return 1;
	}
      }
    }
    else {
      /* do this function for all <verb>s */
      map_array(verb, "remove_trigger", this_object(), func);
      return 1;
    }
  }
  return 0;
}


/* get trigger-status */
varargs mixed query_triggers(string verb) {
  if (mapp(__triggers)) {
    if (stringp(verb)) {
      /* empty verbs ("") always matches */
      mixed *extra, *funcs;
      if (arrayp(extra=__triggers[""])) {
	if (arrayp(funcs=__triggers[verb]))
	  return extra + funcs;
	return copy(extra);
      }
      /* return triggers matching verb */
      return copy(__triggers[verb]);
    }
    return copy(__triggers);
  }
  return ([ ]);
}

Site Stats

Snippets: 1791
Codebases: 571