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