multiplatform: better publisher base class

This commit is contained in:
Thomas Gubler
2015-01-22 09:30:43 +01:00
parent 2af44f5995
commit 8c4fce3654
4 changed files with 59 additions and 42 deletions
+20 -20
View File
@@ -125,26 +125,26 @@ ARCHCXXFLAGS = -fno-exceptions -fno-rtti -std=gnu++0x -fno-threadsafe-statics
# Generic warnings # Generic warnings
# #
ARCHWARNINGS = -Wall \ # ARCHWARNINGS = -Wall \
-Wextra \ # -Wextra \
-Werror \ # -Werror \
-Wdouble-promotion \ # -Wdouble-promotion \
-Wshadow \ # -Wshadow \
-Wfloat-equal \ # -Wfloat-equal \
-Wframe-larger-than=1024 \ # -Wframe-larger-than=1024 \
-Wpointer-arith \ # -Wpointer-arith \
-Wlogical-op \ # -Wlogical-op \
-Wmissing-declarations \ # -Wmissing-declarations \
-Wpacked \ # -Wpacked \
-Wno-unused-parameter \ # -Wno-unused-parameter \
-Werror=format-security \ # -Werror=format-security \
-Werror=array-bounds \ # -Werror=array-bounds \
-Wfatal-errors \ # -Wfatal-errors \
-Wformat=1 \ # -Wformat=1 \
-Werror=unused-but-set-variable \ # -Werror=unused-but-set-variable \
-Werror=unused-variable \ # -Werror=unused-variable \
-Werror=double-promotion \ # -Werror=double-promotion \
-Werror=reorder # -Werror=reorder
# -Wcast-qual - generates spurious noreturn attribute warnings, try again later # -Wcast-qual - generates spurious noreturn attribute warnings, try again later
# -Wconversion - would be nice, but too many "risky-but-safe" conversions in the code # -Wconversion - would be nice, but too many "risky-but-safe" conversions in the code
# -Wcast-align - would help catch bad casts in some cases, but generates too many false positives # -Wcast-align - would help catch bad casts in some cases, but generates too many false positives
+2 -1
View File
@@ -37,6 +37,7 @@
* *
* @author Thomas Gubler <thomasgubler@gmail.com> * @author Thomas Gubler <thomasgubler@gmail.com>
*/ */
#pragma once
#include <px4.h> #include <px4.h>
class PublisherExample { class PublisherExample {
@@ -48,5 +49,5 @@ public:
int main(); int main();
protected: protected:
px4::NodeHandle _n; px4::NodeHandle _n;
px4::Publisher * _rc_channels_pub; px4::Publisher<px4::px4_rc_channels> * _rc_channels_pub;
}; };
+7 -7
View File
@@ -124,12 +124,12 @@ public:
* Advertise topic * Advertise topic
*/ */
template<typename T> template<typename T>
Publisher* advertise() Publisher<T>* advertise()
{ {
ros::Publisher ros_pub = ros::NodeHandle::advertise<typename std::remove_reference<decltype(((T*)nullptr)->data())>::type &>(T::handle(), kQueueSizeDefault); ros::Publisher ros_pub = ros::NodeHandle::advertise<typename std::remove_reference<decltype(((T*)nullptr)->data())>::type &>(T::handle(), kQueueSizeDefault);
Publisher *pub = new Publisher(ros_pub); PublisherROS<T> *pub = new PublisherROS<T>(ros_pub);
_pubs.push_back(pub); _pubs.push_back((PublisherBase*)pub);
return pub; return (Publisher<T>*)pub;
} }
/** /**
@@ -236,16 +236,16 @@ public:
* Advertise topic * Advertise topic
*/ */
template<typename T> template<typename T>
Publisher *advertise() Publisher<T> *advertise()
{ {
//XXX //XXX
// uORB::PublicationBase * uorb_pub = new uORB::PublicationBase((new T())->handle()); // uORB::PublicationBase * uorb_pub = new uORB::PublicationBase((new T())->handle());
uORB::PublicationBase * uorb_pub = new uORB::PublicationBase(T::handle()); uORB::PublicationBase * uorb_pub = new uORB::PublicationBase(T::handle());
Publisher *pub = new Publisher(uorb_pub); PublisherUORB<T> *pub = new PublisherUORB<T>(uorb_pub);
_pubs.add(pub); _pubs.add(pub);
return pub; return (Publisher<T>*)pub;
} }
/** /**
+30 -14
View File
@@ -61,29 +61,44 @@ public:
~PublisherBase() {}; ~PublisherBase() {};
}; };
/**
* Publisher base class, templated with the message type
* */
template <typename T>
class __EXPORT Publisher
{
public:
Publisher() {};
~Publisher() {};
virtual int publish(const T &msg) = 0;
};
#if defined(__PX4_ROS) #if defined(__PX4_ROS)
class Publisher : template <typename T>
public PublisherBase class PublisherROS :
public Publisher<T>
{ {
public: public:
/** /**
* Construct Publisher by providing a ros::Publisher * Construct Publisher by providing a ros::Publisher
* @param ros_pub the ros publisher which will be used to perform the publications * @param ros_pub the ros publisher which will be used to perform the publications
*/ */
Publisher(ros::Publisher ros_pub) : PublisherROS(ros::Publisher ros_pub) :
PublisherBase(), Publisher<T>(),
_ros_pub(ros_pub) _ros_pub(ros_pub)
{} {}
~Publisher() {}; ~PublisherROS() {};
/** Publishes msg /** Publishes msg
* @param msg the message which is published to the topic * @param msg the message which is published to the topic
*/ */
template <typename T> int publish(const T &msg)
int publish(T &msg) { {
_ros_pub.publish(msg.data()); _ros_pub.publish(msg.data());
return 0;} return 0;
}
private: private:
ros::Publisher _ros_pub; /**< Handle to the ros publisher */ ros::Publisher _ros_pub; /**< Handle to the ros publisher */
}; };
@@ -104,8 +119,9 @@ public:
virtual void update() = 0; virtual void update() = 0;
}; };
class __EXPORT Publisher : template <typename T>
public PublisherBase, class __EXPORT PublisherUORB :
public Publisher<T>,
public PublisherNode public PublisherNode
{ {
@@ -113,17 +129,17 @@ public:
/** /**
* Construct Publisher by providing orb meta data * Construct Publisher by providing orb meta data
*/ */
Publisher(uORB::PublicationBase * uorb_pub) : PublisherUORB(uORB::PublicationBase * uorb_pub) :
PublisherBase(), Publisher<T>(),
PublisherNode(),
_uorb_pub(uorb_pub) _uorb_pub(uorb_pub)
{} {}
~Publisher() {}; ~PublisherUORB() {};
/** Publishes msg /** Publishes msg
* @param msg the message which is published to the topic * @param msg the message which is published to the topic
*/ */
template<typename T>
int publish(const T &msg) int publish(const T &msg)
{ {
_uorb_pub->update((void *)&(msg.data())); _uorb_pub->update((void *)&(msg.data()));