comparison nodejs/coord.cc @ 745:4ccb0553e804

Refactor code of free C objects into xnjsmb_coord_free_subtree
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 25 Aug 2010 14:43:48 +0800
parents 6a988e23ad2a
children 1dbc74a14199
comparison
equal deleted inserted replaced
744:6a988e23ad2a 745:4ccb0553e804
66 * \param self is the object of the root of subtree. 66 * \param self is the object of the root of subtree.
67 * 67 *
68 * \sa \ref jsgc 68 * \sa \ref jsgc
69 */ 69 */
70 static void 70 static void
71 xnjsmb_coord_invalidate_subtree(Handle<Object> self) { 71 xnjsmb_coord_invalidate_subtree(coord_t *coord) {
72 Persistent<Object> *child_hdl; 72 Persistent<Object> *child_hdl;
73 Persistent<Object> *mem_hdl; 73 Persistent<Object> *mem_hdl;
74 coord_t *coord, *child, *last_child; 74 coord_t *child;
75 Handle<Object> js_rt; 75 shape_t *mem;
76 redraw_man_t *rdman; 76 Handle<Value> _false = Boolean::New(0);
77
78 /* Invalidate all coords in the subtree */
79 FOR_COORDS_PREORDER(coord, child) {
80 child_hdl = (Persistent<Object> *)mb_prop_get(&child->obj.props,
81 PROP_JSOBJ);
82 SET(*child_hdl, "valid", _false);
83 WRAP(*child_hdl, NULL);
84
85 /* Invalidate members of a coord */
86 FOR_COORD_SHAPES(child, mem) {
87 mem_hdl = (Persistent<Object> *)mb_prop_get(&mem->obj.props,
88 PROP_JSOBJ);
89 SET(*mem_hdl, "valid", _false);
90 WRAP(*mem_hdl, NULL);
91 }
92 }
93 }
94
95 /*! \brief Free C objects for coords and shapes in a subtree.
96 *
97 * \param self is the object of the root of subtree.
98 *
99 * \sa \ref jsgc
100 */
101 static void
102 xnjsmb_coord_free_subtree(redraw_man_t *rdman, coord_t *coord) {
103 coord_t *child, *last_child;
77 shape_t *mem, *last_mem; 104 shape_t *mem, *last_mem;
78 Handle<Value> _false = Boolean::New(0);
79 int r; 105 int r;
80 106
81 if(!GET(self, "valid")->ToBoolean()->Value()) /* Invalidated object */
82 return;
83
84 js_rt = GET(self, "mbrt")->ToObject();
85 rdman = xnjsmb_rt_rdman(js_rt);
86
87 coord = (coord_t *)UNWRAP(self);
88 rdman_coord_changed(rdman, coord); 107 rdman_coord_changed(rdman, coord);
89 108
90 /* Invalidate all coords in the subtree */
91 last_child = NULL; 109 last_child = NULL;
92 FOR_COORDS_POSTORDER(coord, child) { 110 FOR_COORDS_POSTORDER(coord, child) {
93 if(last_child != NULL) { 111 if(last_child != NULL) {
94 r = rdman_coord_free(rdman, coord); 112 r = rdman_coord_free(rdman, coord);
95 if(r != OK) 113 if(r != OK)
96 THROW_noret("Unknown error"); 114 THROW_noret("Unknown error");
97 } 115 }
98
99 child_hdl = (Persistent<Object> *)mb_prop_get(&child->obj.props,
100 PROP_JSOBJ);
101 SET(*child_hdl, "valid", _false);
102 WRAP(*child_hdl, NULL);
103 116
104 /* Invalidate members of a coord */ 117 /* Free members of a coord */
105 last_mem = NULL; 118 last_mem = NULL;
106 FOR_COORD_SHAPES(child, mem) { 119 FOR_COORD_SHAPES(child, mem) {
107 if(last_mem != NULL) { 120 if(last_mem != NULL) {
108 r = rdman_shape_free(rdman, last_mem); 121 r = rdman_shape_free(rdman, last_mem);
109 if(r != OK) 122 if(r != OK)
110 THROW_noret("Unknown error"); 123 THROW_noret("Unknown error");
111 } 124 }
112 125
113 mem_hdl = (Persistent<Object> *)mb_prop_get(&mem->obj.props,
114 PROP_JSOBJ);
115 SET(*mem_hdl, "valid", _false);
116 WRAP(*mem_hdl, NULL);
117
118 last_mem = mem; 126 last_mem = mem;
119 } 127 }
120 if(last_mem != NULL) { 128 if(last_mem != NULL) {
121 r = rdman_shape_free(rdman, last_mem); 129 r = rdman_shape_free(rdman, last_mem);
122 if(r != OK) 130 if(r != OK)
195 if(r != 0) 203 if(r != 0)
196 *err = "Unknown error"; 204 *err = "Unknown error";
197 } 205 }
198 206
199 static void 207 static void
200 xnjsmb_coord_remove(coord_t *coord, Handle<Object> self, const char **err) { 208 xnjsmb_coord_remove(coord_t *coord, Handle<Object> self) {
201 xnjsmb_coord_invalidate_subtree(self); 209 Handle<Object> js_rt;
210 redraw_man_t *rdman;
211
212 if(!GET(self, "valid")->ToBoolean()->Value()) /* Invalidated object */
213 THROW_noret("Invalid object");
214
215 js_rt = GET(self, "mbrt")->ToObject();
216 rdman = xnjsmb_rt_rdman(js_rt);
217
218 xnjsmb_coord_invalidate_subtree(coord);
219 xnjsmb_coord_free_subtree(rdman, coord);
202 } 220 }
203 221
204 #include "coord-inc.h" 222 #include "coord-inc.h"
205 223
206 /*! \brief This function used by \ref xnjsmb_mb_rt to wrap coord object. 224 /*! \brief This function used by \ref xnjsmb_mb_rt to wrap coord object.