Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

dtool/src/interrogate/interfaceMakerC.cxx

Go to the documentation of this file.
00001 // Filename: interfaceMakerC.cxx
00002 // Created by:  drose (25Sep01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "interfaceMakerC.h"
00020 #include "interrogateBuilder.h"
00021 #include "interrogate.h"
00022 #include "functionRemap.h"
00023 #include "parameterRemapUnchanged.h"
00024 #include "typeManager.h"
00025 
00026 #include "interrogateDatabase.h"
00027 #include "interrogateType.h"
00028 #include "interrogateFunction.h"
00029 #include "cppFunctionType.h"
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: InterfaceMakerC::Constructor
00033 //       Access: Public
00034 //  Description:
00035 ////////////////////////////////////////////////////////////////////
00036 InterfaceMakerC::
00037 InterfaceMakerC(InterrogateModuleDef *def) :
00038   InterfaceMaker(def)
00039 {
00040 }
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //     Function: InterfaceMakerC::Destructor
00044 //       Access: Public, Virtual
00045 //  Description:
00046 ////////////////////////////////////////////////////////////////////
00047 InterfaceMakerC::
00048 ~InterfaceMakerC() {
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: InterfaceMakerC::write_prototypes
00053 //       Access: Public, Virtual
00054 //  Description: Generates the list of function prototypes
00055 //               corresponding to the functions that will be output in
00056 //               write_functions().
00057 ////////////////////////////////////////////////////////////////////
00058 void InterfaceMakerC::
00059 write_prototypes(ostream &out) {
00060   Functions::iterator fi;
00061   for (fi = _functions.begin(); fi != _functions.end(); ++fi) {
00062     Function *func = (*fi);
00063     write_prototype_for(out, func);
00064   }
00065 
00066   out << "\n";
00067   InterfaceMaker::write_prototypes(out);
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: InterfaceMakerC::write_functions
00072 //       Access: Public, Virtual
00073 //  Description: Generates the list of functions that are appropriate
00074 //               for this interface.  This function is called *before*
00075 //               write_prototypes(), above.
00076 ////////////////////////////////////////////////////////////////////
00077 void InterfaceMakerC::
00078 write_functions(ostream &out) {
00079   Functions::iterator fi;
00080   for (fi = _functions.begin(); fi != _functions.end(); ++fi) {
00081     Function *func = (*fi);
00082     write_function_for(out, func);
00083   }
00084 
00085   InterfaceMaker::write_functions(out);
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: InterfaceMakerC::synthesize_this_parameter
00090 //       Access: Public, Virtual
00091 //  Description: This method should be overridden and redefined to
00092 //               return true for interfaces that require the implicit
00093 //               "this" parameter, if present, to be passed as the
00094 //               first parameter to any wrapper functions.
00095 ////////////////////////////////////////////////////////////////////
00096 bool InterfaceMakerC::
00097 synthesize_this_parameter() {
00098   return true;
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: InterfaceMakerC::get_wrapper_prefix
00103 //       Access: Protected, Virtual
00104 //  Description: Returns the prefix string used to generate wrapper
00105 //               function names.
00106 ////////////////////////////////////////////////////////////////////
00107 string InterfaceMakerC::
00108 get_wrapper_prefix() {
00109   return "_inC";
00110 }
00111 
00112 ////////////////////////////////////////////////////////////////////
00113 //     Function: InterfaceMakerC::get_unique_prefix
00114 //       Access: Protected, Virtual
00115 //  Description: Returns the prefix string used to generate unique
00116 //               symbolic names, which are not necessarily C-callable
00117 //               function names.
00118 ////////////////////////////////////////////////////////////////////
00119 string InterfaceMakerC::
00120 get_unique_prefix() {
00121   return "c";
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: InterfaceMakerC::record_function_wrapper
00126 //       Access: Protected, Virtual
00127 //  Description: Associates the function wrapper with its function in
00128 //               the appropriate structures in the database.
00129 ////////////////////////////////////////////////////////////////////
00130 void InterfaceMakerC::
00131 record_function_wrapper(InterrogateFunction &ifunc, 
00132                         FunctionWrapperIndex wrapper_index) {
00133   ifunc._c_wrappers.push_back(wrapper_index);
00134 }
00135 
00136 ////////////////////////////////////////////////////////////////////
00137 //     Function: InterfaceMakerC::write_prototype_for
00138 //       Access: Private
00139 //  Description: Writes the prototype for the indicated function.
00140 ////////////////////////////////////////////////////////////////////
00141 void InterfaceMakerC::
00142 write_prototype_for(ostream &out, InterfaceMaker::Function *func) {
00143   Function::Remaps::const_iterator ri;
00144 
00145   for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
00146     FunctionRemap *remap = (*ri);
00147     if (output_function_names) {
00148       out << "extern \"C\" ";
00149     }
00150     write_function_header(out, func, remap, false);
00151     out << ";\n";
00152   }
00153 }
00154 
00155 ////////////////////////////////////////////////////////////////////
00156 //     Function: InterfaceMakerC::write_function_for
00157 //       Access: Private
00158 //  Description: Writes the definition for a function that will call
00159 //               the indicated C++ function or method.
00160 ////////////////////////////////////////////////////////////////////
00161 void InterfaceMakerC::
00162 write_function_for(ostream &out, InterfaceMaker::Function *func) {
00163   Function::Remaps::const_iterator ri;
00164 
00165   for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
00166     FunctionRemap *remap = (*ri);
00167     write_function_instance(out, func, remap);
00168   }
00169 }
00170 
00171 ////////////////////////////////////////////////////////////////////
00172 //     Function: InterfaceMakerC::write_function_instance
00173 //       Access: Private
00174 //  Description: Writes out the particular function that handles a
00175 //               single instance of an overloaded function.
00176 ////////////////////////////////////////////////////////////////////
00177 void InterfaceMakerC::
00178 write_function_instance(ostream &out, InterfaceMaker::Function *func,
00179                         FunctionRemap *remap) {
00180   out << "/*\n"
00181       << " * C wrapper for\n"
00182       << " * ";
00183   remap->write_orig_prototype(out, 0);
00184   out << "\n"
00185       << " */\n";
00186     
00187   if (!output_function_names) {
00188     // If we're not saving the function names, don't export it from
00189     // the library.
00190     out << "static ";
00191   }
00192 
00193   write_function_header(out, func, remap, true);
00194   out << " {\n";
00195 
00196   if (generate_spam) {
00197     write_spam_message(out, remap);
00198   }
00199     
00200   string return_expr = 
00201     remap->call_function(out, 2, true, "param0");
00202   return_expr = manage_return_value(out, 2, remap, return_expr);
00203   if (!return_expr.empty()) {
00204     out << "  return " << return_expr << ";\n";
00205   }
00206   
00207   out << "}\n\n";
00208 }
00209     
00210 ////////////////////////////////////////////////////////////////////
00211 //     Function: InterfaceMakerC::write_function_header
00212 //       Access: Private
00213 //  Description: Writes the first line of a function definition,
00214 //               either for a prototype or a function body.
00215 ////////////////////////////////////////////////////////////////////
00216 void InterfaceMakerC::
00217 write_function_header(ostream &out, InterfaceMaker::Function *func,
00218                       FunctionRemap *remap, bool newline) {
00219   if (remap->_void_return) {
00220     out << "void";
00221   } else {
00222     out << remap->_return_type->get_new_type()->get_local_name(&parser);
00223   }
00224   if (newline) {
00225     out << "\n";
00226   } else {
00227     out << " ";
00228   }
00229 
00230   out << remap->_wrapper_name << "(";
00231   int pn = 0;
00232   if (pn < (int)remap->_parameters.size()) {
00233     remap->_parameters[pn]._remap->get_new_type()->
00234       output_instance(out, remap->get_parameter_name(pn), &parser);
00235     pn++;
00236     while (pn < (int)remap->_parameters.size()) {
00237       out << ", ";
00238       remap->_parameters[pn]._remap->get_new_type()->
00239         output_instance(out, remap->get_parameter_name(pn), &parser);
00240       pn++;
00241     }
00242   }
00243   out << ")";
00244 }

Generated on Thu May 1 22:13:01 2003 for DTool by doxygen1.3