Skip to content

Commit

Permalink
Merge pull request robotology#1696 from jgvictores/swig-vocab-global-…
Browse files Browse the repository at this point in the history
…scope

Fix and test SWIG wrapper generation of VOCABs defined in the global scope
  • Loading branch information
drdanz authored May 25, 2018
2 parents 6983a23 + c710202 commit c0b4ee6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
8 changes: 8 additions & 0 deletions bindings/lua/tests/test_vocab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ function test_vocab_pixel_types_enum()
assert(909209453 == yarp.VOCAB_PIXEL_MONO16) -- VOCAB4 with '1' and '6'
end

function test_vocab_global_scope()
assert("number" == type(yarp.VOCAB_CM_POSITION))
assert(7565168 == yarp.VOCAB_CM_POSITION) -- VOCAB3
assert(1685286768 == yarp.VOCAB_CM_POSITION_DIRECT) -- VOCAB4
assert(845375334 == yarp.VOCAB_FRAMEGRABBER_CONTROL2) -- VOCAB4 with '2'
end

test_vocab()
test_vocab_pixel_types_enum()
--test_vocab_global_scope() -- Requires SWIG_VERSION >= 0x030011 (3.0.11), see yarp.i

28 changes: 16 additions & 12 deletions bindings/yarp.i
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,24 @@ namespace yarp {
}
}

#if defined(SWIGCSHARP)
%define YARP_OS_VOCAB_H 1
%enddef
%define VOCAB(a,b,c,d) 0
%enddef
%define VOCAB4(a,b,c,d) VOCAB((a),(b),(c),(d))
%enddef
%define VOCAB3(a,b,c) VOCAB((a),(b),(c),(0))
%enddef
%define VOCAB2(a,b) VOCAB((a),(b),(0),(0))
// We skip macros from Vocab.h via SWIG_PREPROCESSOR_SHOULD_SKIP_THIS directive
// But cannot define YARP_OS_VOCAB_H as we would miss encode/decode
#if (SWIG_VERSION >= 0x030011) && (!defined(SWIGCSHARP))
%define VOCAB(x1,x2,x3,x4) x4*16777216+x3*65536+x2*256+x1 // Tested on Lua and Python
%enddef
%define VOCAB1(a) VOCAB((a),(0),(0),(0))
#elif defined(SWIGCSHARP)
// We'd rather no VOCAB vs a defective VOCAB (value 0), but required for CSHARP.
%define VOCAB(x1,x2,x3,x4) 0 // VOCABs in enum should be generated in Lua and Python, not C#
%enddef
#endif
#endif // If old SWIG and not CSHARP, no global defined (but enum should be) VOCABs wrappers generated
%define VOCAB4(x1,x2,x3,x4) VOCAB(x1,x2,x3,x4)
%enddef
%define VOCAB3(x1,x2,x3) VOCAB(x1,x2,x3,0)
%enddef
%define VOCAB2(x1,x2) VOCAB(x1,x2,0,0)
%enddef
%define VOCAB1(x1) VOCAB(x1,0,0,0)
%enddef

%define YARP_BEGIN_PACK
%enddef
Expand Down
7 changes: 4 additions & 3 deletions doc/release/v2_3_72_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ yarpserver is launched with `--ros` option. (#722)

* Fixed out of source builds.
* Added `SWIG_PREPROCESSOR_SHOULD_SKIP_THIS` to avoid the SWIG preprocessor from
parsing `VOCAB`. This avoids hacks required for compilation of bindings (hacks
removed), and enables the correct generation of `VOCAB`s inside `enum`s.
parsing `VOCAB`. Introduced SWIG-compliant working `VOCAB` definitions in
`yarp.i`, with required checks on target language and SWIG version.
* Removed related hacks required for compilation from YARP internals.
* Added `test_vocab.lua` to catch regressions on `VOCAB` in the future.
* Rename `test_string.lua` to more representative `test_port.lua`, enable it to
* Renamed `test_string.lua` to more representative `test_port.lua`, enabled it to
run without the `yarpserver`, and integrate it in the test infrastructure.

Contributors
Expand Down
4 changes: 2 additions & 2 deletions src/libYARP_OS/include/yarp/os/Vocab.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ namespace yarp {
}

// We need a macro for efficient switching.
// Use as, for example, VOCAB('s','e','t')
// Use as, for example, VOCAB3('s','e','t')
#ifndef SWIG_PREPROCESSOR_SHOULD_SKIP_THIS
#define VOCAB(a,b,c,d) ((((int)(d))<<24)+(((int)(c))<<16)+(((int)(b))<<8)+((int)(a)))
#endif // SWIG_PREPROCESSOR_SHOULD_SKIP_THIS
#define VOCAB4(a,b,c,d) VOCAB((a),(b),(c),(d))
#define VOCAB3(a,b,c) VOCAB((a),(b),(c),(0))
#define VOCAB2(a,b) VOCAB((a),(b),(0),(0))
#define VOCAB1(a) VOCAB((a),(0),(0),(0))
#endif // SWIG_PREPROCESSOR_SHOULD_SKIP_THIS

/**
* Short readable codes. They are integers, for efficient switching,
Expand Down

0 comments on commit c0b4ee6

Please sign in to comment.