cxx_gen.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- python -*-
00003 #
00004 #  @file cxx_gen.py
00005 #  @brief rtc-template C++ source code generator class
00006 #  @date $Date: 2007/07/20 17:28:56 $
00007 #  @author Noriaki Ando <n-ando@aist.go.jp>
00008 # 
00009 #  Copyright (C) 2004-2007
00010 #      Task-intelligence Research Group,
00011 #      Intelligent Systems Research Institute,
00012 #      National Institute of
00013 #          Advanced Industrial Science and Technology (AIST), Japan
00014 #      All rights reserved.
00015 # 
00016 #  $Id: cxx_gen.py,v 1.8.2.1 2007/07/20 17:28:56 n-ando Exp $
00017 # 
00018 
00019 #
00020 #  $Log: cxx_gen.py,v $
00021 #  Revision 1.8.2.1  2007/07/20 17:28:56  n-ando
00022 #  A fix for win32 porting.
00023 #
00024 #  Revision 1.8  2007/05/29 01:44:29  n-ando
00025 #  make clean target was modified to delete *Compl.o
00026 #
00027 #  Revision 1.7  2007/04/23 01:41:16  n-ando
00028 #  New option "--config" and configuration template code were added.
00029 #
00030 #  Revision 1.6  2007/04/13 15:05:50  n-ando
00031 #  Now RTC::OK becomes RTC::RTC_OK in RTC.idl.
00032 #
00033 #  Revision 1.5  2007/01/14 23:38:30  n-ando
00034 #  Updated to use setModuleInitProc() to initialize component locally.
00035 #
00036 #  Revision 1.4  2007/01/11 07:46:38  n-ando
00037 #  Modified for OMG RTC specificatin and OpenRTM-aist-0.4.0.
00038 #  Almost all template codes were modified according to the new framework.
00039 #
00040 #  Revision 1.3  2005/09/08 09:23:55  n-ando
00041 #  - A bug fix for merge function.
00042 #
00043 #  Revision 1.2  2005/09/06 14:37:03  n-ando
00044 #  rtc-template's command options and data structure for ezt (Easy Template)
00045 #  are changed for RTComponent's service features.
00046 #  Now rtc-template can generate services' skeletons, stubs and
00047 #  implementation files.
00048 #  The implementation code generation uses omniidl's IDL parser.
00049 #
00050 #  Revision 1.1  2005/08/26 12:02:14  n-ando
00051 #  This code generator module uses ezt (Easy Template).
00052 #
00053 #  Revision 1.1.1.1  2005/05/12 09:06:18  n-ando
00054 #  Public release.
00055 #
00056 #
00057 
00058 import re
00059 import os
00060 import sys
00061 import StringIO
00062 import ezt
00063 import gen_base
00064 
00065 
00066 def description():
00067         return "C++ component code generator"
00068 
00069 def usage_short():
00070         """
00071         C++ generator specific usage (short version)
00072         """
00073         return """
00074 Options for C++ backend:
00075 
00076     [--svc-impl-suffix[=suffix]]          Suffix of implementation class
00077     [--svc-skel-suffix[=suffix]]          Suffix of server skeleton files
00078     [--svc-stub-suffix[=suffix]]          Suffix of client stub files
00079 """
00080 
00081 def usage():
00082         """
00083         C++ generator specific usage
00084         """
00085         return """
00086 -------------------------------
00087   Help for C++ code geenrator
00088 -------------------------------
00089     --svc-impl-suffix=[Suffix]:
00090         Specify the suffix for implementation class name. This suffix is also
00091         used for implementation class header file and code file.
00092         
00093     --svc-skel-suffix=[Suffix]:
00094         Specify the suffix for server skeleton files.
00095         
00096     --svc-stub-suffix=[Suffix]:
00097         Specify the suffix for client stub files.
00098 
00099 C++ code generator generates the following files.
00100     [Component name].h.............Component class header
00101     [Component name].cpp...........Component class soruce code
00102     [Component name]Comp.cpp.......Component startup code
00103     [IDL basename]Skel.h...........Server skeleton header
00104     [IDL basename]Skel.cpp.........Server skeleton source code
00105     [IDL basename]Stub.h...........Client stub header
00106     [IDL basename]Stub.cpp.........Client stub source code
00107     [IDL basename]SVC_impl.h.......Server implementation header
00108     [IDL basename]SVC_impl.cpp.....Server implementation source code
00109     Makefile.[Component name]......Makefile to compile this codes
00110     README.[Component name]........Specification template of the component
00111 
00112  Suffixes (Skel, Stub, SVC_impl) can be specified --svc-*-suffix option
00113 
00114  Other CORBA implementation specific skeleton/stub code would be generated.
00115  --omniORB--
00116     [IDL basename].hh..............Client stub header
00117     [IDL basename]SK.cc............Server skeleton source code
00118     [IDL basename]DynSK.cc.........Dynamic server skeleton source code
00119  --TAO--
00120     [IDL basename]S.h..............Server skeleton header
00121     [IDL basename]S.h..............Server skeleton source code
00122     [IDL basename]C.h..............Client stub header
00123     [IDL basename]C.h..............Client stub source code
00124  --MICO--
00125     [IDL basename].h...............Server skeleton header
00126     [IDL basename]_skel.cc.........Server skeleton source code
00127     [IDL basename].h...............Client stub header
00128     [IDL basename].cc..............Client stub source code
00129 
00130 """
00131 
00132 def get_opt_fmt():
00133         opt_args_fmt = ["svc-impl-suffix=",
00134                         "svc-skel-suffix=",
00135                         "svc-stub-suffix="]
00136         return opt_args_fmt
00137 
00138         
00139 #------------------------------------------------------------
00140 # Component header template code
00141 #------------------------------------------------------------
00142 comp_header = """// -*- C++ -*-
00143 /*!
00144  * @file  [fname_h]
00145  * @brief [module.desc]
00146  * @date  [rcs_date]
00147  *
00148  * [rcs_id]
00149  */
00150 
00151 #ifndef [u_name]_H
00152 #define [u_name]_H
00153 
00154 #include <rtm/Manager.h>
00155 #include <rtm/DataFlowComponentBase.h>
00156 #include <rtm/CorbaPort.h>
00157 #include <rtm/DataInPort.h>
00158 #include <rtm/DataOutPort.h>
00159 #include <rtm/idl/BasicDataTypeSkel.h>
00160 
00161 // Service implementation headers
00162 // <rtc-template block="service_impl_h">
00163 // </rtc-template>
00164 
00165 // Service Consumer stub headers
00166 // <rtc-template block="consumer_stub_h">
00167 // </rtc-template>
00168 
00169 using namespace RTC;
00170 
00171 class [module.name]
00172   : public RTC::DataFlowComponentBase
00173 {
00174  public:
00175   [module.name](RTC::Manager* manager);
00176   ~[module.name]();
00177 
00178   // The initialize action (on CREATED->ALIVE transition)
00179   // formaer rtc_init_entry() 
00180  virtual RTC::ReturnCode_t onInitialize();
00181 
00182   // The finalize action (on ALIVE->END transition)
00183   // formaer rtc_exiting_entry()
00184   // virtual RTC::ReturnCode_t onFinalize();
00185 
00186   // The startup action when ExecutionContext startup
00187   // former rtc_starting_entry()
00188   // virtual RTC::ReturnCode_t onStartup(RTC::UniqueId ec_id);
00189 
00190   // The shutdown action when ExecutionContext stop
00191   // former rtc_stopping_entry()
00192   // virtual RTC::ReturnCode_t onShutdown(RTC::UniqueId ec_id);
00193 
00194   // The activated action (Active state entry action)
00195   // former rtc_active_entry()
00196   // virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id);
00197 
00198   // The deactivated action (Active state exit action)
00199   // former rtc_active_exit()
00200   // virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id);
00201 
00202   // The execution action that is invoked periodically
00203   // former rtc_active_do()
00204   // virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id);
00205 
00206   // The aborting action when main logic error occurred.
00207   // former rtc_aborting_entry()
00208   // virtual RTC::ReturnCode_t onAborting(RTC::UniqueId ec_id);
00209 
00210   // The error action in ERROR state
00211   // former rtc_error_do()
00212   // virtual RTC::ReturnCode_t onError(RTC::UniqueId ec_id);
00213 
00214   // The reset action that is invoked resetting
00215   // This is same but different the former rtc_init_entry()
00216   // virtual RTC::ReturnCode_t onReset(RTC::UniqueId ec_id);
00217   
00218   // The state update action that is invoked after onExecute() action
00219   // no corresponding operation exists in OpenRTm-aist-0.2.0
00220   // virtual RTC::ReturnCode_t onStateUpdate(RTC::UniqueId ec_id);
00221 
00222   // The action that is invoked when execution context's rate is changed
00223   // no corresponding operation exists in OpenRTm-aist-0.2.0
00224   // virtual RTC::ReturnCode_t onRateChanged(RTC::UniqueId ec_id);
00225 
00226 
00227  protected:
00228   // Configuration variable declaration
00229   // <rtc-template block="config_declare">
00230   // </rtc-template>
00231 
00232   // DataInPort declaration
00233   // <rtc-template block="inport_declare">
00234   // </rtc-template>
00235 
00236   // DataOutPort declaration
00237   // <rtc-template block="outport_declare">
00238   // </rtc-template>
00239 
00240   // CORBA Port declaration
00241   // <rtc-template block="corbaport_declare">
00242   // </rtc-template>
00243 
00244   // Service declaration
00245   // <rtc-template block="service_declare">
00246   // </rtc-template>
00247 
00248   // Consumer declaration
00249   // <rtc-template block="consumer_declare">
00250   // </rtc-template>
00251 
00252  private:
00253   int dummy;
00254 
00255 };
00256 
00257 
00258 extern "C"
00259 {
00260   void [module.name]Init(RTC::Manager* manager);
00261 };
00262 
00263 #endif // [u_name]_H
00264 """
00265 
00266 
00267 #------------------------------------------------------------
00268 # Component source template code
00269 #------------------------------------------------------------
00270 comp_soruce = """// -*- C++ -*-
00271 /*!
00272  * @file  [fname_cpp]
00273  * @brief [module.desc]
00274  * [rcs_date]
00275  *
00276  * [rcs_id]
00277  */
00278 
00279 #include "[fname_h]"
00280 
00281 // Module specification
00282 // <rtc-template block="module_spec">
00283 // </rtc-template>
00284 
00285 [module.name]::[module.name](RTC::Manager* manager)
00286   : RTC::DataFlowComponentBase(manager),
00287     // <rtc-template block="initializer">
00288     // </rtc-template>
00289         dummy(0)
00290 {
00291   // Registration: InPort/OutPort/Service
00292   // <rtc-template block="registration">
00293   // </rtc-template>
00294 
00295 }
00296 
00297 [module.name]::~[module.name]()
00298 {
00299 }
00300 
00301 
00302 RTC::ReturnCode_t [module.name]::onInitialize()
00303 {
00304   // <rtc-template block="bind_config">
00305   // </rtc-template>
00306   return RTC::RTC_OK;
00307 }
00308 
00309 
00310 [for activity]
00311 /*
00312 RTC::ReturnCode_t [module.name]::[activity.name]([activity.args])
00313 {
00314   return RTC::RTC_OK;
00315 }
00316 */
00317 [end]
00318 
00319 
00320 extern "C"
00321 {
00322  
00323   void [module.name]Init(RTC::Manager* manager)
00324   {
00325     RTC::Properties profile([l_name]_spec);
00326     manager->registerFactory(profile,
00327                              RTC::Create<[module.name]>,
00328                              RTC::Delete<[module.name]>);
00329   }
00330   
00331 };
00332 
00333 
00334 """
00335 
00336 
00337 #------------------------------------------------------------
00338 # Component soruce template
00339 #------------------------------------------------------------
00340 comp_compsrc = """// -*- C++ -*-
00341 /*!
00342  * @file [module.name]Comp.cpp
00343  * @brief Standalone component
00344  * @date [rcs_date]
00345  *
00346  * [rcs_id]
00347  */
00348 
00349 #include <rtm/Manager.h>
00350 #include <iostream>
00351 #include <string>
00352 #include "[fname_h]"
00353 
00354 
00355 void MyModuleInit(RTC::Manager* manager)
00356 {
00357   [module.name]Init(manager);
00358   RTC::RtcBase* comp;
00359 
00360   // Create a component
00361   comp = manager->createComponent("[module.name]");
00362 
00363 
00364   // Example
00365   // The following procedure is examples how handle RT-Components.
00366   // These should not be in this function.
00367 
00368   // Get the component's object reference
00369 //  RTC::RTObject_var rtobj;
00370 //  rtobj = RTC::RTObject::_narrow(manager->getPOA()->servant_to_reference(comp));
00371 
00372   // Get the port list of the component
00373 //  PortList* portlist;
00374 //  portlist = rtobj->get_ports();
00375 
00376   // getting port profiles
00377 //  std::cout << "Number of Ports: ";
00378 //  std::cout << portlist->length() << std::endl << std::endl; 
00379 //  for (CORBA::ULong i(0), n(portlist->length()); i < n; ++i)
00380 //  {
00381 //    Port_ptr port;
00382 //    port = (*portlist)[begin_brace]i[end_brace];
00383 //    std::cout << "Port" << i << " (name): ";
00384 //    std::cout << port->get_port_profile()->name << std::endl;
00385 //    
00386 //    RTC::PortInterfaceProfileList iflist;
00387 //    iflist = port->get_port_profile()->interfaces;
00388 //    std::cout << "---interfaces---" << std::endl;
00389 //    for (CORBA::ULong i(0), n(iflist.length()); i < n; ++i)
00390 //    {
00391 //      std::cout << "I/F name: ";
00392 //      std::cout << iflist[begin_brace]i[end_brace].instance_name << std::endl;
00393 //      std::cout << "I/F type: ";
00394 //      std::cout << iflist[begin_brace]i[end_brace].type_name << std::endl;
00395 //      const char* pol;
00396 //      pol = iflist[begin_brace]i[end_brace].polarity == 0 ? "PROVIDED" : "REQUIRED";
00397 //      std::cout << "Polarity: " << pol << std::endl;
00398 //    }
00399 //    std::cout << "---properties---" << std::endl;
00400 //    NVUtil::dump(port->get_port_profile()->properties);
00401 //    std::cout << "----------------" << std::endl << std::endl;
00402 //  }
00403 
00404   return;
00405 }
00406 
00407 int main (int argc, char** argv)
00408 {
00409   RTC::Manager* manager;
00410   manager = RTC::Manager::init(argc, argv);
00411 
00412   // Initialize manager
00413   manager->init(argc, argv);
00414 
00415   // Set module initialization proceduer
00416   // This procedure will be invoked in activateManager() function.
00417   manager->setModuleInitProc(MyModuleInit);
00418 
00419   // Activate manager and register to naming service
00420   manager->activateManager();
00421 
00422   // run the manager in blocking mode
00423   // runManager(false) is the default.
00424   manager->runManager();
00425 
00426   // If you want to run the manager in non-blocking mode, do like this
00427   // manager->runManager(true);
00428 
00429   return 0;
00430 }
00431 """
00432 
00433 
00434 #------------------------------------------------------------
00435 # Makefile template
00436 #------------------------------------------------------------
00437 makefile = """# -*- Makefile -*-
00438 #
00439 # @file  Makefile.[module.name]
00440 # @brief RTComponent makefile for "[module.name] component"
00441 # @date  [rcs_date]
00442 #
00443 # This file is generated by rtc-template with the following argments.
00444 #
00445 [for fmtd_args]#  [fmtd_args]
00446 [end]#
00447 #
00448 # [rcs_id]
00449 #
00450 CXXFLAGS = `rtm-config --cflags`
00451 LDFLAGS  = `rtm-config --libs`
00452 SHFLAGS  = -shared
00453 
00454 IDLC     = `rtm-config --idlc`
00455 IDLFLAGS = `rtm-config --idlflags` -I`rtm-config --prefix`/include/rtm/idl
00456 WRAPPER  = rtm-skelwrapper
00457 WRAPPER_FLAGS = --include-dir="" --skel-suffix=Skel --stub-suffix=Stub
00458 
00459 SKEL_OBJ = [for service_idl][service_idl.skel_basename].o [end] \
00460         [for consumer_idl][consumer_idl.stub_basename].o [end]
00461 STUB_OBJ = [for service_idl][service_idl.stub_basename].o [end]
00462 IMPL_OBJ = [for service_idl][service_idl.impl_basename].o [end]
00463 OBJS     = [module.name].o $(SKEL_OBJ) $(IMPL_OBJ)
00464 
00465 .SUFFIXES : .so
00466 
00467 all: [module.name].so [module.name]Comp
00468 
00469 
00470 .cpp.o:
00471         rm -f $@
00472         $(CXX) $(CXXFLAGS) -c -o $@ $<
00473 
00474 .o.so:
00475         rm -f $@
00476         $(CXX) $(SHFLAGS) -o $@ $(OBJS) $(LDFLAGS)
00477 
00478 [module.name]Comp: [module.name]Comp.o $(OBJS)
00479         $(CXX) -o $@ $(OBJS) [module.name]Comp.o $(LDFLAGS) 
00480 
00481 
00482 clean: clean_objs clean_skelstub
00483         rm -f *~
00484 
00485 clean_objs:
00486         rm -f $(OBJS) [module.name]Comp.o [module.name].so [module.name]Comp
00487 
00488 clean_skelstub:
00489         rm -f *[skel_suffix].h *[skel_suffix].cpp
00490         rm -f *[stub_suffix].h *[stub_suffix].cpp
00491 
00492 
00493 
00494 [for service_idl]
00495 [service_idl.skel_basename].cpp : [service_idl.idl_fname]
00496         $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
00497         $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
00498 [service_idl.skel_basename].h : [service_idl.idl_fname]
00499         $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
00500         $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
00501 [service_idl.stub_basename].cpp : [service_idl.idl_fname]
00502         $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
00503         $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
00504 [service_idl.stub_basename].h : [service_idl.idl_fname]
00505         $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
00506         $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
00507 [end]
00508 
00509 [for consumer_idl]
00510 [consumer_idl.stub_basename].cpp : [consumer_idl.idl_fname]
00511         $(IDLC) $(IDLFLAGS) [consumer_idl.idl_fname]
00512         $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[consumer_idl.idl_fname]
00513 [consumer_idl.stub_basename].h : [consumer_idl.idl_fname]
00514         $(IDLC) $(IDLFLAGS) [consumer_idl.idl_fname]
00515         $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[consumer_idl.idl_fname]
00516 [end]
00517 
00518 
00519 [module.name].so: $(OBJS)
00520 [module.name].o: [module.name].h \
00521         [for service_idl][service_idl.skel_basename].h [service_idl.impl_basename].h [end] \
00522         [for consumer_idl][consumer_idl.stub_basename].h [end]
00523 [module.name]Comp.o: [module.name]Comp.cpp [module.name].cpp [module.name].h [for service_idl][service_idl.skel_basename].h [service_idl.impl_basename].h [end]
00524 
00525 [for service_idl]
00526 [service_idl.impl_basename].o: [service_idl.impl_basename].cpp [service_idl.impl_basename].h [service_idl.skel_basename].h [service_idl.stub_basename].h
00527 [service_idl.skel_basename].o: [service_idl.skel_basename].cpp [service_idl.skel_basename].h [service_idl.stub_basename].h
00528 [service_idl.stub_basename].o: [service_idl.stub_basename].cpp [service_idl.stub_basename].h
00529 [end]
00530 
00531 [for consumer_idl]
00532 [consumer_idl.stub_basename].o: [consumer_idl.stub_basename].cpp [consumer_idl.stub_basename].h
00533 [end]
00534 
00535 # end of Makefile
00536 """
00537 
00538 
00539 #============================================================
00540 # Replaced strings definition for <rtc-template> tags
00541 #============================================================
00542 service_impl_h = """[for service_idl]#include "[service_idl.impl_h]"
00543 [end]"""
00544 consumer_stub_h = """[for consumer_idl]#include "[consumer_idl.stub_h]"
00545 [end]"""
00546 
00547 module_spec = """static const char* [l_name]_spec[] =
00548   {
00549     "implementation_id", "[module.name]",
00550     "type_name",         "[module.name]",
00551     "description",       "[module.desc]",
00552     "version",           "[module.version]",
00553     "vendor",            "[module.vendor]",
00554     "category",          "[module.category]",
00555     "activity_type",     "[module.comp_type]",
00556     "max_instance",      "[module.max_inst]",
00557     "language",          "C++",
00558     "lang_type",         "compile",
00559     // Configuration variables
00560 [for config]    "conf.default.[config.name]", "[config.default]",
00561 [end]
00562     ""
00563   };"""
00564 
00565 config_declare = \
00566 """  [for config][config.type] m_[config.name];
00567   [end]"""
00568   
00569 
00570 inport_declare = \
00571 """  [for inport][inport.type] m_[inport.name];
00572   InPort<[inport.type]> m_[inport.name]In;
00573   [end]"""
00574 
00575 outport_declare = \
00576 """  [for outport][outport.type] m_[outport.name];
00577   OutPort<[outport.type]> m_[outport.name]Out;
00578   [end]"""
00579 
00580 corbaport_declare = \
00581 """  [for corbaport]RTC::CorbaPort m_[corbaport.name]Port;
00582   [end]"""
00583  
00584 service_declare = \
00585 """  [for service][service.type]SVC_impl m_[service.name];
00586   [end]"""
00587 
00588 consumer_declare = \
00589 """  [for consumer]RTC::CorbaConsumer<[consumer.type]> m_[consumer.name];
00590   [end]"""
00591 
00592 initializer = \
00593 """    [for inport]m_[inport.name]In("[inport.name]", m_[inport.name]),
00594     [end][for outport]m_[outport.name]Out("[outport.name]", m_[outport.name]),
00595     [end][for corbaport]m_[corbaport.name]Port("[corbaport.name]"),[end]"""
00596 
00597 
00598 registration = \
00599 """  // Set InPort buffers
00600   [for inport]registerInPort("[inport.name]", m_[inport.name]In);
00601   [end]
00602   // Set OutPort buffer
00603   [for outport]registerOutPort("[outport.name]", m_[outport.name]Out);
00604   [end]
00605   // Set service provider to Ports
00606   [for service]m_[service.port]Port.registerProvider("[service.name]", "[service.type]", m_[service.name]);
00607   [end]
00608   // Set service consumers to Ports
00609   [for consumer]m_[consumer.port]Port.registerConsumer("[consumer.name]", "[consumer.type]", m_[consumer.name]);
00610   [end]
00611   // Set CORBA Service Ports
00612   [for corbaport]registerPort(m_[corbaport.name]Port);
00613   [end]"""
00614 
00615 bind_config = \
00616 """  // Bind variables and configuration variable
00617   [for config]bindParameter("[config.name]", m_[config.name], "[config.default]");
00618   [end]"""
00619 
00620 #------------------------------------------------------------
00621 
00622 
00623 #============================================================
00624 # Classes and Functions
00625 #============================================================
00626 
00627 class Struct:
00628         def __init__(self):
00629                 return
00630 
00631 
00632 def MakeSuffix(opts, dict):
00633         impl_suffix = "SVC_impl"
00634         skel_suffix = "Skel"
00635         stub_suffix = "Stub"
00636         for opt, arg in opts:
00637                 if opt.find("--svc-impl-suffix") == 0:
00638                         impl_suffix = arg
00639                 if opt.find("--svc-skel-suffix") == 0:
00640                         skel_suffix = arg
00641                 if opt.find("--svc-stub-suffix") == 0:
00642                         stub_suffix = arg
00643         dict["impl_suffix"] = impl_suffix
00644         dict["skel_suffix"] = skel_suffix
00645         dict["stub_suffix"] = stub_suffix
00646 
00647 
00648 def MakeServiceIDL(dict):
00649         for d in dict["service_idl"]:
00650                 d.impl_basename = d.idl_basename + dict["impl_suffix"]
00651                 d.impl_h        = d.impl_basename + ".h"
00652                 d.impl_cpp      = d.impl_basename + ".cpp"
00653                 d.skel_basename = d.idl_basename + dict["skel_suffix"]
00654                 d.skel_h        = d.skel_basename + ".h"
00655                 d.skel_cpp      = d.skel_basename + ".cpp"
00656                 d.stub_suffix   = dict["stub_suffix"]
00657                 d.stub_basename = d.idl_basename + dict["stub_suffix"]
00658                 d.stub_h        = d.stub_basename + ".h"
00659                 d.stub_cpp      = d.stub_basename + ".cpp"
00660 
00661 
00662 def MakeConsumerIDL(dict):
00663         conslist = []
00664         for cons in dict["consumer_idl"]:
00665                 dup = False
00666                 for svc in dict["service_idl"]:
00667                         if cons.idl_fname == svc.idl_fname:
00668                                 dup = True
00669                 if not dup:
00670                         tmp = cons
00671                         tmp.stub_suffix   = dict["stub_suffix"]
00672                         tmp.stub_basename = tmp.idl_basename \
00673                             + dict["stub_suffix"]
00674                         tmp.stub_h        = tmp.stub_basename + ".h"
00675                         tmp.stub_cpp      = tmp.stub_basename + ".cpp"
00676                         conslist.append(tmp)
00677         dict["consumer_idl"] = conslist
00678 
00679 
00680 def MakeActivityFuncs(dict):
00681         acts = (("onFinalize",    ""), \
00682                 ("onStartup",     "RTC::UniqueId ec_id"), \
00683                 ("onShutdown",    "RTC::UniqueId ec_id"), \
00684                 ("onActivated",   "RTC::UniqueId ec_id"), \
00685                 ("onDeactivated", "RTC::UniqueId ec_id"), \
00686                 ("onExecute",     "RTC::UniqueId ec_id"), \
00687                 ("onAborting",    "RTC::UniqueId ec_id"), \
00688                 ("onError",       "RTC::UniqueId ec_id"), \
00689                 ("onReset",       "RTC::UniqueId ec_id"), \
00690                 ("onStateUpdate", "RTC::UniqueId ec_id"), \
00691                 ("onRateChanged", "RTC::UniqueId ec_id"))
00692         actlist = []
00693         for name, args in acts:
00694                 a = Struct()
00695                 a.name = name
00696                 a.args = args
00697                 actlist.append(a)
00698 
00699         dict["activity"] = actlist
00700 
00701 
00702 class cxx_gen(gen_base.gen_base):
00703         """
00704         C++ component source code generator
00705         """
00706         _fname_space = 16
00707         def __init__(self, data, opts):
00708                 self.data = data.copy()
00709                 
00710                 MakeSuffix(opts, self.data)
00711                 MakeServiceIDL(self.data)
00712                 MakeConsumerIDL(self.data)
00713                 MakeActivityFuncs(self.data)
00714                 self.data["begin_brace"] = "["
00715                 self.data["end_brace"] = "]"
00716                 self.data["rcs_date"] = "$" + "Date" + "$"
00717                 self.data["rcs_id"] = "$" + "Id" + "$"
00718                 self.data["fname_h"] = self.data["fname"] + ".h"
00719                 self.data["fname_cpp"] = self.data["fname"] + ".cpp"
00720                 self.data["fname_comp"] = self.data["fname"] + "Comp.cpp"
00721                 self.data["makefile"] = "Makefile." + self.data["fname"]
00722                 self.data["u_name"] = self.data["module"].name.upper()
00723                 self.data["l_name"] = self.data["module"].name.lower()
00724 
00725 
00726                 self.tags = {}
00727                 self.tags["service_impl_h"]    = service_impl_h
00728                 self.tags["consumer_stub_h"]   = consumer_stub_h
00729                 self.tags["module_spec"]       = module_spec
00730                 self.tags["config_declare"]    = config_declare
00731                 self.tags["inport_declare"]    = inport_declare
00732                 self.tags["outport_declare"]   = outport_declare
00733                 self.tags["corbaport_declare"] = corbaport_declare
00734                 self.tags["service_declare"]   = service_declare
00735                 self.tags["consumer_declare"]  = consumer_declare
00736                 self.tags["initializer"]       = initializer
00737                 self.tags["registration"]      = registration
00738                 self.tags["bind_config"]       = bind_config
00739 
00740                 self.gen_tags(self.tags)
00741                 return
00742 
00743 
00744         def print_header(self):
00745                 """
00746                 Generate component class header
00747                 """
00748                 self.gen(self.data["fname_h"],
00749                          comp_header, self.data, self.tags)
00750                 return
00751 
00752 
00753         def print_source(self):
00754                 """
00755                 Generate component class source code
00756                 """
00757                 self.gen(self.data["fname_cpp"],
00758                          comp_soruce, self.data, self.tags)
00759                 return
00760 
00761 
00762         def print_compsrc(self):
00763                 """
00764                 Generate component source code
00765                 """
00766                 self.gen(self.data["fname_comp"],
00767                          comp_compsrc, self.data, self.tags)
00768                 return
00769 
00770 
00771         def print_makefile(self):
00772                 """
00773                 Generate Makefile
00774                 """
00775                 self.gen(self.data["makefile"],
00776                          makefile, self.data, self.tags)
00777 
00778 
00779         def print_impl(self):
00780                 for svc_idl in self.data["service_idl"]:
00781                         fd_h = None
00782                         fd_cpp = None
00783                         lines_h = None
00784                         lines_cpp = None
00785                         if not os.access(svc_idl.idl_fname, os.F_OK):
00786                                 sys.stderr.write("Error: IDL file \"" \
00787                                                  + svc_idl.idl_fname \
00788                                                  + "\" not found.\n")
00789                                 sys.exit(1)
00790                         fd_h, lines_h = \
00791                             self.check_overwrite(svc_idl.impl_h)
00792                         fd_cpp, lines_cpp = \
00793                             self.check_overwrite(svc_idl.impl_cpp)
00794                         if not fd_h:
00795                                 sys.stderr.write("Cannot open file:" + 
00796                                                  svc_idl.impl_h + "\n")
00797                                 sys.exit(1)
00798                         if not fd_cpp:
00799                                 sys.stderr.write("Cannot open file:" + 
00800                                                  svc_idl.impl_cpp + "\n")
00801                                 sys.exit(1)
00802                         if lines_h or lines_cpp:
00803                                 sys.stderr.write("Merge of service impl." +
00804                                                  "code is not supported.\n")
00805                                 return
00806 
00807                         try:
00808                                 idl_include = self.data["idl_include"]
00809                                 impl_suffix = self.data["impl_suffix"]
00810                                 skel_suffix = self.data["skel_suffix"]
00811                                 import cxx_svc_impl
00812                                 ifs = cxx_svc_impl.generate(svc_idl.idl_fname,
00813                                                             idl_include,
00814                                                             impl_suffix,
00815                                                             skel_suffix,
00816                                                             fd_h, fd_cpp)
00817                                 print "  File \"" \
00818                                     + svc_idl.impl_h \
00819                                     + "\" was generated."
00820                                 print "  File \"" \
00821                                     + svc_idl.impl_cpp \
00822                                     + "\" was generated."
00823                         except:
00824                                 sys.stderr.write("Generate error: " \
00825                                                  + svc_idl.impl_h
00826                                                  + ", "
00827                                                  + svc_idl.impl_cpp + "\n")
00828                                 
00829         def print_all(self):
00830                 self.print_impl()
00831                 self.print_header()
00832                 self.print_source()
00833                 self.print_compsrc()
00834                 self.print_makefile()
00835                 return
00836 
00837 
00838 class idl2char:
00839         def __init__(self, filename = None):
00840                 self.lines = ""
00841                 self.inComment = False
00842                 if filename:
00843                         f = open(filename)
00844                         l = f.readlines()
00845                         self.erase_comments(l)
00846                         f.close()
00847 
00848         def open_file(self, filename):
00849                 f = open(filename)
00850                 l = f.readlines()
00851                 self.erase_comments(l)
00852                 f.close()
00853 
00854         def cleanup():
00855                 self.lines = ""
00856 
00857         def erase_comments(self, lines):
00858                 for l in lines:
00859                         m = re.search("/\*.*\*/", l)
00860                         if m:
00861                                 l = l.replace(m.group(), "")
00862                                 
00863                         m = re.search(".*\*/", l)
00864                         if m:
00865                                 l = l.replace(m.group(), "")
00866                                 if self.inComment == True:
00867                                         self.inComment = False
00868 
00869                         m = re.search("/\*.*$", l)
00870                         if m:
00871                                 l = l.replace(m.group(), "")
00872                                 if self.inComment == False:
00873                                         self.inComment = True
00874 
00875                         m = re.search("//.*$", l)
00876                         if m:
00877                                 l = l.replace(m.group(), "")
00878 
00879                         m = re.search("\s+$", l)
00880                         if m:
00881                                 l = l.replace(m.group(), "")
00882                                 l = l + "\n"
00883 
00884                         m = re.search("\S", l)
00885                         if m:
00886                                 if not self.inComment:
00887                                         self.lines += l
00888 
00889                                         
00890         def get_lines(self):
00891                 return self.lines
00892 
00893         def make_chardata(self, var_name):
00894                 self.lines = self.lines.replace("\n","\\n\\\n")
00895                 self.lines = self.lines.replace("\"","\\\"")
00896                 self.lines = "static char* " + var_name  \
00897                     + " = \"\\\n" + self.lines
00898                 self.lines += "\";\n"
00899                 return self.lines
00900 
00901         def add_slash(self):
00902                 self.lines = self.lines.replace("\n","\\\n")
00903                 return self.lines
00904 
00905         def write_file(self, filename):
00906                 f = open(filename, "w")
00907                 f.write(self.lines)
00908                 f.close()
00909 
00910 


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28