G_DELETE(2) Demeter Software G_DELETE(2) NAME g_delete - sends the generic delete message to an object. SYNOPSIS void Universal:: _g__d_e_l_e_t_e ( char* fileName ) DESCRIPTION _g__d_e_l_e_t_e , when sent to a given object, regards this object as a graph and recursively deletes a subgraph of this object graph. The subgraph used as reference for the deletion is described textually in _f_i_l_e_N_a_m_e . If _f_i_l_e_N_a_m_e is not pro- vided, _g__d_e_l_e_t_e deletes the entire object receiving the _g__d_e_l_e_t_e message. The receiving object must be a tree object. That is, it cannot have any cycles or shared subob- jects. _g__d_e_l_e_t_e is a generic method implemented at class Universal and inherited by any user class. _g__d_e_l_e_t_e deletes the subgraph of the receiving object graph by recursively traversing the receiving object graph in a post-order fashion, traversing the neighbors of a given node and then deleting the node. Since a node in an object graph is itself an object, _g__d_e_l_e_t_e deletes a node by invoking the C++ _d_e_l_e_t_e operation on the corresponding object. USAGE Here is an example of how to use _g__d_e_l_e_t_e . Suppose the fol- lowing class dictionary graph describes an object to be deleted: Example = Prefix_list. Prefix_list ~ { Prefix }. Prefix : Compound | Simple. Compound = "(" Op Prefix_list ")". Simple = DemNumber. Op : Plus. Plus = "+". The following code reads in and makes a copy of an object of class _E_x_a_m_p_l_e and deletes the copied object. Example* iExample = new Example(); if ( ( Example* ) iExample -> g_parse( Dem_input ) == NULL ) { cerr << "Parser error." << endl; exit(1); } Example* nExample = ( Example* ) iExample -> g_copy(); nExample->g_delete(); Suppose we now want to delete only _C_o_m_p_o_u_n_d objects and Demeter Last change: 28 September 1993 1 G_DELETE(2) Demeter Software G_DELETE(2) within them, only their _a_r_g_s parts. The following class dic- tionary graph, contained in file _c_d-_d_e_l_e_t_e describes the subgraph that should be given to _g__d_e_l_e_t_e . Example = Prefix_list. Prefix_list ~ { Prefix }. Prefix : Compound. Compound = "(" Prefix_list ")". Op : Plus. Plus = "+". The following code should then read in and make a copy of an object of class _E_x_a_m_p_l_e and delete the subobject described by the required subgraph from the copied object. Example* iExample = new Example(); if ( ( Example* ) iExample -> g_parse( Dem_input ) == NULL ) { cerr << "Parser error." << endl; exit(1); } Example* nExample = ( Example* ) iExample -> g_copy(); nExample->g_delete( "cd-delete" ); Notice that the only difference between this piece of code and the original is the argument file name. ASSUMPTIONS Since _g__d_e_l_e_t_e uses the C++ _d_e_l_e_t_e operation to delete a node once its neighbors have been traversed, it should be noted that using _g__d_e_l_e_t_e assumes the following. No destruc- tor should perform any _d_e_l_e_t_e operation on an immediate part, for any object of the subgraph being deleted. The reson for this is that otherwise objects would be deleted more than once, as the following example illustrates. Sup- pose the object to be deleted is described by the following class dictionary graph. A = B. B = C. C = . A simplified rendition of _g__d_e_l_e_t_e for the class dictionary graph at hand is: void A::g_delete() { b->g_delete(); delete b; } void B::g_delete() { c->g_delete(); delete c; } void C::g_delete() { /* empty */ } Also suppose that the assumption is not true and that the destructors for the given classes contain the following Demeter Last change: 28 September 1993 2 G_DELETE(2) Demeter Software G_DELETE(2) code. A::~A() { delete b; } B::~B() { delete c; } C::~C() { /* empty */ } Then, executing the following code: C *c = new C; B *b = new B(c); A *a = new A(b); a -> g_delete(); will destroy b's and c's storage twice, which may result in a memory error. SEE ALSO g_parse(2), g_copy(2) REFERENCES _U_s_e_r _M_a_n_u_a_l _f_o_r _T_h_e _C++ _D_e_m_e_t_e_r Walter L. Hursch Northeastern University, 1993 _T_h_e _A_n_n_o_t_a_t_e_d _C++ _R_e_f_e_r_e_n_c_e _M_a_n_u_a_l Margaret A. Ellis and Bjarne Stroustrup Addison-Wesley, 1990 _C++ _P_r_i_m_e_r Stanley B. Lippman Addison-Wesley, 1989 Demeter Last change: 28 September 1993 3