1
1
# Generate Julia composite types for ROS messages
2
2
3
+ using Compat
4
+ import Compat: String, Symbol
5
+
3
6
export @rosimport , rostypegen, rostypereset, gentypes, cleartypes
4
7
5
8
# Type definitions
6
9
# Composite types for internal use. Keeps track of the imported types and helps
7
10
# keep code generation orderly.
8
11
abstract ROSModule
9
12
type ROSPackage
10
- name:: ASCIIString
13
+ name:: String
11
14
msg:: ROSModule
12
15
srv:: ROSModule
13
- function ROSPackage (pkgname:: ASCIIString )
16
+ function ROSPackage (pkgname:: String )
14
17
pkg = new (pkgname)
15
18
pkg. msg = ROSMsgModule (pkg)
16
19
pkg. srv = ROSSrvModule (pkg)
@@ -19,25 +22,25 @@ type ROSPackage
19
22
end
20
23
type ROSMsgModule <: ROSModule
21
24
pkg:: ROSPackage
22
- members:: Vector{ASCIIString }
23
- deps:: Set{ASCIIString }
24
- ROSMsgModule (pkg) = new (pkg, ASCIIString [], Set {ASCIIString } ())
25
+ members:: Vector{String }
26
+ deps:: Set{String }
27
+ ROSMsgModule (pkg) = new (pkg, String [], Set {String } ())
25
28
end
26
29
type ROSSrvModule <: ROSModule
27
30
pkg:: ROSPackage
28
- members:: Vector{ASCIIString }
29
- deps:: Set{ASCIIString }
30
- ROSSrvModule (pkg) = new (pkg, ASCIIString [], Set {ASCIIString } ())
31
+ members:: Vector{String }
32
+ deps:: Set{String }
33
+ ROSSrvModule (pkg) = new (pkg, String [], Set {String } ())
31
34
end
32
35
33
36
# These two global objects maintain the hierarchy from multiple calls to
34
37
# `@rosimport` and keep the link to the Python objects whenever communication
35
38
# goes between RobotOS and rospy.
36
- const _rospy_imports = Dict {ASCIIString ,ROSPackage} ()
37
- const _rospy_objects = Dict {ASCIIString ,PyObject} ()
38
- const _rospy_modules = Dict {ASCIIString ,PyObject} ()
39
+ const _rospy_imports = Dict {String ,ROSPackage} ()
40
+ const _rospy_objects = Dict {String ,PyObject} ()
41
+ const _rospy_modules = Dict {String ,PyObject} ()
39
42
40
- const _ros_builtin_types = Dict {ASCIIString , Symbol} (
43
+ const _ros_builtin_types = Dict {String , Symbol} (
41
44
" bool" => :Bool ,
42
45
" int8" => :Int8 ,
43
46
" int16" => :Int16 ,
@@ -49,7 +52,7 @@ const _ros_builtin_types = Dict{ASCIIString, Symbol}(
49
52
" uint64" => :UInt64 ,
50
53
" float32" => :Float32 ,
51
54
" float64" => :Float64 ,
52
- " string" => :ASCIIString ,
55
+ " string" => :String ,
53
56
" time" => :Time ,
54
57
" duration" => :Duration ,
55
58
# Deprecated but supported
@@ -71,7 +74,7 @@ macro rosimport(input)
71
74
if input. head == :tuple
72
75
@assert isa (input. args[1 ], Expr) " Improper @rosimport input"
73
76
@assert input. args[1 ]. head == :(:) " First argument needs ':' following"
74
- types = ASCIIString []
77
+ types = String []
75
78
pkg, ismsg, typ = _pkgtype_import (input. args[1 ])
76
79
push! (types, typ)
77
80
for t in input. args[2 : end ]
@@ -109,7 +112,7 @@ function _pkgtype_import(input::Expr)
109
112
return ps,msb,ts
110
113
end
111
114
# Import a set of types from a single package
112
- function _rosimport (package:: ASCIIString , ismsg:: Bool , names:: ASCIIString ... )
115
+ function _rosimport (package:: String , ismsg:: Bool , names:: String ... )
113
116
global _rospy_imports
114
117
if ! haskey (_rospy_imports, package)
115
118
@debug (" Importing new package: " ,package," ." , ismsg ? " msg" : " srv" )
145
148
146
149
# Populate the module with a new message type. Import and add dependencies first
147
150
# so they will appear first in the generated code.
148
- function addtype! (mod:: ROSMsgModule , typ:: ASCIIString )
151
+ function addtype! (mod:: ROSMsgModule , typ:: String )
149
152
global _rospy_objects
150
153
if ! (typ in mod. members)
151
154
@debug (" Message type import: " , _fullname (mod), " ." , typ)
165
168
166
169
# Populate the module with a new service type. Import and add dependencies
167
170
# first.
168
- function addtype! (mod:: ROSSrvModule , typ:: ASCIIString )
171
+ function addtype! (mod:: ROSSrvModule , typ:: String )
169
172
global _rospy_objects
170
173
if ! (typ in mod. members)
171
174
@debug (" Service type import: " , _fullname (mod), " ." , typ)
@@ -191,7 +194,7 @@ function addtype!(mod::ROSSrvModule, typ::ASCIIString)
191
194
end
192
195
193
196
# Return the python module and python object for a particular type
194
- function _pyvars (modname:: ASCIIString , typ:: ASCIIString )
197
+ function _pyvars (modname:: String , typ:: String )
195
198
pymod = _import_rospy_pkg (modname)
196
199
pyobj =
197
200
try pymod[typ]
@@ -233,7 +236,7 @@ function _importdeps!(mod::ROSModule, deps::Vector)
233
236
end
234
237
235
238
# Bring in the python modules as needed
236
- function _import_rospy_pkg (package:: ASCIIString )
239
+ function _import_rospy_pkg (package:: String )
237
240
global _rospy_modules
238
241
if ! haskey (_rospy_modules, package)
239
242
@debug (" Importing python package: " , package)
@@ -252,7 +255,7 @@ function buildpackage(pkg::ROSPackage)
252
255
@debug (" Building package: " , _name (pkg))
253
256
254
257
# Create the top-level module for the package in Main
255
- pkgsym = symbol (_name (pkg))
258
+ pkgsym = Symbol (_name (pkg))
256
259
pkgcode = Expr (:toplevel , :(module ($ pkgsym) end ))
257
260
Main. eval (pkgcode)
258
261
pkgmod = Main. eval (pkgsym)
@@ -284,14 +287,10 @@ function modulecode(mod::ROSModule)
284
287
modcode = Expr[]
285
288
286
289
# Common imports
287
- if VERSION < v " 0.4-"
288
- push! (modcode,
289
- quote
290
- using Compat
291
- end )
292
- end
293
290
push! (modcode,
294
291
quote
292
+ using Compat
293
+ import Compat: String, Symbol
295
294
using PyCall
296
295
import Base: convert, getindex
297
296
import RobotOS
320
319
function _importexprs (mod:: ROSMsgModule )
321
320
imports = Expr[Expr (:import , :RobotOS , :MsgT )]
322
321
othermods = filter (d -> d != _name (mod), mod. deps)
323
- append! (imports, [Expr (:using ,symbol (m),:msg ) for m in othermods])
322
+ append! (imports, [Expr (:using ,Symbol (m),:msg ) for m in othermods])
324
323
imports
325
324
end
326
325
function _importexprs (mod:: ROSSrvModule )
@@ -330,32 +329,32 @@ function _importexprs(mod::ROSSrvModule)
330
329
Expr (:import , :RobotOS , :_srv_reqtype ),
331
330
Expr (:import , :RobotOS , :_srv_resptype ),
332
331
]
333
- append! (imports, [Expr (:using ,symbol (m),:msg ) for m in mod. deps])
332
+ append! (imports, [Expr (:using ,Symbol (m),:msg ) for m in mod. deps])
334
333
imports
335
334
end
336
335
337
336
# The exported names for each module
338
337
function _exportexpr (mod:: ROSMsgModule )
339
338
exportexpr = Expr (:export )
340
339
for m in mod. members
341
- push! (exportexpr. args, symbol (_jl_safe_name (m," Msg" )))
340
+ push! (exportexpr. args, Symbol (_jl_safe_name (m," Msg" )))
342
341
end
343
342
exportexpr
344
343
end
345
344
function _exportexpr (mod:: ROSSrvModule )
346
345
exportexpr = Expr (:export )
347
346
for typ in mod. members
348
347
push! (exportexpr. args,
349
- symbol (typ),
350
- symbol (string (typ," Request" )),
351
- symbol (string (typ," Response" ))
348
+ Symbol (typ),
349
+ Symbol (string (typ," Request" )),
350
+ Symbol (string (typ," Response" ))
352
351
)
353
352
end
354
353
exportexpr
355
354
end
356
355
357
356
# All the generated code for a generated message type
358
- function buildtype (mod:: ROSMsgModule , typename:: ASCIIString )
357
+ function buildtype (mod:: ROSMsgModule , typename:: String )
359
358
global _rospy_objects
360
359
fulltypestr = _rostypestr (mod, typename)
361
360
pyobj = _rospy_objects[fulltypestr]
368
367
369
368
# All the generated code for a generated service type
370
369
# Will create 3 different composite types.
371
- function buildtype (mod:: ROSSrvModule , typename:: ASCIIString )
370
+ function buildtype (mod:: ROSSrvModule , typename:: String )
372
371
global _rospy_objects
373
372
374
373
req_typestr = _rostypestr (mod, string (typename," Request" ))
@@ -387,9 +386,9 @@ function buildtype(mod::ROSSrvModule, typename::ASCIIString)
387
386
pyresp = :(RobotOS. _rospy_objects[$ resp_typestr])
388
387
respexprs = typecode (resp_typestr, :SrvT , respmems)
389
388
390
- defsym = symbol (typename)
391
- reqsym = symbol (string (typename," Request" ))
392
- respsym = symbol (string (typename," Response" ))
389
+ defsym = Symbol (typename)
390
+ reqsym = Symbol (string (typename," Request" ))
391
+ respsym = Symbol (string (typename," Response" ))
393
392
srvexprs = Expr[
394
393
:(immutable $ defsym <: ServiceDefinition end ),
395
394
:(_typerepr (:: Type{$defsym} ) = $ (_rostypestr (mod,typename))),
406
405
# (3) convert(PyObject, ...)
407
406
# (4) convert(..., o::PyObject)
408
407
# (5) getindex for accessing member constants
409
- function typecode (rosname:: ASCIIString , super:: Symbol , members:: Vector )
408
+ function typecode (rosname:: String , super:: Symbol , members:: Vector )
410
409
tname = _splittypestr (rosname)[2 ]
411
410
@debug (" Type: " , tname)
412
411
@@ -415,7 +414,7 @@ function typecode(rosname::ASCIIString, super::Symbol, members::Vector)
415
414
suffix = if super == :MsgT ; " Msg"
416
415
elseif super == :SrvT ; " Srv"
417
416
else ; " ROS" end
418
- jlsym = symbol (_jl_safe_name (tname,suffix))
417
+ jlsym = Symbol (_jl_safe_name (tname,suffix))
419
418
420
419
exprs = Expr[]
421
420
# First the empty expressions
@@ -446,7 +445,7 @@ function typecode(rosname::ASCIIString, super::Symbol, members::Vector)
446
445
# (4) Convert from PyObject
447
446
push! (exprs, :(
448
447
function convert (jlt:: Type{$jlsym} , o:: PyObject )
449
- if convert (ASCIIString , o[" _type" ]) != _typerepr (jlt)
448
+ if convert (String , o[" _type" ]) != _typerepr (jlt)
450
449
throw (InexactError ())
451
450
end
452
451
jl = $ jlsym ()
@@ -487,7 +486,7 @@ function _addtypemember!(exprs, namestr, typestr)
487
486
488
487
typestr, arraylen = _check_array_type (typestr)
489
488
if _isrostype (typestr)
490
- j_typ = symbol (_splittypestr (typestr)[2 ])
489
+ j_typ = Symbol (_splittypestr (typestr)[2 ])
491
490
# Default has to be deferred until the types exist
492
491
j_def = Expr (:call , j_typ)
493
492
else
@@ -499,7 +498,7 @@ function _addtypemember!(exprs, namestr, typestr)
499
498
j_def = @eval _typedefault ($ j_typ)
500
499
end
501
500
502
- namesym = symbol (namestr)
501
+ namesym = Symbol (namestr)
503
502
if arraylen >= 0
504
503
memexpr = :($ namesym:: Array{$j_typ,1} )
505
504
defexpr = :([$ j_def for i = 1 : $ arraylen])
@@ -559,15 +558,15 @@ function _order(d::Dict)
559
558
end
560
559
end
561
560
end
562
- tlist = ASCIIString []
561
+ tlist = String []
563
562
for t in keys (d)
564
563
trecurse! (tlist, d, t)
565
564
end
566
565
tlist
567
566
end
568
567
569
- _rostypestr (mod:: ROSModule , name:: ASCIIString ) = string (_name (mod)," /" ,name)
570
- function _splittypestr (typestr:: ASCIIString )
568
+ _rostypestr (mod:: ROSModule , name:: String ) = string (_name (mod)," /" ,name)
569
+ function _splittypestr (typestr:: String )
571
570
if ! _isrostype (typestr)
572
571
error (string (" Invalid message type '$typestr ', " ,
573
572
" use 'package_name/type_name'" ))
@@ -577,12 +576,12 @@ function _splittypestr(typestr::ASCIIString)
577
576
end
578
577
# Valid ROS type string is all word chars split by a single forward slash, with
579
578
# optional square brackets for array types
580
- _isrostype (s:: ASCIIString ) = ismatch (r" ^\w +/\w +(?:\[\d *\] )?$" , s)
579
+ _isrostype (s:: String ) = ismatch (r" ^\w +/\w +(?:\[\d *\] )?$" , s)
581
580
582
581
# Sanitize a string by checking for and removing brackets if they are present
583
582
# Return the sanitized type and the number inside the brackets if it is a fixed
584
583
# size type. Returns 0 if variable size (no number), -1 if no brackets
585
- function _check_array_type (typ:: ASCIIString )
584
+ function _check_array_type (typ:: String )
586
585
arraylen = - 1
587
586
m = match (r" ^([\w /]+)\[ (\d *)\] $" , typ)
588
587
if m != nothing
@@ -620,11 +619,11 @@ _jl_safe_name(name::AbstractString, suffix) = _nameconflicts(name) ?
620
619
621
620
# Check if the type name conflicts with a Julia builtin. Currently this is only
622
621
# some of the messages from the std_msgs.msg package
623
- _nameconflicts (typename:: ASCIIString ) = isdefined (Base, symbol (typename))
622
+ _nameconflicts (typename:: String ) = isdefined (Base, Symbol (typename))
624
623
625
624
# Get a default value for any builtin ROS type
626
625
_typedefault {T<:Real} (:: Type{T} ) = zero (T)
627
- _typedefault (:: Type{ASCIIString } ) = " "
626
+ _typedefault (:: Type{String } ) = " "
628
627
_typedefault (:: Type{Time} ) = Time (0 ,0 )
629
628
_typedefault (:: Type{Duration} ) = Duration (0 ,0 )
630
629
0 commit comments