/****************************************************************************
 * KONOHA COPYRIGHT, LICENSE NOTICE, AND DISCRIMER
 *
 * Copyright (c) 2005-2008, Kimio Kuramitsu <kimio at ynu.ac.jp>
 *           (c) 2008-      Konoha Software Foundation
 * All rights reserved.
 *
 * You may choose one of the following two licenses when you use konoha.
 * See www.konohaware.org/license.html for further information.
 *
 * (1) GNU General Public License 2.0      (with    KONOHA_UNDER_GPL2)
 * (2) Konoha Software Foundation License 1.0
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#ifndef CLASS_KONOHA_H_
#define CLASS_KONOHA_H_

#include<konoha/konoha_t.h>
#include<konoha/konoha_api.h>

/* ************************************************************************ */

#ifdef __cplusplus
extern "C" {
#endif

/* ------------------------------------------------------------------------ */
/* flag */

/* ------------------------------------------------------------------------ */
/* @class Object Object knh_Object_t @Cyclic */

/* @flag Object.Release!Debug OF (%s)->h.flag 'is:*:is:*' */
/* @flag Object.Immutable OF (%s)->h.flag 'is:*:is:*' */
/* @flag Object.Cyclic OF (%s)->h.flag 'is:set:*:*' */
/* @flag Object.MetaData OF (%s)->h.flag 'has:set:has:*' */
/* @flag Object.Synchronized OF (%s)->h.flag 'is:*:is:*' */
/* @flag Object.Modified OF (%s)->h.flag 'is:set:is:set' */
/* @flag Object.GCMarked OF (%s)->h.flag 'is:set:is:set' */
/* @flag Object.Formatted OF (%s)->h.flag 'is:set:is:set' */
/* @flag Object.Local4 OF (%s)->h.flag 'is:set:*:*' */
/* @flag Object.Local3 OF (%s)->h.flag 'is:set:*:*' */
/* @flag Object.Local2 OF (%s)->h.flag 'is:set:*:*' */
/* @flag Object.Local1 OF (%s)->h.flag 'is:set:*:*' */

#ifndef KNH_OBJECT_FIELDSIZE
#define KNH_OBJECT_FIELDSIZE 64
#endif

/* ------------------------------------------------------------------------ */
/* @class Any Object knh_Object_t @Cyclic */
/* @class This Any knh_Object_t   */
/* @class Any1 Any knh_Object_t   */
/* @class Any2 Any knh_Object_t   */

typedef void knh_Any_t;
typedef void knh_This_t;
typedef void knh_Any1_t;
typedef void knh_Any2_t;

/* ------------------------------------------------------------------------ */
/* @class Nue Object knh_Object_t @Immutable */
/* @flag Nue.GCHook NUE:1 (%s)->h.flag 'is:set:*:*' */

typedef struct knh_Nue_t {
	knh_hObject_t h;
	knh_uchar_t *str;
	size_t size;
	struct knh_String_t *orign;
} knh_Nue_t;

/* ------------------------------------------------------------------------ */
/* @class Boolean Object knh_Object_t @Immutable */

typedef struct knh_Boolean_t {
	knh_hObject_t h;
	knh_bool_t value;
} knh_Boolean_t;

/* ------------------------------------------------------------------------ */
/* @class Number Object  knh_Object_t @Immutable */

typedef struct knh_Number_t {
	knh_hObject_t h;
} knh_Number_t;

/* ------------------------------------------------------------------------ */
/* @class Int Number  knh_Object_t @Immutable */
/* @class IntX Number knh_Object_t @Immutable */

typedef struct knh_Int_t {
	knh_hObject_t h;
	knh_int_t value;
} knh_Int_t;

typedef knh_Int_t knh_IntX_t;

/* ------------------------------------------------------------------------ */
/* @class Int64  Number knh_Object_t @Immutable */
/* @class Int64X Number knh_Object_t @Immutable */

typedef struct knh_Int64_t {
	knh_hObject_t h;
	knh_int64_t value;
} knh_Int64_t;

typedef knh_Int64_t knh_Int64X_t;

/* ------------------------------------------------------------------------ */
/* @class Float Number knh_Object_t @Immutable */
/* @class FloatX Number knh_Object_t @Immutable */

typedef struct knh_Float_t {
	knh_hObject_t h;
	knh_float_t value;
} knh_Float_t;

typedef knh_Float_t knh_FloatX_t;

/* ------------------------------------------------------------------------ */
/* @class String Object knh_Object_t @Immutable */
/* @class StringX Object knh_Object_t @Immutable */
/* @flag String.TextSgm STRING:1 (%s)->h.flag 'is:set:*:*' */
/* @flag String.ASCII STRING:2 (%s)->h.flag 'is:set:*:*' */

typedef struct knh_String_t {
	knh_hObject_t h;
	knh_uchar_t *str;
	size_t size;
	struct knh_String_t *orign;
} knh_String_t;

typedef knh_String_t   knh_StringX_t;

/* ------------------------------------------------------------------------ */
/* @class Bytes Object knh_Object_t */
/* @flag Bytes.StackBuffer BYTES:1 (%s)->h.flag 'is:set:*:*' */

typedef struct knh_Bytes_t {
	knh_hObject_t h;
	knh_uchar_t *buf;
	size_t size;
//	size_t capacity;
} knh_Bytes_t ;

/* ------------------------------------------------------------------------ */
/* @class Tuple2 Object knh_Object_t @Immutable */

typedef struct knh_Tuple2_t {
	knh_hObject_t h;
	knh_Object_t  *first;
	knh_Object_t  *second;
} knh_Tuple2_t;

/* ------------------------------------------------------------------------ */
/* @class Range Object knh_Object_t @Immutable */

typedef struct knh_Range_t {
	knh_hObject_t h;
	knh_Object_t  *start;
	knh_Object_t  *end;
} knh_Range_t;

/* ------------------------------------------------------------------------ */
/* @class Array Object knh_Object_t @Cyclic */

typedef struct knh_Array_t {
	knh_hObject_t h;
	struct knh_Object_t **list;
	size_t size;
//	size_t capacity;
} knh_Array_t;

/* ------------------------------------------------------------------------ */
/* @class DictMap2 Object knh_Object_t @Cyclic */
/* @class DictSet2 Object knh_Object_t @Cyclic */

typedef struct {
	union {
		Object *key;
		struct knh_String_t *name;
	};
	union {
		Object *value;
		knh_uint_t uvalue;
	};
} knh_dictentry_t;

typedef struct knh_DictMap2_t {
	knh_hObject_t h;
	struct dictentry *list;
	size_t size;
} knh_DictMap2_t;

#define knh_DictSet2_t    knh_DictMap2_t

/* ------------------------------------------------------------------------ */
/* @class DictMap Object knh_ */

typedef struct knh_dictmape_t {
	struct knh_String_t *key;
	Object *value;
} knh_dictmape_t;

typedef struct knh_DictMap {
	size_t size;
	size_t capacity;
	knh_dictmape_t* list;
	size_t sorted;
} knh_DictMap;

/* ------------------------------------------------------------------------ */
/* @class DictSet Object knh_ */

typedef struct knh_dictsete_t {
	struct knh_String_t *key;
	knh_uint_t  value;
} knh_dictsete_t;

typedef struct knh_DictSet {
	size_t size;
	size_t capacity;
	knh_dictsete_t* list;
	size_t sorted;
} knh_DictSet;


/* ------------------------------------------------------------------------ */
/* HashMap, HashSet */

typedef struct {
	knh_hcode_t key;
	Object *keyobj;
} knh_hashkey_t;

typedef struct knh_hashmape_t {
	knh_hashkey_t           h;
	struct knh_hashmape_t  *next;
	Object                 *value;
} knh_hashmape_t ;

/* ------------------------------------------------------------------------ */
/* @class HashMap Object knh_ */

typedef struct knh_HashMap {
	size_t size;
	size_t capacity;
	knh_hashmape_t** array;
	float factor;
} knh_HashMap;

/* ------------------------------------------------------------------------ */
/* @class HashSet Object knh_ */

typedef struct knh_hashsete_t {
	knh_hashkey_t            h;
	struct knh_hashsete_t   *next;
	knh_uint_t               value;
} knh_hashsete_t ;

typedef struct knh_HashSet {
	size_t size;
	size_t capacity;
	knh_hashsete_t** array;
	float factor;
} knh_HashSet;

/* ------------------------------------------------------------------------ */
/* @ class LRUCache Object knh_LRUCache */

typedef struct knh_lruentry_t {
	knh_hcode_t keyid;
	Object *key;
	Object *value;
	struct knh_lruentry_t  *value_next;
	struct knh_lruentry_t  *prev;
	struct knh_lruentry_t  *next;
} knh_lruentry_t ;

typedef struct knh_LRUCache {
	size_t size;
	size_t capacity;
	knh_lruentry_t** array;
	knh_lruentry_t** base;
	knh_lruentry_t *first;
	knh_lruentry_t *last;
#ifdef KONOHA_STATMODE
	char *name;
	size_t hitc;
	size_t missc;
#endif
} knh_LRUCache;


/* ------------------------------------------------------------------------ */
/* class Map Object knh_Map */

struct knh_Map;
typedef Object* (*f_map_get)(Ctx *, Object *b, Object *k);
typedef Object* (*f_map_set)(Ctx *, Object *b, Object *k, Object *v);

typedef struct knh_Map {
	Object *map;
} knh_Map ;

/* ------------------------------------------------------------------------ */
/* @class Iterator Object knh_Iterator */

struct knh_Iterator_t;
typedef Object* (*f_next)(Ctx *, struct knh_Iterator_t *);
//typedef Object* (*f_each)(Ctx *, struct knh_Iterator *, knh_class_t);

typedef struct knh_Iterator {
	Object* source;
//	knh_class_t cid;
//	knh_flag_t flag;
//	f_each feach;
	size_t pos;
	void* ref;
	f_next fnext;
	size_t count;
	size_t start;
	size_t end;
} knh_Iterator;

/* ------------------------------------------------------------------------ */
/* @class DictIdx Object knh_ */

typedef struct knh_DictIdx {
	knh_int_t    offset;
	knh_Array_t* terms;
	struct knh_DictSet_t* termsDictSet;
} knh_DictIdx;

/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* @class Class Object knh_Object_t */
/* @flag Class.Release!Debug CF knh_tClass[(%s)].cflag 'is:*:is:*' */
/* @flag Class.Immutable CF knh_tClass[(%s)].cflag 'is:*:is:*' */
/* @flag Class.Cyclic CF knh_tClass[(%s)].cflag 'is:set:*:*' */
/* @flag Class.MetaExtensible CF knh_tClass[(%s)].cflag 'is:*:is:*' */
/* @flag Class.Private!Public CF knh_tClass[(%s)].cflag 'is:*:is:*' */
/* @flag Class.Final CF knh_tClass[(%s)].cflag 'is:*:is:*' */
/* @flag Class.Strict CF knh_tClass[(%s)].cflag 'is:*:*:*' */
/* @flag Class.System CF knh_tClass[(%s)].cflag 'is:*:is:*' */
/* @flag Class.NullObject CF knh_tClass[(%s)].cflag 'is:*:*:*' */
/* @flag Class.Interface  CF knh_tClass[(%s)].cflag 'is:*:is:*' */

typedef struct knh_Class_t {
	knh_hObject_t h;
	knh_int_t   cid;
} knh_Class_t;

/* ------------------------------------------------------------------------ */
/* @class ClassStruct Object knh_ @Private */
/* @flag ClassStruct.Hidden CFF ((ClassStruct)%s)->fields[n].flag 'is:set:*:*' */
/* @flag ClassStruct.AutoName CFF ((ClassStruct)%s)->fields[n].flag 'is:set:*:*' */
/* @flag ClassStruct.SafeValue CFF ((ClassStruct)%s)->fields[n].flag 'is:set:*:*' */
/* @flag ClassStruct.Protected CFF ((ClassStruct)%s)->fields[n].flag 'is:set:*:*' */
/* @flag ClassStruct.Getter CFF ((ClassStruct)%s)->fields[n].flag 'is:set:*:*' */
/* @flag ClassStruct.Setter CFF ((ClassStruct)%s)->fields[n].flag 'is:set:*:*' */
/* @flag ClassStruct.Key CFF ((ClassStruct)%s)->fields[n].flag 'is:set:*:*' */

typedef struct knh_cfield_t {
	knh_flag_t    flag  ;
	knh_type_t    type  ;
	knh_fieldn_t  fn    ;
	Object      *value;
} knh_cfield_t ;

typedef struct knh_ClassStruct {
	knh_struct_t sid;
	knh_ushort_t fsize;
	knh_cfield_t* fields;
	knh_Array_t*  methods;
} knh_ClassStruct;

/* ------------------------------------------------------------------------ */
/* @class MethodField Object knh_ */

typedef struct  knh_mfield_t {
	knh_type_t   type  ;
	knh_fieldn_t  fn    ;
} knh_mfield_t ;

typedef struct knh_MethodField {
	knh_flag_t flag;
	knh_ushort_t size;
	knh_mfield_t* params;
} knh_MethodField;

/* ------------------------------------------------------------------------ */
/* @class Method Object knh_Method */
/* @flag Method.Public!Private MF DP((Method*)%s)->flag 'is:set:is:*' */
/* @flag Method.Virtual!Final MF DP((Method*)%s)->flag 'is:set:is:*' */
/* @flag Method.Static MF DP((Method*)%s)->flag 'is:*:is:*' */
/* @flag Method.Class MF DP((Method*)%s)->flag 'is:*:is:*' */
/* @flag Method.NullBase MF DP((Method*)%s)->flag 'is:*:is:*' */
/* @flag Method.VarArgs MF DP((Method*)%s)->flag 'is:set:is:*' */
/* @flag Method.Smart   MF DP((Method*)%s)->flag 'is:*:*:*' */
/* @flag Method.Aspect MF DP((Method*)%s)->flag 'is:set:is:*' */
/* @flag Method.ObjectCode MF DP((Method*)%s)->flag 'is:set:*:*' */
/* @flag Method.Generated MF DP((Method*)%s)->flag 'is:set:*:*' */

typedef struct knh_Method {
	knh_flag_t     flag;  knh_class_t    cid;
	knh_methodn_t  mn;    knh_ushort_t   delta;
	struct knh_MethodField_t* mf;
	f_method       func;
	void*          code;
} knh_Method;

/* ------------------------------------------------------------------------ */
/* @class Mapper Object knh_ */
/* @flag Mapper.Interface MMF DP((Mapper*)%s)->flag     'is:set:is:*' */
/* @flag Mapper.Significant MMF DP((Mapper*)%s)->flag   'is:set:is:*' */
/* @flag Mapper.Final MMF DP((Mapper*)%s)->flag         'is:*:is:*' */
/* @flag Mapper.Synonym MMF DP((Mapper*)%s)->flag       'is:set:is:*' */
/* @flag Mapper.LossLess MMF DP((Mapper*)%s)->flag      'is:set:is:*' */
/* @flag Mapper.Total!Partial MMF DP((Mapper*)%s)->flag 'is:set:is:*' */
/* @flag Mapper.Static!Temporal MMF DP((Mapper*)%s)->flag 'is:set:is:*' */
/* @flag Mapper.Local MMF DP((Mapper*)%s)->flag 'is:set:is:*' */
/* @flag Mapper.Derived MMF DP((Mapper*)%s)->flag 'is:set:is:*' */

typedef struct knh_Mapper {
	knh_flag_t   flag;
	knh_ushort_t size;
	knh_class_t  scid;
	knh_class_t  tcid;
	f_mapper     fmap;
	Object*      mapdata;
} knh_Mapper;

/* ------------------------------------------------------------------------ */
/* @class ClassMap Object knh_ */
/* @flag ClassMap.Sorted CM:1 (%s)->h.flag 'is:set:*:*' */

typedef struct knh_ClassMap {
	struct knh_Mapper_t**   maplist;
	knh_ushort_t   size;
	knh_ushort_t   capacity;
} knh_ClassMap;

/* ------------------------------------------------------------------------ */
/* class Closure Object knh_ */

typedef struct knh_Closure {
	Object* self;
	struct knh_Method_t* method;
} knh_Closure;

/* ------------------------------------------------------------------------ */
/* @class AffineConv Object knh_ */

typedef struct knh_AffineConv {
	knh_float_t scale;
	knh_float_t shift;
} knh_AffineConv;

/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* IntSpec, FloatSpec, StringSpec */

typedef struct knh_cspec_t {
	knh_flag_t  flag;
	knh_class_t cid;
	knh_String_t* urn;
	knh_String_t* tag;
	Object* defvalue;
} knh_cspec_t;

/* ------------------------------------------------------------------------ */
/* @class IntUnit Object knh_ */
/* @flag IntUnit.Unsigned UF DP((IntUnit*)%s)->spec.flag 'is:*:is:*' */

struct  knh_IntUnit_t;
typedef knh_bool_t  (*f_iunit_chk)(struct knh_IntUnit_t *, knh_int_t v);
typedef knh_int_t   (*f_iunit_cmp)(struct knh_IntUnit_t *, knh_int_t v1, knh_int_t v2);
typedef void        (*f_iunit_fmt)(struct knh_IntUnit_t *, char *buf, size_t bufsiz, knh_int_t v);

typedef struct knh_IntUnit {
	knh_cspec_t spec;
	f_iunit_chk fchk;
	f_iunit_cmp fcmp;
	f_iunit_fmt ffmt;
	knh_int_t min;
	knh_int_t max;
	knh_int_t step;
	knh_int_t bfoffset;
	size_t bfsize;
	Int** bfarray;
} knh_IntUnit;

/* ------------------------------------------------------------------------ */
/* @class FloatUnit Object knh_ */

struct knh_FloatUnit_t;
typedef knh_bool_t  (*f_funit_chk)(struct knh_FloatUnit_t *, knh_float_t v);
typedef knh_int_t   (*f_funit_cmp)(struct knh_FloatUnit_t *, knh_float_t v1, knh_float_t v2);
typedef void       (*f_funit_fmt)(struct knh_FloatUnit_t *, char *buf, size_t bufsiz, knh_float_t v);

typedef struct knh_FloatUnit {
	knh_cspec_t spec;
	f_funit_chk fchk;
	f_funit_cmp fcmp;
	f_funit_fmt ffmt;
	knh_float_t min;
	knh_float_t max;
	knh_float_t step;
	char* FMT;
} knh_FloatUnit;

/* ------------------------------------------------------------------------ */
/* @ class Regex Object knh_ */

typedef struct knh_Regex {
	knh_String_t* pstr;
	void * re;
} knh_Regex;


/* ------------------------------------------------------------------------ */
/* @class BytesConv Object knh_ */

struct knh_BytesConv_t;
typedef size_t (*f_bconv)(Ctx *ctx, struct knh_BytesConv_t *o, knh_bytes_t t, knh_Bytes_t *ba);

#ifdef KNH_USING_ICONV
	#include<iconv.h>
#endif

typedef struct knh_BytesConv {
	struct knh_String_t *name;
	f_bconv fbconv;
#ifdef KNH_USING_ICONV
	iconv_t iconv_d;
#endif
} knh_BytesConv;

/* ------------------------------------------------------------------------ */
/* @class StringUnit Object knh_ */

struct knh_StringUnit_t;
typedef knh_String_t *(*f_sunit_new)(Ctx *, knh_class_t cid, knh_bytes_t, struct knh_String_t *);
typedef int (*f_sunit_cmp)(struct knh_StringUnit_t *, knh_bytes_t, knh_bytes_t);

typedef struct knh_StringUnit {
	knh_cspec_t spec;
	f_sunit_new fnew;
	f_sunit_cmp fcmp;
	f_bconv     fbconv;
	struct knh_BytesConv_t *bconv;
	knh_ushort_t bytelen;
	knh_ushort_t charlen;
	Object* pattern;
	Object* vocabDictIdx;
} knh_StringUnit;


/* ------------------------------------------------------------------------ */
/* InputStream, OutputStream */

/* ------------------------------------------------------------------------ */
/* @class InputStream Object knh_ */

struct knh_InputStream;
typedef void knh_inptr_t;

typedef knh_inptr_t* (*f_inptr_open)(Ctx *ctx, knh_bytes_t);
typedef int (*f_inptr_getc)(Ctx *ctx, knh_inptr_t *);
typedef size_t (*f_inptr_read)(Ctx *ctx, knh_inptr_t *, char *buf, size_t bufsiz);
typedef void (*f_inptr_close)(Ctx *ctx, knh_inptr_t *);

typedef struct knh_inptr_apis_t {
	f_inptr_open  fopen;
	f_inptr_getc  fgetc;
	f_inptr_read  fread;
	f_inptr_close fclose;
} knh_inptr_apis_t;

typedef knh_inptr_apis_t* (*f_inptr_drivers)(Ctx *ctx, knh_bytes_t);

typedef struct knh_InputStream {
	knh_inptr_t* inptr;
	knh_Bytes_t* ba;
	size_t  bapos;
	size_t  baend;
	knh_inptr_apis_t apis;
	knh_String_t *enc;
	struct knh_BytesConv_t* bconv;
	knh_String_t*  urn;
	knh_fileid_t  fileid;
	knh_ushort_t  prev;
	size_t line;
	size_t size;
} knh_InputStream;

/* ------------------------------------------------------------------------ */
/* @class OutputStream Object knh_ */
/* @flag OutputStream.BOL OUT:1 (%s)->h.flag 'is:set:*:*' */
/* @flag OutputStream.AutoFlush OUT:2 (%s)->h.flag 'is:set:is:set' */

struct knh_OutputStream;
typedef void knh_outptr_t;

typedef knh_outptr_t* (*f_outptr_open)(Ctx *ctx, knh_bytes_t);
typedef void (*f_outptr_putc)(Ctx *ctx, knh_outptr_t *, int ch);
typedef size_t (*f_outptr_write)(Ctx *ctx, knh_outptr_t *, char *buf, size_t bufsiz);
typedef void (*f_outptr_flush)(Ctx *ctx, knh_outptr_t *);
typedef void (*f_outptr_close)(Ctx *ctx, knh_outptr_t *);

typedef struct knh_outptr_apis_t {
	f_outptr_open   fopen;
	f_outptr_putc   fputc;
	f_outptr_write  fwrite;
	f_outptr_flush  fflush;
	f_outptr_close  fclose;
} knh_outptr_apis_t;

typedef struct knh_OutputStream {
	knh_outptr_t* outptr;
	knh_Bytes_t*   ba;
	knh_outptr_apis_t apis;
	knh_String_t *enc;
	struct knh_BytesConv_t* bconv;
	knh_String_t*  urn;
	size_t size;
	size_t line;
	knh_String_t*  NEWLINE;
	knh_String_t*  TAB;
	size_t indent;
} knh_OutputStream;

/* ------------------------------------------------------------------------ */
/* @ class Socket Object knh_ */

typedef int knh_socket_t;

typedef struct knh_Socket {
	knh_String_t* urn;
	int port;
	knh_socket_t sd;
	FILE *fp_in;
	FILE *fp_out;
	struct knh_InputStream_t  *in;
	struct knh_OutputStream_t *out;
} knh_Socket;

/* ------------------------------------------------------------------------ */
/* @class ResultSet Object knh_ */

struct knh_ResultSet_t;
typedef void        knh_rsptr_t;
typedef knh_bool_t  (*f_rsptr_next)(Ctx *, knh_rsptr_t *, struct knh_ResultSet_t*);
typedef void        (*f_rsptr_free)(knh_rsptr_t *);

typedef struct knh_rsptr_drivers_t {
	f_rsptr_free  ffree;
	f_rsptr_next  fnext;
} knh_rsptr_api_t;

#define knh_ResultSet_CTYPE__null    0
#define knh_ResultSet_CTYPE__int     1
#define knh_ResultSet_CTYPE__int64   2
#define knh_ResultSet_CTYPE__float   3
#define knh_ResultSet_CTYPE__bytes   4

typedef struct knh_rscolumn_t {
	knh_type_t type;
	knh_ushort_t ctype;
	knh_String_t *name;
	size_t start;
	size_t len;
	int dbtype;
} knh_rscolumn_t;

typedef struct knh_ResultSet {
	knh_rsptr_t         *rsptr;
	knh_rsptr_api_t     api;
	knh_String_t        *tableName;
	knh_class_t         tcid;
	knh_ushort_t        column_size;
	knh_rscolumn_t      *column;
	knh_Bytes_t         *databuf;
} knh_ResultSet;

/* ------------------------------------------------------------------------ */
/* @class Connection Object knh_ */

struct knh_Connection;

typedef void   knh_connptr_t;
typedef knh_connptr_t* (*f_connptr_open)(Ctx *ctx, knh_bytes_t url, char *uname, char *upwd);
typedef struct knh_ResultSet_t* (*f_connptr_query)(Ctx *ctx, knh_connptr_t *, char *query);
typedef void  (*f_connptr_close)(Ctx *ctx, knh_connptr_t *);

typedef struct knh_connptr_api_t {
	f_connptr_open  fopen;
	f_connptr_query fquery;
	f_connptr_close fclose;
} knh_connptr_api_t;

typedef struct knh_Connection {
	knh_connptr_t      *conn;
	knh_connptr_api_t   api;
	knh_String_t       *urn;
} knh_Connection;


/* ------------------------------------------------------------------------ */
/* @class Exception Object knh_ */
/* @flag Exception.Fatal EF DP((Exception*)%s)->flag 'is:*:is:*' */
/* @flag Exception.Logging EF DP((Exception*)%s)->flag 'is:*:is:*' */
/* @flag Exception.Generated EF DP((Exception*)%s)->flag 'is:*:is:*' */
/* @flag Exception.Caught EF DP((Exception*)%s)->flag 'is:set:*:*' */

typedef struct knh_Exception {
	knh_flag_t flag;
	knh_expt_t eid;
	knh_String_t* message;
	Object* bag;
	knh_Array_t* traces;
	char* file;
	int line;
} knh_Exception;

/* ------------------------------------------------------------------------ */
/* @class ExceptionHandler Object knh_ @Private */
/* @flag ExceptionHandler.Catching EHF:1 (%s)->h.flag 'is:set:*:*' */

typedef struct knh_ExceptionHandler {
	struct knh_Exception_t* caught;
	jmp_buf jmpbuf;
} knh_ExceptionHandler;

/* ------------------------------------------------------------------------ */
/* @class Script Object knh_Object_t */

#define KNH_SCRIPT_FIELDSIZE   128

typedef struct knh_Script_t {
	knh_hObject_t h;
	Object **fields;
} knh_Script_t ;

/* ------------------------------------------------------------------------ */
/* @class NameSpace Object knh_ @Tuple @Private */

typedef struct knh_NameSpace {
	struct knh_NameSpace_t* parent;
	knh_String_t*           nsname;
	knh_Array_t*            imports;
	struct knh_DictSet_t*   name2cidDictSet;
	struct knh_DictSet_t*   func2cidDictSet;
	struct knh_DictMap_t*   lconstDictMap;
	struct knh_DictMap_t*   tag2urnDictMap;
	struct knh_Script_t*    script;
} knh_NameSpace;

/* ------------------------------------------------------------------------ */
/* @class System Object knh_ @Tuple */

typedef struct knh_System {
	struct knh_DictMap_t* props;
	struct knh_NameSpace_t* ns;
	struct knh_String_t*       enc;
	struct knh_InputStream_t*  in;
	struct knh_OutputStream_t* out;
	struct knh_OutputStream_t* err;
	struct knh_DictIdx_t *tfileidDictIdx;
	struct knh_DictIdx_t *tfieldnDictIdx;
	struct knh_DictMap_t *tConstDictMap;
	knh_String_t         *homeDir;
	struct knh_DictIdx_t *pkgmgrDictSet;
	struct knh_DictSet_t *dylibDictSet;
	struct knh_DictSet_t *funcDictSet;
} knh_System;

/* ------------------------------------------------------------------------ */
/* @class Runtime Object knh_ */

typedef struct knh_Runtime {
	knh_uint_t runtime_id;
	size_t     ctxcount;
	struct knh_HashMap_t* tmetaHashMap;
	struct knh_DictMap_t* tnameSpaceDictMap;
//	struct knh_DictMap_t* tnameContextDictMap;
	struct knh_HashMap_t* tmapperHashMap;
	struct knh_HashMap_t* tmethodHashMap;
} knh_Runtime;

/* ------------------------------------------------------------------------ */
/* @class Context Object knh_Object_t */
/* @flag Context.Verbose CTX ((Context*)%s)->flag 'is:set:*:*' */


/* ------------------------------------------------------------------------ */
/* class Mutex Object knh_ */

typedef struct knh_Mutex {
	knh_uint_t magic;
	knh_uint_t loccount;
	knh_mutex_t mutex;
} knh_Mutex;

/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

#define KONOHA_TABLE_MAGIC   4219881

#define TSTRUCTDB_SIZE  (KONOHA_TSTRUCT_SIZE * sizeof(knh_tstruct_t))

/* ------------------------------------------------------------------------ */

//#ifndef KONOHA_TCLASS_SIZE
//#define KONOHA_TCLASS_SIZE 256
//#endif
//
//#define TCLASSDB_SIZE  (KONOHA_TCLASS_SIZE * sizeof(knh_tclass_t))
//
//#ifndef KONOHA_TFIELDN_SIZE
//#define KONOHA_TFIELDN_SIZE 256
//#endif
//
//#ifndef KONOHA_TMETHODN_SIZE
//#define KONOHA_TMETHODN_SIZE 512
//#endif
//
///* ------------------------------------------------------------------------ */
//
//typedef struct {
//	knh_flag_t   flag;
//	knh_expt_t   parent;
//	struct knh_String_t     *name;
//} knh_texpt_t;
//
//#ifndef KONOHA_TEXPT_SIZE
//#define KONOHA_TEXPT_SIZE (KONOHA_TCLASS_SIZE/4)
//#endif
//
//#define TEXPTDB_SIZE  (KONOHA_TEXPT_SIZE * sizeof(knh_texpt_t))

/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* include generated header file */

#ifdef __cplusplus
}
#endif

#endif /*CLASS_KONOHA_H_*/

