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


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:03