cxx_gen.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- python -*-
3 #
4 # @file cxx_gen.py
5 # @brief rtc-template C++ source code generator class
6 # @date $Date: 2007/07/20 17:28:56 $
7 # @author Noriaki Ando <n-ando@aist.go.jp>
8 #
9 # Copyright (C) 2004-2007
10 # Task-intelligence Research Group,
11 # Intelligent Systems Research Institute,
12 # National Institute of
13 # Advanced Industrial Science and Technology (AIST), Japan
14 # All rights reserved.
15 #
16 # $Id: cxx_gen.py,v 1.8.2.1 2007/07/20 17:28:56 n-ando Exp $
17 #
18 
19 #
20 # $Log: cxx_gen.py,v $
21 # Revision 1.8.2.1 2007/07/20 17:28:56 n-ando
22 # A fix for win32 porting.
23 #
24 # Revision 1.8 2007/05/29 01:44:29 n-ando
25 # make clean target was modified to delete *Compl.o
26 #
27 # Revision 1.7 2007/04/23 01:41:16 n-ando
28 # New option "--config" and configuration template code were added.
29 #
30 # Revision 1.6 2007/04/13 15:05:50 n-ando
31 # Now RTC::OK becomes RTC::RTC_OK in RTC.idl.
32 #
33 # Revision 1.5 2007/01/14 23:38:30 n-ando
34 # Updated to use setModuleInitProc() to initialize component locally.
35 #
36 # Revision 1.4 2007/01/11 07:46:38 n-ando
37 # Modified for OMG RTC specificatin and OpenRTM-aist-0.4.0.
38 # Almost all template codes were modified according to the new framework.
39 #
40 # Revision 1.3 2005/09/08 09:23:55 n-ando
41 # - A bug fix for merge function.
42 #
43 # Revision 1.2 2005/09/06 14:37:03 n-ando
44 # rtc-template's command options and data structure for ezt (Easy Template)
45 # are changed for RTComponent's service features.
46 # Now rtc-template can generate services' skeletons, stubs and
47 # implementation files.
48 # The implementation code generation uses omniidl's IDL parser.
49 #
50 # Revision 1.1 2005/08/26 12:02:14 n-ando
51 # This code generator module uses ezt (Easy Template).
52 #
53 # Revision 1.1.1.1 2005/05/12 09:06:18 n-ando
54 # Public release.
55 #
56 #
57 
58 import re
59 import os
60 import sys
61 import StringIO
62 import ezt
63 import gen_base
64 
65 
67  return "C++ component code generator"
68 
70  """
71  C++ generator specific usage (short version)
72  """
73  return """
74 Options for C++ backend:
75 
76  [--svc-impl-suffix[=suffix]] Suffix of implementation class
77  [--svc-skel-suffix[=suffix]] Suffix of server skeleton files
78  [--svc-stub-suffix[=suffix]] Suffix of client stub files
79 """
80 
81 def usage():
82  """
83  C++ generator specific usage
84  """
85  return """
86 -------------------------------
87  Help for C++ code geenrator
88 -------------------------------
89  --svc-impl-suffix=[Suffix]:
90  Specify the suffix for implementation class name. This suffix is also
91  used for implementation class header file and code file.
92 
93  --svc-skel-suffix=[Suffix]:
94  Specify the suffix for server skeleton files.
95 
96  --svc-stub-suffix=[Suffix]:
97  Specify the suffix for client stub files.
98 
99 C++ code generator generates the following files.
100  [Component name].h.............Component class header
101  [Component name].cpp...........Component class soruce code
102  [Component name]Comp.cpp.......Component startup code
103  [IDL basename]Skel.h...........Server skeleton header
104  [IDL basename]Skel.cpp.........Server skeleton source code
105  [IDL basename]Stub.h...........Client stub header
106  [IDL basename]Stub.cpp.........Client stub source code
107  [IDL basename]SVC_impl.h.......Server implementation header
108  [IDL basename]SVC_impl.cpp.....Server implementation source code
109  Makefile.[Component name]......Makefile to compile this codes
110  README.[Component name]........Specification template of the component
111 
112  Suffixes (Skel, Stub, SVC_impl) can be specified --svc-*-suffix option
113 
114  Other CORBA implementation specific skeleton/stub code would be generated.
115  --omniORB--
116  [IDL basename].hh..............Client stub header
117  [IDL basename]SK.cc............Server skeleton source code
118  [IDL basename]DynSK.cc.........Dynamic server skeleton source code
119  --TAO--
120  [IDL basename]S.h..............Server skeleton header
121  [IDL basename]S.h..............Server skeleton source code
122  [IDL basename]C.h..............Client stub header
123  [IDL basename]C.h..............Client stub source code
124  --MICO--
125  [IDL basename].h...............Server skeleton header
126  [IDL basename]_skel.cc.........Server skeleton source code
127  [IDL basename].h...............Client stub header
128  [IDL basename].cc..............Client stub source code
129 
130 """
131 
133  opt_args_fmt = ["svc-impl-suffix=",
134  "svc-skel-suffix=",
135  "svc-stub-suffix="]
136  return opt_args_fmt
137 
138 
139 #------------------------------------------------------------
140 # Component header template code
141 #------------------------------------------------------------
142 comp_header = """// -*- C++ -*-
143 /*!
144  * @file [fname_h]
145  * @brief [module.desc]
146  * @date [rcs_date]
147  *
148  * [rcs_id]
149  */
150 
151 #ifndef [u_name]_H
152 #define [u_name]_H
153 
154 #include <rtm/Manager.h>
155 #include <rtm/DataFlowComponentBase.h>
156 #include <rtm/CorbaPort.h>
157 #include <rtm/DataInPort.h>
158 #include <rtm/DataOutPort.h>
159 #include <rtm/idl/BasicDataTypeSkel.h>
160 
161 // Service implementation headers
162 // <rtc-template block="service_impl_h">
163 // </rtc-template>
164 
165 // Service Consumer stub headers
166 // <rtc-template block="consumer_stub_h">
167 // </rtc-template>
168 
169 using namespace RTC;
170 
171 class [module.name]
172  : public RTC::DataFlowComponentBase
173 {
174  public:
175  [module.name](RTC::Manager* manager);
176  ~[module.name]();
177 
178  // The initialize action (on CREATED->ALIVE transition)
179  // formaer rtc_init_entry()
180  virtual RTC::ReturnCode_t onInitialize();
181 
182  // The finalize action (on ALIVE->END transition)
183  // formaer rtc_exiting_entry()
184  // virtual RTC::ReturnCode_t onFinalize();
185 
186  // The startup action when ExecutionContext startup
187  // former rtc_starting_entry()
188  // virtual RTC::ReturnCode_t onStartup(RTC::UniqueId ec_id);
189 
190  // The shutdown action when ExecutionContext stop
191  // former rtc_stopping_entry()
192  // virtual RTC::ReturnCode_t onShutdown(RTC::UniqueId ec_id);
193 
194  // The activated action (Active state entry action)
195  // former rtc_active_entry()
196  // virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id);
197 
198  // The deactivated action (Active state exit action)
199  // former rtc_active_exit()
200  // virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id);
201 
202  // The execution action that is invoked periodically
203  // former rtc_active_do()
204  // virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id);
205 
206  // The aborting action when main logic error occurred.
207  // former rtc_aborting_entry()
208  // virtual RTC::ReturnCode_t onAborting(RTC::UniqueId ec_id);
209 
210  // The error action in ERROR state
211  // former rtc_error_do()
212  // virtual RTC::ReturnCode_t onError(RTC::UniqueId ec_id);
213 
214  // The reset action that is invoked resetting
215  // This is same but different the former rtc_init_entry()
216  // virtual RTC::ReturnCode_t onReset(RTC::UniqueId ec_id);
217 
218  // The state update action that is invoked after onExecute() action
219  // no corresponding operation exists in OpenRTm-aist-0.2.0
220  // virtual RTC::ReturnCode_t onStateUpdate(RTC::UniqueId ec_id);
221 
222  // The action that is invoked when execution context's rate is changed
223  // no corresponding operation exists in OpenRTm-aist-0.2.0
224  // virtual RTC::ReturnCode_t onRateChanged(RTC::UniqueId ec_id);
225 
226 
227  protected:
228  // Configuration variable declaration
229  // <rtc-template block="config_declare">
230  // </rtc-template>
231 
232  // DataInPort declaration
233  // <rtc-template block="inport_declare">
234  // </rtc-template>
235 
236  // DataOutPort declaration
237  // <rtc-template block="outport_declare">
238  // </rtc-template>
239 
240  // CORBA Port declaration
241  // <rtc-template block="corbaport_declare">
242  // </rtc-template>
243 
244  // Service declaration
245  // <rtc-template block="service_declare">
246  // </rtc-template>
247 
248  // Consumer declaration
249  // <rtc-template block="consumer_declare">
250  // </rtc-template>
251 
252  private:
253  int dummy;
254 
255 };
256 
257 
258 extern "C"
259 {
260  void [module.name]Init(RTC::Manager* manager);
261 };
262 
263 #endif // [u_name]_H
264 """
265 
266 
267 #------------------------------------------------------------
268 # Component source template code
269 #------------------------------------------------------------
270 comp_soruce = """// -*- C++ -*-
271 /*!
272  * @file [fname_cpp]
273  * @brief [module.desc]
274  * [rcs_date]
275  *
276  * [rcs_id]
277  */
278 
279 #include "[fname_h]"
280 
281 // Module specification
282 // <rtc-template block="module_spec">
283 // </rtc-template>
284 
285 [module.name]::[module.name](RTC::Manager* manager)
286  : RTC::DataFlowComponentBase(manager),
287  // <rtc-template block="initializer">
288  // </rtc-template>
289  dummy(0)
290 {
291  // Registration: InPort/OutPort/Service
292  // <rtc-template block="registration">
293  // </rtc-template>
294 
295 }
296 
297 [module.name]::~[module.name]()
298 {
299 }
300 
301 
302 RTC::ReturnCode_t [module.name]::onInitialize()
303 {
304  // <rtc-template block="bind_config">
305  // </rtc-template>
306  return RTC::RTC_OK;
307 }
308 
309 
310 [for activity]
311 /*
312 RTC::ReturnCode_t [module.name]::[activity.name]([activity.args])
313 {
314  return RTC::RTC_OK;
315 }
316 */
317 [end]
318 
319 
320 extern "C"
321 {
322 
323  void [module.name]Init(RTC::Manager* manager)
324  {
325  RTC::Properties profile([l_name]_spec);
326  manager->registerFactory(profile,
327  RTC::Create<[module.name]>,
328  RTC::Delete<[module.name]>);
329  }
330 
331 };
332 
333 
334 """
335 
336 
337 #------------------------------------------------------------
338 # Component soruce template
339 #------------------------------------------------------------
340 comp_compsrc = """// -*- C++ -*-
341 /*!
342  * @file [module.name]Comp.cpp
343  * @brief Standalone component
344  * @date [rcs_date]
345  *
346  * [rcs_id]
347  */
348 
349 #include <rtm/Manager.h>
350 #include <iostream>
351 #include <string>
352 #include "[fname_h]"
353 
354 
355 void MyModuleInit(RTC::Manager* manager)
356 {
357  [module.name]Init(manager);
358  RTC::RtcBase* comp;
359 
360  // Create a component
361  comp = manager->createComponent("[module.name]");
362 
363 
364  // Example
365  // The following procedure is examples how handle RT-Components.
366  // These should not be in this function.
367 
368  // Get the component's object reference
369 // RTC::RTObject_var rtobj;
370 // rtobj = RTC::RTObject::_narrow(manager->getPOA()->servant_to_reference(comp));
371 
372  // Get the port list of the component
373 // PortList* portlist;
374 // portlist = rtobj->get_ports();
375 
376  // getting port profiles
377 // std::cout << "Number of Ports: ";
378 // std::cout << portlist->length() << std::endl << std::endl;
379 // for (CORBA::ULong i(0), n(portlist->length()); i < n; ++i)
380 // {
381 // Port_ptr port;
382 // port = (*portlist)[begin_brace]i[end_brace];
383 // std::cout << "Port" << i << " (name): ";
384 // std::cout << port->get_port_profile()->name << std::endl;
385 //
386 // RTC::PortInterfaceProfileList iflist;
387 // iflist = port->get_port_profile()->interfaces;
388 // std::cout << "---interfaces---" << std::endl;
389 // for (CORBA::ULong i(0), n(iflist.length()); i < n; ++i)
390 // {
391 // std::cout << "I/F name: ";
392 // std::cout << iflist[begin_brace]i[end_brace].instance_name << std::endl;
393 // std::cout << "I/F type: ";
394 // std::cout << iflist[begin_brace]i[end_brace].type_name << std::endl;
395 // const char* pol;
396 // pol = iflist[begin_brace]i[end_brace].polarity == 0 ? "PROVIDED" : "REQUIRED";
397 // std::cout << "Polarity: " << pol << std::endl;
398 // }
399 // std::cout << "---properties---" << std::endl;
400 // NVUtil::dump(port->get_port_profile()->properties);
401 // std::cout << "----------------" << std::endl << std::endl;
402 // }
403 
404  return;
405 }
406 
407 int main (int argc, char** argv)
408 {
409  RTC::Manager* manager;
410  manager = RTC::Manager::init(argc, argv);
411 
412  // Initialize manager
413  manager->init(argc, argv);
414 
415  // Set module initialization proceduer
416  // This procedure will be invoked in activateManager() function.
417  manager->setModuleInitProc(MyModuleInit);
418 
419  // Activate manager and register to naming service
420  manager->activateManager();
421 
422  // run the manager in blocking mode
423  // runManager(false) is the default.
424  manager->runManager();
425 
426  // If you want to run the manager in non-blocking mode, do like this
427  // manager->runManager(true);
428 
429  return 0;
430 }
431 """
432 
433 
434 #------------------------------------------------------------
435 # Makefile template
436 #------------------------------------------------------------
437 makefile = """# -*- Makefile -*-
438 #
439 # @file Makefile.[module.name]
440 # @brief RTComponent makefile for "[module.name] component"
441 # @date [rcs_date]
442 #
443 # This file is generated by rtc-template with the following argments.
444 #
445 [for fmtd_args]# [fmtd_args]
446 [end]#
447 #
448 # [rcs_id]
449 #
450 CXXFLAGS = `rtm-config --cflags`
451 LDFLAGS = `rtm-config --libs`
452 SHFLAGS = -shared
453 
454 IDLC = `rtm-config --idlc`
455 IDLFLAGS = `rtm-config --idlflags` -I`rtm-config --prefix`/include/rtm/idl
456 WRAPPER = rtm-skelwrapper
457 WRAPPER_FLAGS = --include-dir="" --skel-suffix=Skel --stub-suffix=Stub
458 
459 SKEL_OBJ = [for service_idl][service_idl.skel_basename].o [end] \
460  [for consumer_idl][consumer_idl.stub_basename].o [end]
461 STUB_OBJ = [for service_idl][service_idl.stub_basename].o [end]
462 IMPL_OBJ = [for service_idl][service_idl.impl_basename].o [end]
463 OBJS = [module.name].o $(SKEL_OBJ) $(IMPL_OBJ)
464 
465 .SUFFIXES : .so
466 
467 all: [module.name].so [module.name]Comp
468 
469 
470 .cpp.o:
471  rm -f $@
472  $(CXX) $(CXXFLAGS) -c -o $@ $<
473 
474 .o.so:
475  rm -f $@
476  $(CXX) $(SHFLAGS) -o $@ $(OBJS) $(LDFLAGS)
477 
478 [module.name]Comp: [module.name]Comp.o $(OBJS)
479  $(CXX) -o $@ $(OBJS) [module.name]Comp.o $(LDFLAGS)
480 
481 
482 clean: clean_objs clean_skelstub
483  rm -f *~
484 
485 clean_objs:
486  rm -f $(OBJS) [module.name]Comp.o [module.name].so [module.name]Comp
487 
488 clean_skelstub:
489  rm -f *[skel_suffix].h *[skel_suffix].cpp
490  rm -f *[stub_suffix].h *[stub_suffix].cpp
491 
492 
493 
494 [for service_idl]
495 [service_idl.skel_basename].cpp : [service_idl.idl_fname]
496  $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
497  $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
498 [service_idl.skel_basename].h : [service_idl.idl_fname]
499  $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
500  $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
501 [service_idl.stub_basename].cpp : [service_idl.idl_fname]
502  $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
503  $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
504 [service_idl.stub_basename].h : [service_idl.idl_fname]
505  $(IDLC) $(IDLFLAGS) [service_idl.idl_fname]
506  $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[service_idl.idl_fname]
507 [end]
508 
509 [for consumer_idl]
510 [consumer_idl.stub_basename].cpp : [consumer_idl.idl_fname]
511  $(IDLC) $(IDLFLAGS) [consumer_idl.idl_fname]
512  $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[consumer_idl.idl_fname]
513 [consumer_idl.stub_basename].h : [consumer_idl.idl_fname]
514  $(IDLC) $(IDLFLAGS) [consumer_idl.idl_fname]
515  $(WRAPPER) $(WRAPPER_FLAGS) --idl-file=[consumer_idl.idl_fname]
516 [end]
517 
518 
519 [module.name].so: $(OBJS)
520 [module.name].o: [module.name].h \
521  [for service_idl][service_idl.skel_basename].h [service_idl.impl_basename].h [end] \
522  [for consumer_idl][consumer_idl.stub_basename].h [end]
523 [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]
524 
525 [for service_idl]
526 [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
527 [service_idl.skel_basename].o: [service_idl.skel_basename].cpp [service_idl.skel_basename].h [service_idl.stub_basename].h
528 [service_idl.stub_basename].o: [service_idl.stub_basename].cpp [service_idl.stub_basename].h
529 [end]
530 
531 [for consumer_idl]
532 [consumer_idl.stub_basename].o: [consumer_idl.stub_basename].cpp [consumer_idl.stub_basename].h
533 [end]
534 
535 # end of Makefile
536 """
537 
538 
539 #============================================================
540 # Replaced strings definition for <rtc-template> tags
541 #============================================================
542 service_impl_h = """[for service_idl]#include "[service_idl.impl_h]"
543 [end]"""
544 consumer_stub_h = """[for consumer_idl]#include "[consumer_idl.stub_h]"
545 [end]"""
546 
547 module_spec = """static const char* [l_name]_spec[] =
548  {
549  "implementation_id", "[module.name]",
550  "type_name", "[module.name]",
551  "description", "[module.desc]",
552  "version", "[module.version]",
553  "vendor", "[module.vendor]",
554  "category", "[module.category]",
555  "activity_type", "[module.comp_type]",
556  "max_instance", "[module.max_inst]",
557  "language", "C++",
558  "lang_type", "compile",
559  // Configuration variables
560 [for config] "conf.default.[config.name]", "[config.default]",
561 [end]
562  ""
563  };"""
564 
565 config_declare = \
566 """ [for config][config.type] m_[config.name];
567  [end]"""
568 
569 
570 inport_declare = \
571 """ [for inport][inport.type] m_[inport.name];
572  InPort<[inport.type]> m_[inport.name]In;
573  [end]"""
574 
575 outport_declare = \
576 """ [for outport][outport.type] m_[outport.name];
577  OutPort<[outport.type]> m_[outport.name]Out;
578  [end]"""
579 
580 corbaport_declare = \
581 """ [for corbaport]RTC::CorbaPort m_[corbaport.name]Port;
582  [end]"""
583 
584 service_declare = \
585 """ [for service][service.type]SVC_impl m_[service.name];
586  [end]"""
587 
588 consumer_declare = \
589 """ [for consumer]RTC::CorbaConsumer<[consumer.type]> m_[consumer.name];
590  [end]"""
591 
592 initializer = \
593 """ [for inport]m_[inport.name]In("[inport.name]", m_[inport.name]),
594  [end][for outport]m_[outport.name]Out("[outport.name]", m_[outport.name]),
595  [end][for corbaport]m_[corbaport.name]Port("[corbaport.name]"),[end]"""
596 
597 
598 registration = \
599 """ // Set InPort buffers
600  [for inport]registerInPort("[inport.name]", m_[inport.name]In);
601  [end]
602  // Set OutPort buffer
603  [for outport]registerOutPort("[outport.name]", m_[outport.name]Out);
604  [end]
605  // Set service provider to Ports
606  [for service]m_[service.port]Port.registerProvider("[service.name]", "[service.type]", m_[service.name]);
607  [end]
608  // Set service consumers to Ports
609  [for consumer]m_[consumer.port]Port.registerConsumer("[consumer.name]", "[consumer.type]", m_[consumer.name]);
610  [end]
611  // Set CORBA Service Ports
612  [for corbaport]registerPort(m_[corbaport.name]Port);
613  [end]"""
614 
615 bind_config = \
616 """ // Bind variables and configuration variable
617  [for config]bindParameter("[config.name]", m_[config.name], "[config.default]");
618  [end]"""
619 
620 #------------------------------------------------------------
621 
622 
623 #============================================================
624 # Classes and Functions
625 #============================================================
626 
627 class Struct:
628  def __init__(self):
629  return
630 
631 
632 def MakeSuffix(opts, dict):
633  impl_suffix = "SVC_impl"
634  skel_suffix = "Skel"
635  stub_suffix = "Stub"
636  for opt, arg in opts:
637  if opt.find("--svc-impl-suffix") == 0:
638  impl_suffix = arg
639  if opt.find("--svc-skel-suffix") == 0:
640  skel_suffix = arg
641  if opt.find("--svc-stub-suffix") == 0:
642  stub_suffix = arg
643  dict["impl_suffix"] = impl_suffix
644  dict["skel_suffix"] = skel_suffix
645  dict["stub_suffix"] = stub_suffix
646 
647 
648 def MakeServiceIDL(dict):
649  for d in dict["service_idl"]:
650  d.impl_basename = d.idl_basename + dict["impl_suffix"]
651  d.impl_h = d.impl_basename + ".h"
652  d.impl_cpp = d.impl_basename + ".cpp"
653  d.skel_basename = d.idl_basename + dict["skel_suffix"]
654  d.skel_h = d.skel_basename + ".h"
655  d.skel_cpp = d.skel_basename + ".cpp"
656  d.stub_suffix = dict["stub_suffix"]
657  d.stub_basename = d.idl_basename + dict["stub_suffix"]
658  d.stub_h = d.stub_basename + ".h"
659  d.stub_cpp = d.stub_basename + ".cpp"
660 
661 
662 def MakeConsumerIDL(dict):
663  conslist = []
664  for cons in dict["consumer_idl"]:
665  dup = False
666  for svc in dict["service_idl"]:
667  if cons.idl_fname == svc.idl_fname:
668  dup = True
669  if not dup:
670  tmp = cons
671  tmp.stub_suffix = dict["stub_suffix"]
672  tmp.stub_basename = tmp.idl_basename \
673  + dict["stub_suffix"]
674  tmp.stub_h = tmp.stub_basename + ".h"
675  tmp.stub_cpp = tmp.stub_basename + ".cpp"
676  conslist.append(tmp)
677  dict["consumer_idl"] = conslist
678 
679 
681  acts = (("onFinalize", ""), \
682  ("onStartup", "RTC::UniqueId ec_id"), \
683  ("onShutdown", "RTC::UniqueId ec_id"), \
684  ("onActivated", "RTC::UniqueId ec_id"), \
685  ("onDeactivated", "RTC::UniqueId ec_id"), \
686  ("onExecute", "RTC::UniqueId ec_id"), \
687  ("onAborting", "RTC::UniqueId ec_id"), \
688  ("onError", "RTC::UniqueId ec_id"), \
689  ("onReset", "RTC::UniqueId ec_id"), \
690  ("onStateUpdate", "RTC::UniqueId ec_id"), \
691  ("onRateChanged", "RTC::UniqueId ec_id"))
692  actlist = []
693  for name, args in acts:
694  a = Struct()
695  a.name = name
696  a.args = args
697  actlist.append(a)
698 
699  dict["activity"] = actlist
700 
701 
703  """
704  C++ component source code generator
705  """
706  _fname_space = 16
707  def __init__(self, data, opts):
708  self.data = data.copy()
709 
710  MakeSuffix(opts, self.data)
711  MakeServiceIDL(self.data)
712  MakeConsumerIDL(self.data)
713  MakeActivityFuncs(self.data)
714  self.data["begin_brace"] = "["
715  self.data["end_brace"] = "]"
716  self.data["rcs_date"] = "$" + "Date" + "$"
717  self.data["rcs_id"] = "$" + "Id" + "$"
718  self.data["fname_h"] = self.data["fname"] + ".h"
719  self.data["fname_cpp"] = self.data["fname"] + ".cpp"
720  self.data["fname_comp"] = self.data["fname"] + "Comp.cpp"
721  self.data["makefile"] = "Makefile." + self.data["fname"]
722  self.data["u_name"] = self.data["module"].name.upper()
723  self.data["l_name"] = self.data["module"].name.lower()
724 
725 
726  self.tags = {}
727  self.tags["service_impl_h"] = service_impl_h
728  self.tags["consumer_stub_h"] = consumer_stub_h
729  self.tags["module_spec"] = module_spec
730  self.tags["config_declare"] = config_declare
731  self.tags["inport_declare"] = inport_declare
732  self.tags["outport_declare"] = outport_declare
733  self.tags["corbaport_declare"] = corbaport_declare
734  self.tags["service_declare"] = service_declare
735  self.tags["consumer_declare"] = consumer_declare
736  self.tags["initializer"] = initializer
737  self.tags["registration"] = registration
738  self.tags["bind_config"] = bind_config
739 
740  self.gen_tags(self.tags)
741  return
742 
743 
744  def print_header(self):
745  """
746  Generate component class header
747  """
748  self.gen(self.data["fname_h"],
749  comp_header, self.data, self.tags)
750  return
751 
752 
753  def print_source(self):
754  """
755  Generate component class source code
756  """
757  self.gen(self.data["fname_cpp"],
758  comp_soruce, self.data, self.tags)
759  return
760 
761 
762  def print_compsrc(self):
763  """
764  Generate component source code
765  """
766  self.gen(self.data["fname_comp"],
767  comp_compsrc, self.data, self.tags)
768  return
769 
770 
771  def print_makefile(self):
772  """
773  Generate Makefile
774  """
775  self.gen(self.data["makefile"],
776  makefile, self.data, self.tags)
777 
778 
779  def print_impl(self):
780  for svc_idl in self.data["service_idl"]:
781  fd_h = None
782  fd_cpp = None
783  lines_h = None
784  lines_cpp = None
785  if not os.access(svc_idl.idl_fname, os.F_OK):
786  sys.stderr.write("Error: IDL file \"" \
787  + svc_idl.idl_fname \
788  + "\" not found.\n")
789  sys.exit(1)
790  fd_h, lines_h = \
791  self.check_overwrite(svc_idl.impl_h)
792  fd_cpp, lines_cpp = \
793  self.check_overwrite(svc_idl.impl_cpp)
794  if not fd_h:
795  sys.stderr.write("Cannot open file:" +
796  svc_idl.impl_h + "\n")
797  sys.exit(1)
798  if not fd_cpp:
799  sys.stderr.write("Cannot open file:" +
800  svc_idl.impl_cpp + "\n")
801  sys.exit(1)
802  if lines_h or lines_cpp:
803  sys.stderr.write("Merge of service impl." +
804  "code is not supported.\n")
805  return
806 
807  try:
808  idl_include = self.data["idl_include"]
809  impl_suffix = self.data["impl_suffix"]
810  skel_suffix = self.data["skel_suffix"]
811  import cxx_svc_impl
812  ifs = cxx_svc_impl.generate(svc_idl.idl_fname,
813  idl_include,
814  impl_suffix,
815  skel_suffix,
816  fd_h, fd_cpp)
817  print " File \"" \
818  + svc_idl.impl_h \
819  + "\" was generated."
820  print " File \"" \
821  + svc_idl.impl_cpp \
822  + "\" was generated."
823  except:
824  sys.stderr.write("Generate error: " \
825  + svc_idl.impl_h
826  + ", "
827  + svc_idl.impl_cpp + "\n")
828 
829  def print_all(self):
830  self.print_impl()
831  self.print_header()
832  self.print_source()
833  self.print_compsrc()
834  self.print_makefile()
835  return
836 
837 
838 class idl2char:
839  def __init__(self, filename = None):
840  self.lines = ""
841  self.inComment = False
842  if filename:
843  f = open(filename)
844  l = f.readlines()
845  self.erase_comments(l)
846  f.close()
847 
848  def open_file(self, filename):
849  f = open(filename)
850  l = f.readlines()
851  self.erase_comments(l)
852  f.close()
853 
854  def cleanup():
855  self.lines = ""
856 
857  def erase_comments(self, lines):
858  for l in lines:
859  m = re.search("/\*.*\*/", l)
860  if m:
861  l = l.replace(m.group(), "")
862 
863  m = re.search(".*\*/", l)
864  if m:
865  l = l.replace(m.group(), "")
866  if self.inComment == True:
867  self.inComment = False
868 
869  m = re.search("/\*.*$", l)
870  if m:
871  l = l.replace(m.group(), "")
872  if self.inComment == False:
873  self.inComment = True
874 
875  m = re.search("//.*$", l)
876  if m:
877  l = l.replace(m.group(), "")
878 
879  m = re.search("\s+$", l)
880  if m:
881  l = l.replace(m.group(), "")
882  l = l + "\n"
883 
884  m = re.search("\S", l)
885  if m:
886  if not self.inComment:
887  self.lines += l
888 
889 
890  def get_lines(self):
891  return self.lines
892 
893  def make_chardata(self, var_name):
894  self.lines = self.lines.replace("\n","\\n\\\n")
895  self.lines = self.lines.replace("\"","\\\"")
896  self.lines = "static char* " + var_name \
897  + " = \"\\\n" + self.lines
898  self.lines += "\";\n"
899  return self.lines
900 
901  def add_slash(self):
902  self.lines = self.lines.replace("\n","\\\n")
903  return self.lines
904 
905  def write_file(self, filename):
906  f = open(filename, "w")
907  f.write(self.lines)
908  f.close()
909 
910 
def gen(self, fname, temp_txt, data, tags)
Definition: gen_base.py:105


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34