Program Listing for File publisher.hpp
↰ Return to documentation for file (include/naoqi_driver/publisher/publisher.hpp)
/*
* Copyright 2015 Aldebaran
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef PUBLISHER_HPP
#define PUBLISHER_HPP
#include <string>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <rclcpp/rclcpp.hpp>
namespace naoqi
{
namespace publisher
{
class Publisher
{
public:
template<typename T>
Publisher( const T& pub ):
pubPtr_( boost::make_shared<PublisherModel<T> >(pub) )
{}
bool isInitialized() const
{
return pubPtr_->isInitialized();
}
bool isSubscribed() const
{
return pubPtr_->isSubscribed();
}
void reset( rclcpp::Node* node )
{
std::cout << topic() << " is resetting" << std::endl;
pubPtr_->reset( node );
std::cout << topic() << " reset" << std::endl;
}
std::string topic() const
{
return pubPtr_->topic();
}
friend bool operator==( const Publisher& lhs, const Publisher& rhs )
{
// decision made for OR-comparison since we want to be more restrictive
if ( lhs.topic() == rhs.topic() )
return true;
return false;
}
friend bool operator==( const boost::shared_ptr<Publisher>& lhs, const boost::shared_ptr<Publisher>& rhs )
{
return operator==( *lhs, *rhs );
}
private:
struct PublisherConcept
{
virtual ~PublisherConcept(){}
virtual bool isInitialized() const = 0;
virtual bool isSubscribed() const = 0;
virtual void reset( rclcpp::Node* node ) = 0;
virtual std::string topic() const = 0;
};
template<typename T>
struct PublisherModel : public PublisherConcept
{
PublisherModel( const T& other ):
publisher_( other )
{}
std::string topic() const
{
return publisher_->topic();
}
bool isInitialized() const
{
return publisher_->isInitialized();
}
bool isSubscribed() const
{
return publisher_->isSubscribed();
}
void reset( rclcpp::Node* node )
{
publisher_->reset( node );
}
T publisher_;
};
boost::shared_ptr<PublisherConcept> pubPtr_;
}; // class publisher
} //publisher
} //naoqi
#endif