How to correctly copy Boost arrays in ROS callbacks?
I am relatively new to C++ (I am experienced in C and embedded C), some C++ concepts are still difficult to wrap my head around.
I am trying to copy boost array from a ROS callback. For those who are not familiar with ROS, the callback in my program (node) passes a reference to an object from the ROS messaging system.
The callback's declaration is: void callback(const boost::shared_ptr<std_msgs::uint8_t>&);
Let's say I have two references that I want to copy from the callback, one of bool foo
and another one boost::array<uint8_t, 8UL> bar
.
Now, this is how I can copy the bool type:
bool x;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
x = msg->foo;
}
This works fine, copies the value into another variable before the object is destructed.
Now, I am not familiar with Boost at all. Boost's array public construct/copy/destruct is this (reference):
template<typename U> array& operator=(const array<U, N>& other);
Effects:
std::copy(rhs.begin(),rhs.end(), begin())
Here's how I am trying to do to copy bar
variable:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
std::copy(msg->bar.begin(), msg->bar.end() + 8, y.begin());
}
This again works fine, however is that the correct way of doing it? As far as I know std::copy
function uses iterators, I don't exactly understand how this works (and if it's correct). I am familiar with memcpy()
, could I use that instead, and if so, what would be the advantages?
EDIT: Also, if I did something like this:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
y = msg->bar;
}
Would that be copied or referenced?
c++ arrays boost callback ros
add a comment |
I am relatively new to C++ (I am experienced in C and embedded C), some C++ concepts are still difficult to wrap my head around.
I am trying to copy boost array from a ROS callback. For those who are not familiar with ROS, the callback in my program (node) passes a reference to an object from the ROS messaging system.
The callback's declaration is: void callback(const boost::shared_ptr<std_msgs::uint8_t>&);
Let's say I have two references that I want to copy from the callback, one of bool foo
and another one boost::array<uint8_t, 8UL> bar
.
Now, this is how I can copy the bool type:
bool x;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
x = msg->foo;
}
This works fine, copies the value into another variable before the object is destructed.
Now, I am not familiar with Boost at all. Boost's array public construct/copy/destruct is this (reference):
template<typename U> array& operator=(const array<U, N>& other);
Effects:
std::copy(rhs.begin(),rhs.end(), begin())
Here's how I am trying to do to copy bar
variable:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
std::copy(msg->bar.begin(), msg->bar.end() + 8, y.begin());
}
This again works fine, however is that the correct way of doing it? As far as I know std::copy
function uses iterators, I don't exactly understand how this works (and if it's correct). I am familiar with memcpy()
, could I use that instead, and if so, what would be the advantages?
EDIT: Also, if I did something like this:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
y = msg->bar;
}
Would that be copied or referenced?
c++ arrays boost callback ros
1
'Would that be copied or referenced?' Copied. It calls the assigment operator for the boost array which copies the elements. Note there is no way in C++ to copy a reference. Maybe that helps.
– john
Jan 20 at 14:47
@john, thank you, that's really helpful. What about the correct way of copying?
– user10940762
Jan 20 at 14:50
1
Using the assignment operator is the correct way (in this case). Usingstd::copy
comes to the same thing but is less idiomatic.
– john
Jan 20 at 14:51
@john, after looking at that reference link I can now see what you mean (even though I am not very familiar with templates and OOP). If you put this in an answer I will give it a star, thank you!
– user10940762
Jan 20 at 15:10
add a comment |
I am relatively new to C++ (I am experienced in C and embedded C), some C++ concepts are still difficult to wrap my head around.
I am trying to copy boost array from a ROS callback. For those who are not familiar with ROS, the callback in my program (node) passes a reference to an object from the ROS messaging system.
The callback's declaration is: void callback(const boost::shared_ptr<std_msgs::uint8_t>&);
Let's say I have two references that I want to copy from the callback, one of bool foo
and another one boost::array<uint8_t, 8UL> bar
.
Now, this is how I can copy the bool type:
bool x;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
x = msg->foo;
}
This works fine, copies the value into another variable before the object is destructed.
Now, I am not familiar with Boost at all. Boost's array public construct/copy/destruct is this (reference):
template<typename U> array& operator=(const array<U, N>& other);
Effects:
std::copy(rhs.begin(),rhs.end(), begin())
Here's how I am trying to do to copy bar
variable:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
std::copy(msg->bar.begin(), msg->bar.end() + 8, y.begin());
}
This again works fine, however is that the correct way of doing it? As far as I know std::copy
function uses iterators, I don't exactly understand how this works (and if it's correct). I am familiar with memcpy()
, could I use that instead, and if so, what would be the advantages?
EDIT: Also, if I did something like this:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
y = msg->bar;
}
Would that be copied or referenced?
c++ arrays boost callback ros
I am relatively new to C++ (I am experienced in C and embedded C), some C++ concepts are still difficult to wrap my head around.
I am trying to copy boost array from a ROS callback. For those who are not familiar with ROS, the callback in my program (node) passes a reference to an object from the ROS messaging system.
The callback's declaration is: void callback(const boost::shared_ptr<std_msgs::uint8_t>&);
Let's say I have two references that I want to copy from the callback, one of bool foo
and another one boost::array<uint8_t, 8UL> bar
.
Now, this is how I can copy the bool type:
bool x;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
x = msg->foo;
}
This works fine, copies the value into another variable before the object is destructed.
Now, I am not familiar with Boost at all. Boost's array public construct/copy/destruct is this (reference):
template<typename U> array& operator=(const array<U, N>& other);
Effects:
std::copy(rhs.begin(),rhs.end(), begin())
Here's how I am trying to do to copy bar
variable:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
std::copy(msg->bar.begin(), msg->bar.end() + 8, y.begin());
}
This again works fine, however is that the correct way of doing it? As far as I know std::copy
function uses iterators, I don't exactly understand how this works (and if it's correct). I am familiar with memcpy()
, could I use that instead, and if so, what would be the advantages?
EDIT: Also, if I did something like this:
boost::array<uint8_t, 8UL> y;
void callback(const boost::shared_ptr<std_msgs::uint8_t>& msg) {
y = msg->bar;
}
Would that be copied or referenced?
c++ arrays boost callback ros
c++ arrays boost callback ros
edited Jan 20 at 14:49
asked Jan 20 at 14:15
user10940762
1
'Would that be copied or referenced?' Copied. It calls the assigment operator for the boost array which copies the elements. Note there is no way in C++ to copy a reference. Maybe that helps.
– john
Jan 20 at 14:47
@john, thank you, that's really helpful. What about the correct way of copying?
– user10940762
Jan 20 at 14:50
1
Using the assignment operator is the correct way (in this case). Usingstd::copy
comes to the same thing but is less idiomatic.
– john
Jan 20 at 14:51
@john, after looking at that reference link I can now see what you mean (even though I am not very familiar with templates and OOP). If you put this in an answer I will give it a star, thank you!
– user10940762
Jan 20 at 15:10
add a comment |
1
'Would that be copied or referenced?' Copied. It calls the assigment operator for the boost array which copies the elements. Note there is no way in C++ to copy a reference. Maybe that helps.
– john
Jan 20 at 14:47
@john, thank you, that's really helpful. What about the correct way of copying?
– user10940762
Jan 20 at 14:50
1
Using the assignment operator is the correct way (in this case). Usingstd::copy
comes to the same thing but is less idiomatic.
– john
Jan 20 at 14:51
@john, after looking at that reference link I can now see what you mean (even though I am not very familiar with templates and OOP). If you put this in an answer I will give it a star, thank you!
– user10940762
Jan 20 at 15:10
1
1
'Would that be copied or referenced?' Copied. It calls the assigment operator for the boost array which copies the elements. Note there is no way in C++ to copy a reference. Maybe that helps.
– john
Jan 20 at 14:47
'Would that be copied or referenced?' Copied. It calls the assigment operator for the boost array which copies the elements. Note there is no way in C++ to copy a reference. Maybe that helps.
– john
Jan 20 at 14:47
@john, thank you, that's really helpful. What about the correct way of copying?
– user10940762
Jan 20 at 14:50
@john, thank you, that's really helpful. What about the correct way of copying?
– user10940762
Jan 20 at 14:50
1
1
Using the assignment operator is the correct way (in this case). Using
std::copy
comes to the same thing but is less idiomatic.– john
Jan 20 at 14:51
Using the assignment operator is the correct way (in this case). Using
std::copy
comes to the same thing but is less idiomatic.– john
Jan 20 at 14:51
@john, after looking at that reference link I can now see what you mean (even though I am not very familiar with templates and OOP). If you put this in an answer I will give it a star, thank you!
– user10940762
Jan 20 at 15:10
@john, after looking at that reference link I can now see what you mean (even though I am not very familiar with templates and OOP). If you put this in an answer I will give it a star, thank you!
– user10940762
Jan 20 at 15:10
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277342%2fhow-to-correctly-copy-boost-arrays-in-ros-callbacks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277342%2fhow-to-correctly-copy-boost-arrays-in-ros-callbacks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
'Would that be copied or referenced?' Copied. It calls the assigment operator for the boost array which copies the elements. Note there is no way in C++ to copy a reference. Maybe that helps.
– john
Jan 20 at 14:47
@john, thank you, that's really helpful. What about the correct way of copying?
– user10940762
Jan 20 at 14:50
1
Using the assignment operator is the correct way (in this case). Using
std::copy
comes to the same thing but is less idiomatic.– john
Jan 20 at 14:51
@john, after looking at that reference link I can now see what you mean (even though I am not very familiar with templates and OOP). If you put this in an answer I will give it a star, thank you!
– user10940762
Jan 20 at 15:10