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


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:42