##

CallbackHelper.hh
Go to the documentation of this file.
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* 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 _CALLBACKHELPER_HH_
#define _CALLBACKHELPER_HH_
#include <google/protobuf/message.h>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <string>
#include <mutex>
#include "gazebo/common/Console.hh"
#include "gazebo/msgs/msgs.hh"
#include "gazebo/common/Exception.hh"
#include "gazebo/transport/TransportTypes.hh"
#include "gazebo/util/system.hh"
namespace gazebo
{
namespace transport
{
class GZ_TRANSPORT_VISIBLE CallbackHelper
{
public: explicit CallbackHelper(bool _latching = false);
public: virtual ~CallbackHelper();
public: virtual std::string GetMsgType() const;
public: virtual bool HandleData(const std::string &_newdata,
boost::function<void(uint32_t)> _cb, uint32_t _id) = 0;
public: virtual bool HandleMessage(MessagePtr _newMsg) = 0;
public: virtual bool IsLocal() const = 0;
public: bool GetLatching() const;
public: void SetLatching(bool _latch);
public: unsigned int GetId() const;
protected: bool latching;
protected: mutable std::mutex latchingMutex;
private: static unsigned int idCounter;
private: unsigned int id;
};
typedef boost::shared_ptr<CallbackHelper> CallbackHelperPtr;
template<class M>
class CallbackHelperT : public CallbackHelper
{
public: CallbackHelperT(const boost::function<
void (const boost::shared_ptr<M const> &)> &_cb,
bool _latching = false)
: CallbackHelper(_latching), callback(_cb)
{
// Just some code to make sure we have a google protobuf.
/*M test;
google::protobuf::Message *m;
if ((m =dynamic_cast<google::protobuf::Message*>(&test))
== NULL)
gzthrow("Message type must be a google::protobuf type\n");
*/
}
// documentation inherited
public: std::string GetMsgType() const
{
M test;
google::protobuf::Message *m;
if ((m = dynamic_cast<google::protobuf::Message*>(&test))
== NULL)
gzthrow("Message type must be a google::protobuf type\n");
return m->GetTypeName();
}
// documentation inherited
public: virtual bool HandleData(const std::string &_newdata,
boost::function<void(uint32_t)> _cb, uint32_t _id)
{
this->SetLatching(false);
boost::shared_ptr<M> m(new M);
m->ParseFromString(_newdata);
this->callback(m);
if (!_cb.empty())
_cb(_id);
return true;
}
// documentation inherited
public: virtual bool HandleMessage(MessagePtr _newMsg)
{
this->SetLatching(false);
this->callback(boost::dynamic_pointer_cast<M>(_newMsg));
return true;
}
// documentation inherited
public: virtual bool IsLocal() const
{
return true;
}
private: boost::function<void (const boost::shared_ptr<M const> &)>
callback;
};
class GZ_TRANSPORT_VISIBLE RawCallbackHelper : public CallbackHelper
{
public: RawCallbackHelper(
const boost::function<void (const std::string &)> &_cb,
bool _latching = false)
: CallbackHelper(_latching), callback(_cb)
{
}
// documentation inherited
public: std::string GetMsgType() const
{
return "raw";
}
// documentation inherited
public: virtual bool HandleData(const std::string &_newdata,
boost::function<void(uint32_t)> _cb, uint32_t _id)
{
this->SetLatching(false);
this->callback(_newdata);
if (!_cb.empty())
_cb(_id);
return true;
}
// documentation inherited
public: virtual bool HandleMessage(MessagePtr _newMsg)
{
this->SetLatching(false);
std::string data;
_newMsg->SerializeToString(&data);
this->callback(data);
return true;
}
// documentation inherited
public: virtual bool IsLocal() const
{
return true;
}
private: boost::function<void (const std::string &)> callback;
};
}
}
#endif
gazebo::transport::RawCallbackHelper::GetMsgType
std::string GetMsgType() const
Get the typename of the message that is handled.
Definition: CallbackHelper.hh:191
gazebo::transport::CallbackHelperT::GetMsgType
std::string GetMsgType() const
Get the typename of the message that is handled.
Definition: CallbackHelper.hh:132
gazebo::transport::CallbackHelper::~CallbackHelper
virtual ~CallbackHelper()
Destructor.
gazebo
Forward declarations for the common classes.
Definition: Animation.hh:26
Exception.hh
gazebo::transport::MessagePtr
boost::shared_ptr< google::protobuf::Message > MessagePtr
Definition: TransportTypes.hh:45
NULL
#define NULL
Definition: CommonTypes.hh:31
gzthrow
#define gzthrow(msg)
This macro logs an error to the throw stream and throws an exception that contains the file name and ...
Definition: Exception.hh:39
gazebo::transport::CallbackHelper
A helper class to handle callbacks when messages arrive.
Definition: CallbackHelper.hh:44
gazebo::transport::CallbackHelper::HandleData
virtual bool HandleData(const std::string &_newdata, boost::function< void(uint32_t)> _cb, uint32_t _id)=0
Process new incoming data.
TransportTypes.hh
Forward declarations for transport.
Console.hh
gazebo::transport::CallbackHelperT::HandleData
virtual bool HandleData(const std::string &_newdata, boost::function< void(uint32_t)> _cb, uint32_t _id)
Process new incoming data.
Definition: CallbackHelper.hh:143
system.hh
gazebo::transport::CallbackHelper::HandleMessage
virtual bool HandleMessage(MessagePtr _newMsg)=0
Process new incoming message.
gazebo::transport::RawCallbackHelper::HandleMessage
virtual bool HandleMessage(MessagePtr _newMsg)
Process new incoming message.
Definition: CallbackHelper.hh:208
msgs.hh
gazebo::transport::CallbackHelper::GetId
unsigned int GetId() const
Get the unique ID of this callback.
gazebo::transport::CallbackHelper::GetMsgType
virtual std::string GetMsgType() const
Get the typename of the message that is handled.
gazebo::transport::CallbackHelper::latchingMutex
std::mutex latchingMutex
Mutex to protect the latching variable.
Definition: CallbackHelper.hh:95
gazebo::transport::RawCallbackHelper::HandleData
virtual bool HandleData(const std::string &_newdata, boost::function< void(uint32_t)> _cb, uint32_t _id)
Process new incoming data.
Definition: CallbackHelper.hh:197
gazebo::transport::RawCallbackHelper::IsLocal
virtual bool IsLocal() const
Is the callback local?
Definition: CallbackHelper.hh:219
gazebo::transport::RawCallbackHelper::RawCallbackHelper
RawCallbackHelper(const boost::function< void(const std::string &)> &_cb, bool _latching=false)
Constructor.
Definition: CallbackHelper.hh:183
gazebo::transport::CallbackHelper::SetLatching
void SetLatching(bool _latch)
Set whether this callback is latching.
gazebo::transport::CallbackHelperT
Callback helper Template.
Definition: CallbackHelper.hh:111
gazebo::transport::CallbackHelperT::CallbackHelperT
CallbackHelperT(const boost::function< void(const boost::shared_ptr< M const > &)> &_cb, bool _latching=false)
Constructor.
Definition: CallbackHelper.hh:117
gazebo::transport::CallbackHelperT::IsLocal
virtual bool IsLocal() const
Is the callback local?
Definition: CallbackHelper.hh:164
gazebo::transport::CallbackHelper::IsLocal
virtual bool IsLocal() const =0
Is the callback local?
gazebo::transport::CallbackHelper::CallbackHelper
CallbackHelper(bool _latching=false)
Constructor.
transport
transport
Definition: ConnectionManager.hh:35
gazebo::transport::CallbackHelper::latching
bool latching
True means that the callback helper will get the last published message on the topic.
Definition: CallbackHelper.hh:92
gazebo::transport::CallbackHelper::GetLatching
bool GetLatching() const
Is the callback latching?
gazebo::transport::CallbackHelperT::HandleMessage
virtual bool HandleMessage(MessagePtr _newMsg)
Process new incoming message.
Definition: CallbackHelper.hh:156
gazebo::transport::CallbackHelperPtr
boost::shared_ptr< CallbackHelper > CallbackHelperPtr
boost shared pointer to transport::CallbackHelper
Definition: CallbackHelper.hh:105
gazebo::transport::RawCallbackHelper
Used to connect publishers to subscribers, where the subscriber wants the raw data from the publisher...
Definition: CallbackHelper.hh:177