Skip to content

Make Vocab constexpr#1764

Merged
Nicogene merged 17 commits intorobotology:develfrom
Nicogene:vocabConstExpr
Jun 27, 2018
Merged

Make Vocab constexpr#1764
Nicogene merged 17 commits intorobotology:develfrom
Nicogene:vocabConstExpr

Conversation

@Nicogene
Copy link
Copy Markdown
Member

This PR makes vocab constexpr.

Merge this PR after : robotology/robot-testing-framework#98

Please review code

@Nicogene Nicogene added PR Type: Feat/Enh This PR adds some new feature or enhances some part of YARP Component: Library - YARP_os Component: RTF Plugins PR Status: Changelog - Not Required This PR does not need a changelog entry Target: YARP v3.1.0 labels Jun 21, 2018
@Nicogene Nicogene self-assigned this Jun 21, 2018
@Nicogene Nicogene requested a review from drdanz June 21, 2018 13:38
@PeterBowman
Copy link
Copy Markdown
Member

Idea: remove VOCABX, use default parameters: constexpr int32_t VOCAB(char a, char b = 0, char c = 0, char d = 0) { ... }. Regarding the implementation, you probably don't need so many parentheses as in a macro, perhaps even the (int) casts could be left out (not sure though, and BTW: shouldn't these be int32_t otherwise, for consistency?).

@Nicogene
Copy link
Copy Markdown
Member Author

Idea: remove VOCABX, use default parameters: constexpr int32_t VOCAB(char a, char b = 0, char c = 0, char d = 0) { ... }
@PeterBowman you are right, this another reason to move to constexpr

@Nicogene Nicogene added PR Status: Branch - OK This PR is targetting the right branch PR Status: Changelog - OK This PR has a proper changelog entry and removed PR Status: Changelog - Not Required This PR does not need a changelog entry labels Jun 21, 2018
@PeterBowman
Copy link
Copy Markdown
Member

Another idea (which I'm not quite convinced of): VOCAB('a', 'b', 'c', 'd') (global scope) or yarp::os::VOCAB('a', 'b', 'c', 'd') (C++ namespace)?

@drdanz
Copy link
Copy Markdown
Member

drdanz commented Jun 21, 2018

or maybe yarp::os::makeVocab('a', 'b', 'c', 'd'), and leave VOCAB in global scope, but deprecated

@drdanz
Copy link
Copy Markdown
Member

drdanz commented Jun 21, 2018

And by the way, it would be nice to have also a "string" version

yarp::os::makeVocab("abcd");

@drdanz
Copy link
Copy Markdown
Member

drdanz commented Jun 21, 2018

Actually there is a yarp::os::encode(const std::string&) already... that should become constexpr as well.

@Nicogene Nicogene force-pushed the vocabConstExpr branch 2 times, most recently from 6b6de85 to ed43a28 Compare June 22, 2018 13:33
Copy link
Copy Markdown
Member

@jgvictores jgvictores left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In its current state, the PR introduces some issues regarding SWIG bindings:

  1. Regression on VOCABs defined in the global scope: The changes introduced at #1696 have been removed. Even with an advanced version of SWIG, there is no generation of VOCABs defined in the global scope required by interfaces (e.g. no VOCAB_CM_POSITION). Not caught on Travis because of used SWIG version
    Recovering some of the bindings/yarp.i logic and the preprocessor protection should suffice. 😄

  2. Malformed results via wrapped new makeVocab function:

In [1]: yarp.encode("abcd")
Out[1]: 1684234849

In [2]: yarp.makeVocab('a','b','c','d')
Out[2]: <Swig Object of type 'int32_t *' at 0x7f95d9443840>

Not caught on Travis because it's new. Could definitely be added in Lua tests at tests/test_vocab.lua! 😃

@Nicogene
Copy link
Copy Markdown
Member Author

Nicogene commented Jun 23, 2018

Thanks @jgvictores for checking the bindings part, I wasn't sure about the changes, but I thought that if the compiler and the test doesn't complain is ok 😅

About the 1.: createVocab takes place of VOCAB and it is now scoped, these changes you made are still necessary?

@Nicogene Nicogene added Component: Bindings swig, python, java, ruby, perl, octave, matlab, lua, csharp, tcl Component: Library - YARP_dev Component: Devices labels Jun 23, 2018
Comment thread src/libYARP_OS/include/yarp/os/Vocab.h Outdated
// We need a constexpr for efficient switching.
// Use as, for example, makeVocab('s','e','t')
constexpr yarp::conf::vocab32_t createVocab(char a, char b = 0, char c = 0, char d = 0) {
return ((((yarp::conf::vocab32_t)(d))<<24)+(((yarp::conf::vocab32_t)(c))<<16)+(((yarp::conf::vocab32_t)(b))<<8)+((yarp::conf::vocab32_t)(a)));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit cleaner:

return ((yarp::conf::vocab32_t)d << 24) + ((yarp::conf::vocab32_t)c << 16) + ((yarp::conf::vocab32_t)b << 8) + (yarp::conf::vocab32_t)a;

I'd remove those parentheses around each parameter, the final return, and each C-style cast (ref), here and at VOCABX below, as an unnecessary remnant of the former implementation via C preprocessor macros.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think about c++-style casts, which are more explicit?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I'd really appreciate if you could expand this on more than one line, and add them in the proper order (i.e. a-b-c-d, not d-c-b-a) in order to make it more readable, like here:

https://github.com/robotology-playground/sharedlibpp/blob/45b5cae5b50c6431e5eae8bb8aebf6d751054f8b/src/shlibpp/SharedLibraryClassApi.h#L63:L67

@Nicogene Nicogene force-pushed the vocabConstExpr branch 2 times, most recently from 46b9edd to be976de Compare June 26, 2018 17:11
Comment thread src/libYARP_OS/include/yarp/os/Vocab.h Outdated
YARP_DEPRECATED_MSG("Use yarp::os::createVocab() instead")
constexpr int32_t VOCAB2(char a, char b) { return yarp::os::createVocab((a),(b),(0),(0)); }
YARP_DEPRECATED_MSG("Use yarp::os::createVocab() instead")
constexpr int32_t VOCAB1(char a) { return yarp::os::createVocab((a),(0),(0),(0)); }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #1764 (comment): yarp::os::createVocab(a,b,c,d).

@Nicogene Nicogene merged commit fbe606f into robotology:devel Jun 27, 2018
YARP_DEPRECATED_MSG("Use yarp::os::createVocab() instead")
constexpr int32_t VOCAB2(char a, char b) { return yarp::os::createVocab(a,b,0,0); }
YARP_DEPRECATED_MSG("Use yarp::os::createVocab() instead")
constexpr int32_t VOCAB1(char a) { return yarp::os::createVocab(a,0,0,0); }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these methods should be returning yarp::conf::vocab32_t as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants