G_CODE(2) Demeter Software G_CODE(2) NAME g_code - generates C++ code that creates an identical copy of the object SYNOPSIS char* _g__c_o_d_e ( char* ); char* _g__c_o_d_e ( ofstream& ); virtual char* _g__c_o_d_e ( int&,ofstream& ); DESCRIPTION _g__c_o_d_e generates C++ code that makes, when executed, an instance of an object which is an identical copy of the object to which the message was sent. The code generated can then be integrated into a C++ program. The receiving object must be a tree object. That is, it cannot have any cycles or shared subobjects. ARGUMENTS char* _g__c_o_d_e ( char* ) The input argument is the name of the file you want to store the generated C++ code. char* _g__c_o_d_e ( ofstream& ) The input argument is the _o_f_s_t_r_e_a_m variable to which you want to write the generated C++ code. virtual char* _g__c_o_d_e ( int& idCnt,ofstream& ) _g__c_o_d_e generates a number of temporary variables. The names for each of these variable start with "i" followed by the class name of an object and ends with a number. _i_d_C_n_t refers to this number. Every time g_code generates a variable, it will increment the value of this number. If you use _c_h_a_r* _g__c_o_d_e( _o_f_s_t_r_e_a_m& ), the number starts from 1. The second argument is the _o_f_s_t_r_e_a_m variable to which you want to write the generated C++ code. RETURN VALUES _g__c_o_d_e returns a character pointer that points to a string that contains the name of the variable where the copy of the object is stored. For example: Compound* iCompound = new Compound(); Demeter Last change: 3 May 1991 1 G_CODE(2) Demeter Software G_CODE(2) iCompound = (Compound*)iCompound->g_parse("demeter-input"); char* variableName = iCompound->g_code("code.c"); In file _c_o_d_e._c, there is some C++ code to create an identi- cal copy of Compound-object, iCompound. The address of the resulting object is assigned to the variable whose name is stored in _v_a_r_i_a_b_l_e_N_a_m_e. Notice that as a side effect _g__c_o_d_e will print the code to the stream or file with filename given. As a consequence, if you plan to use the returned value of _g__c_o_d_e, namely _v_a_r_i_a_- _b_l_e_N_a_m_e to print it to the same stream or file, you should be careful to first store the name in a variable and then print it, to give _g__c_o_d_e time to do its printing and then return its value. The following example shows how NOT to use the value returned by _g__c_o_d_e. Suppose the variable _c_o_d_e_S_- _t_r_e_a_m of type ostream is used to hold the stream of file _c_o_d_e._c. Compound* iCompound = new Compound(); iCompound = (Compound*)iCompound->g_parse("demeter-input"); codeStream << "iCompound->g_equal(" << iCompound->g_code("code.c") // WRONG !!! << ");\n"; Here, the string "iCompund->g_equal" is printed first to the stream, then the side effect of _g__c_o_d_e, and then the value of _g__c_o_d_e, which is not what is intended. The correct way of doing it would be as follows. Compound* iCompound = new Compound(); iCompound = (Compound*)iCompound->g_parse("demeter-input"); char* variableName = iCompound->g_code("code.c"); codeStream << "iCompound->g_equal(" << variableName << ");\n"; // OK REDEFINITION Since _g__c_o_d_e with the interface _c_h_a_r* _g__c_o_d_e( _i_n_t&,_o_f_s_t_r_e_a_m& ), is a virtual function, it can be redefined at any user class. This means that the user can change the code _g__c_o_d_e generates for any particular object. As a technical con- sideration, making _g__c_o_d_e virtual allows to define it recur- sively and to give the termination condition as a redefini- tion at the terminal classes DemIdent, DemNumber, DemReal, DemString, and DemText. Demeter Last change: 3 May 1991 2 G_CODE(2) Demeter Software G_CODE(2) SEE ALSO g_parse(2), g_equal(2), terminal(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: 3 May 1991 3