1f08c3bdfSopenharmony_ci
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * ast-model.h
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * Copyright (C) 2010 Christopher Li.
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#ifndef _ast_model_h_
10f08c3bdfSopenharmony_ci#define _ast_model_h_
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include <stdint.h>
13f08c3bdfSopenharmony_ci#include <gtk/gtk.h>
14f08c3bdfSopenharmony_ci#include "lib.h"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#define AST_TYPE_NODE                  (ast_get_type ())
17f08c3bdfSopenharmony_ci#define AST_NODE(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), AST_TYPE_NODE, AstNode))
18f08c3bdfSopenharmony_ci#define AST_NODE_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST ((klass),  AST_TYPE_NODE, AstNodeClass))
19f08c3bdfSopenharmony_ci#define AST_IS_NODE(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), AST_TYPE_NODE))
20f08c3bdfSopenharmony_ci#define AST_IS_NODE_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE ((klass),  AST_TYPE_NODE))
21f08c3bdfSopenharmony_ci#define AST_NODE_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj),  AST_TYPE_NODE, AstNodeClass))
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cienum
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	AST_COL_RECORD = 0,
26f08c3bdfSopenharmony_ci	AST_COL_NAME,
27f08c3bdfSopenharmony_ci	AST_N_COLUMNS,
28f08c3bdfSopenharmony_ci} ;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_citypedef struct AstNode AstNode;
32f08c3bdfSopenharmony_citypedef struct AstNodeClass AstNodeClass;
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci/* AstNode: this structure contains everything we need for our
37f08c3bdfSopenharmony_ci *             model implementation. You can add extra fields to
38f08c3bdfSopenharmony_ci *             this structure, e.g. hashtables to quickly lookup
39f08c3bdfSopenharmony_ci *             rows or whatever else you might need, but it is
40f08c3bdfSopenharmony_ci *             crucial that 'parent' is the first member of the
41f08c3bdfSopenharmony_ci *             structure.                                          */
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistruct AstNode
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	GObject         base;      /* this MUST be the first member */
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	AstNode	*parent;
48f08c3bdfSopenharmony_ci	int index;
49f08c3bdfSopenharmony_ci	const gchar *text;
50f08c3bdfSopenharmony_ci	void (*inspect)(struct AstNode* node);
51f08c3bdfSopenharmony_ci	void *ptr;
52f08c3bdfSopenharmony_ci	GArray *childnodes;
53f08c3bdfSopenharmony_ci	gint stamp;
54f08c3bdfSopenharmony_ci};
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci/* AstNodeClass: more boilerplate GObject stuff */
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistruct AstNodeClass
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	GObjectClass base_class;
63f08c3bdfSopenharmony_ci};
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ciGType ast_get_type(void);
67f08c3bdfSopenharmony_ciAstNode* ast_new(AstNode *parent, int index, const char *prefix, void *ptr, void (*expand)(AstNode*));
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic inline
71f08c3bdfSopenharmony_ciAstNode* ast_append_child(AstNode *parent, const char *text,
72f08c3bdfSopenharmony_ci			   void *ptr, void (*inspect)(AstNode*))
73f08c3bdfSopenharmony_ci{
74f08c3bdfSopenharmony_ci	if (ptr) {
75f08c3bdfSopenharmony_ci		AstNode *child = ast_new(parent, parent->childnodes->len,
76f08c3bdfSopenharmony_ci						text, ptr, inspect);
77f08c3bdfSopenharmony_ci		g_array_append_val(parent->childnodes, child);
78f08c3bdfSopenharmony_ci		return child;
79f08c3bdfSopenharmony_ci	}
80f08c3bdfSopenharmony_ci	return NULL;
81f08c3bdfSopenharmony_ci}
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistatic inline
84f08c3bdfSopenharmony_civoid ast_append_attribute(AstNode *parent, const char *text)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci	AstNode *child = ast_new(parent, parent->childnodes->len, text, NULL, NULL);
87f08c3bdfSopenharmony_ci	g_array_append_val(parent->childnodes, child);
88f08c3bdfSopenharmony_ci}
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci#endif /* _ast_h_*/
91