/*
 * object.h:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

/*
 * $Id: object.h,v 1.2 2000/09/24 01:46:06 root Exp $
 */

/*
 * $Log: object.h,v $
 * Revision 1.2  2000/09/24 01:46:06  root
 * #
 *
 * Revision 1.1  2000/09/24 01:12:49  root
 * #
 *
 */

#ifndef __OBJECT_H__
#define __OBJECT_H__

#include <sys/types.h>
#include <stdlib.h>

#ifndef OBJECT_IN
#define OBJECT_IN

#define Obj_CreateList(a)	 Do_Obj_CreateList((List *) a)
#define Obj_Init(a)		 Do_Obj_Init((Object *) a)
#define Obj_Delete(a)		 Do_Obj_Delete((Object *) a)
#define Obj_Remove(a,b)		 Do_Obj_Remove((List *) a,(Object *) b)
#define Obj_Insert(a,b,c)	 Do_Obj_Insert((List *) a,(Object *) b,c)
#define Obj_InsertAfter(a,b,c)	 Do_Obj_InsertAfter((List *) a,(Object *) b,(Object *) c)
#define Obj_DeleteList(a)	 Do_Obj_DeleteList((List *) a)




#define Creator(x)        void (*)(struct x ## _struct *)
#define Destructor(x)     void (*)(struct x ## _struct *)
#define ListDestructor(x) void (*)(struct x ## _list_struct *)

#ifdef OBJ_NOTINALL
#ifdef OBJ_INTHIS

#define CREATOR(x)    void (*x ## _default_creator)(x)
#define DESTRUCTOR(x) void (*x ## _default_destructor)(x)

#define BEGIN_OBJECT(x) \
	struct x ## _struct { \
	struct x ## _struct *prev;\
	struct x ## _struct *next;\
	unsigned int len;\
	void (*destructor)(struct x ## _struct *);
#define END_OBJECT(x)	}; \
	typedef struct x ## _struct * x;\
	typedef struct x ## _list_struct { \
	caddr_t prev;\
	caddr_t next;\
	unsigned int len;\
	void (*destructor)(struct x ## _list_struct *);\
	x head;\
	x tail;\
	} *x ## _list;\
	extern void (*x ## _default_creator)(x); \
	extern void (*x ## _default_destructor)(x); \
	x x ## _default_alloca(void) {\
	x res;\
	res=(x) malloc(sizeof(struct x ## _struct));\
	if (!res) return((x) 0);\
	bzero((void *) res,sizeof(struct x ## _struct));\
	Obj_Init(res);\
	res->len=sizeof(struct x ## _struct);\
	res->destructor=(Destructor(x)) x ## _default_destructor;\
	(*x ## _default_creator)((List *) res);\
	return(res);\
	} \
	x ## _list x ## _list_alloca(void) {\
	x ## _list res;\
	res=(x ## _list) malloc(sizeof(struct x ## _list_struct));\
	if (!res) return((x ## _list) 0);\
	Obj_Init((Object *) res);\
	res->destructor=(ListDestructor(x)) Obj_ListDestructor;\
	Obj_CreateList((List *) res);\
	return(res);\
	}

#else
#define BEGIN_OBJECT(x) \
	struct x ## _struct { \
	struct x ## _struct *prev;\
	struct x ## _struct *next;\
	unsigned int len;\
	void (*destructor)(struct x ## _struct *);
#define END_OBJECT(x)	}; \
	typedef struct x ## _struct * x;\
	typedef struct x ## _list_struct { \
	caddr_t prev;\
	caddr_t next;\
	unsigned int len;\
	void (*destructor)(struct x ## _list_struct *);\
	x head;\
	x tail;\
	} *x ## _list;\
	extern void (*x ## _default_creator)(x); \
	extern void (*x ## _default_destructor)(x); \
	extern x x ## _default_alloca(void); \
	extern x ## _list x ## _list_alloca(void);
#endif
#else

#define CREATOR(x)    static void (*x ## _default_creator)(x)
#define DESTRUCTOR(x) static void (*x ## _default_destructor)(x)

#define BEGIN_OBJECT(x) \
	struct x ## _struct { \
	struct x ## _struct *prev;\
	struct x ## _struct *next;\
	unsigned int len;\
	void (*destructor)(struct x ## _struct *);
#define END_OBJECT(x)	}; \
	typedef struct x ## _struct * x;\
	typedef struct x ## _list_struct { \
	caddr_t prev;\
	caddr_t next;\
	unsigned int len;\
	void (*destructor)(struct x ## _list_struct *);\
	x head;\
	x tail;\
	} *x ## _list;\
	static void (*x ## _default_creator)(x); \
	static void (*x ## _default_destructor)(x); \
	static x x ## _default_alloca(void) {\
	x res;\
	res=(x) malloc(sizeof(struct x ## _struct));\
	if (!res) return((x) 0);\
	bzero((void *) res,sizeof(struct x ## _struct));\
	Obj_Init((Object *) res);\
	res->len=sizeof(struct x ## _struct);\
	res->destructor=(Destructor(x)) x ## _default_destructor;\
	(*x ## _default_creator)(res);\
	return(res);\
	} \
	static x ## _list x ## _list_alloca(void) {\
	x ## _list res;\
	res=(x ## _list) malloc(sizeof(struct x ## _list_struct));\
	if (!res) return((x ## _list) 0);\
	Obj_Init((Object *) res);\
	res->destructor=(ListDestructor(x)) Obj_ListDestructor;\
	Obj_CreateList((List *) res);\
	return(res);\
	}


#endif


#define CREATE_OBJ(x) x ## _default_alloca()
#define CREATE_LIST(x) x ## _list_alloca()
#define OBJ_SIZE(x) sizeof(struct x ## _struct)

typedef struct Object_struct
{
  struct Object_struct *prev;
  struct Object_struct *next;
  unsigned int len;
  void (*destructor) (struct Object_struct *);
}
Object;

typedef struct List_struct
{
  struct List_struct *prev;
  struct List_struct *next;
  unsigned int len;
  void (*destructor) (caddr_t);
  Object *head, *tail;
}
List;

#define ObjInsertHead	1
#define ObjInsertTail	2

#endif
void Obj_DefaultDestructor (Object * object);
void Obj_DefaultCreator (Object * object);
void Do_Obj_CreateList (List * list);
void Do_Obj_Init (Object * object);
void Do_Obj_Delete (Object * object);
void Do_Obj_Remove (List * list, Object * object);
void Do_Obj_Insert (List * list, Object * object, int action);
void Do_Obj_InsertAfter (List * list, Object * p_object, Object * object);
void Do_Obj_DeleteList (List * list);
void Obj_ListDestructor (List * list);

#endif /*__OBJECT_H__*/
