Aldebaran documentation What's new in NAOqi 2.4.3?

qi::Object

Summary

Global Classes

Detailed Description

qi::AnyObject

qi::AnyObject is a specialization of qi::Object that provides type erasure on objects, similar to what qi::AnyValue does on values. It can only work on registered objects. We’ll use Graph::Drawer from the registering guide. You can make an qi::Object from a boost::shared_ptr.

qi::AnyObject obj = boost::make_shared<Graph::Drawer>();

qi::Object uses a shared_ptr semantics, so the object will be destroyed when there are no more references to it. You can also get a qi::AnyObject from a session, another service, etc.

You can call a function with call or async if you want the call to be asynchronous. You can also connect signals and change properties.

// do a synchronous call
obj.call<bool>("draw", Graph::Point(10, 20), Graph::Green);

// do an ansynchronous call
qi::Future<bool> future =
  obj.async<bool>("draw", Graph::Point(10, 20), Graph::Green);
// do stuff...
future.wait();

// connect a signal and disconnect it
int id = obj.connect("drawDone", &mycallback);
obj.disconnect(id);

// set a property and get it
obj.setProperty("origin", Graph::Point(0, 12));
Graph::Point p = obj.property<Graph::Point>("origin");
std::cout << p.y << std::endl; // 12

Signal Connection

When connecting a callback, as usual, the signature of the callback must match the signature of the signal. But since we are in a type-erased context, if this rule is not respected, the error will only appear (in the logs) when the signal is triggered, not at compile-time or at the connection.

Furthermore, the connect() function expects any type of boost::function since we don’t know that until runtime, so it must be explicitly cast to the correct type, particularly in case of boost::bind.

// this works because mycallback has a specific signature
obj.connect("drawDone", &mycallback);

// this fails at compile time because boost::bind does not force a signature
obj.connect("drawDone", boost::bind(mycallback, _1));

// this works, you must specify the signature expected by the signal
obj.connect("drawDone",
    boost::function<void(const Point&)>(boost::bind(mycallback, _1)));

qi::Object<T>

qi::Object can be specialized with T if the object is T or inherits from T.

qi::Object<Graph::Drawer> obj = boost::make_shared<Graph::Drawer>();
obj->draw(Graph::Point(11, 12), Graph::Green);

qi::Object‘s specializations do not work with remote objects yet.

Passing an object as argument

Some methods in the services you will use expect an object as argument, for instance Logger::addListener(Object<LogListener> listener);. To call this method, you must first implement the LogListener interface into your own class, and then wrap a pointer to an instance of this class into an Object<LogListener> or a qi::AnyObject that will take ownership of the pointer:

class MyLogListener: public LogListener
{
  // Implement LogListener interface
};

void someFunction()
{
  qi::AnyObject logger = session.service("Logger");
  qi::AnyObject o(boost::make_shared<LogListener>());
  logger.call("addListener", o);
}

In the example above, your instance of MyLogListener will be kept alive as long as the logger service holds a qi::AnyObject on it. The same holds true when returning objects.

Reference

typedef Object< Empty > AnyObject

qi::Object Class Reference

Introduction

More...

#include <qi/anyobject.hpp>

Public Functions

Object(GenericObject* go)
Object(T* ptr)
Object(GenericObject* go, boost::function<void(GenericObject*)> deleter)
Object(T* ptr, boost::function<void(T*)> deleter)
Object()
template<typename U>
Object(const Object<U>& o)
template<typename U>
void operator=(const Object<U>& o)
Object(const Object& o)
void operator=(const Object& o)
Object(const qi::Future<MaybeAnyObject>& fobj)
Object(const qi::FutureSync<MaybeAnyObject>& fobj)
template<typename U>
Object(GenericObject* go, boost::shared_ptr<U> other)
template<typename U>
Object(boost::shared_ptr<U> other)
bool operator<(const Object& b) const
template<typename U>
bool operator!=(const Object<U>& b) const
template<typename U>
bool operator==(const Object<U>& b) const
operator bool() const
operator Object<Empty>() const
boost::shared_ptr<T> asSharedPtr()
T& asT()
const T& asT() const
T* operator->()
const T* operator->() const
T& operator*()
const T& operator*() const
bool unique() const
GenericObject* asGenericObject() const
void reset()
unsigned int use_count() const
ObjectTypeInterface* interface()
void checkT()
detail::ManagedObjectPtr managedObjectPtr()

const MetaObject& metaObject() const
qi::Future<AnyReference> metaCall(unsigned int method, const GenericFunctionParameters& params, MetaCallType callType, Signature returnSignature)
int findMethod(const std::string& name, const GenericFunctionParameters& parameters) const
qi::Future<AnyReference> metaCall(const std::string& nameWithOptionalSignature, const GenericFunctionParameters& params, MetaCallType callType, Signature returnSignature)
void metaPost(unsigned int event, const GenericFunctionParameters& params) const
void metaPost(const std::string& nameWithOptionalSignature, const GenericFunctionParameters& in) const
void post(const std::string& eventName, qi::AutoAnyReference p1)
template<typename FUNCTOR_TYPE>
qi::FutureSync<SignalLink> connect(const std::string& eventName, FUNCTOR_TYPE callback, MetaCallType threadingModel) const
qi::FutureSync<SignalLink> connect(const std::string& name, const SignalSubscriber& functor) const
qi::FutureSync<SignalLink> connect(unsigned int signal, const SignalSubscriber& subscriber) const
qi::FutureSync<SignalLink> connect(unsigned int signal, AnyObject target, unsigned int slot) const
qi::FutureSync<void> disconnect(SignalLink linkId) const
template<typename T>
qi::FutureSync<T> property(const std::string& name) const
template<typename T>
qi::FutureSync<void> setProperty(const std::string& name, const T& val) const
qi::FutureSync<AnyValue> property(unsigned int id) const
qi::FutureSync<void> setProperty(unsigned int id, const AnyValue& val) const
ExecutionContext* executionContext() const
bool isStatsEnabled() const
void enableStats(bool enable) const
ObjectStatistics stats() const
void clearStats() const
bool isTraceEnabled() const
void enableTrace(bool enable)
void forceExecutionContext(boost::shared_ptr<qi::ExecutionContext> ec)

Public Static Functions

void keepManagedObjectPtr(detail::ManagedObjectPtr ptr)
template<typename U>
void keepReference(GenericObject* obj, boost::shared_ptr<U> ptr)
void noDeleteT(T*)
void noDelete(GenericObject*)
void deleteGenericObjectOnly(GenericObject* obj)
template<typename U>
void deleteGenericObjectOnlyAndKeep(GenericObject* obj, U)
void deleteCustomDeleter(GenericObject* obj, boost::function<void(T*)> deleter)

Types

typedef boost::mpl::if_< typename boost::is_same< T, Empty >::type, None, Object< Empty > >::type MaybeAnyObject

Detailed Description

Type erased object that has a known interface T.

In case T is unknown, you can use qi::AnyObject which aliases to Object<qi::Empty>.

You can then use the object with type-erasure or call the object directly using the operator ->.

Functions Documentation

static void qi::Object<T>::keepManagedObjectPtr(detail::ManagedObjectPtr ptr)
template<typename U>
static void qi::Object<T>::keepReference(GenericObject* obj, boost::shared_ptr<U> ptr)
static void qi::Object<T>::noDeleteT(T*)
static void qi::Object<T>::noDelete(GenericObject*)
static void qi::Object<T>::deleteGenericObjectOnly(GenericObject* obj)
template<typename U>
static void qi::Object<T>::deleteGenericObjectOnlyAndKeep(GenericObject* obj, U)
static void qi::Object<T>::deleteCustomDeleter(GenericObject* obj, boost::function<void(T*)> deleter)
qi::Object<T>::Object(GenericObject* go)

These constructors take ownership of the underlying pointers. If a callback is given, it will be called instead of the default behavior of deleting the stored GenericObject and the underlying T object.

qi::Object<T>::Object(T* ptr)
qi::Object<T>::Object(GenericObject* go, boost::function<void(GenericObject*)> deleter)
qi::Object<T>::Object(T* ptr, boost::function<void(T*)> deleter)
qi::Object<T>::Object()
template<typename U>
qi::Object<T>::Object(const Object<U>& o)
template<typename U>
void qi::Object<T>::operator=(const Object<U>& o)
qi::Object<T>::Object(const Object& o)
void qi::Object<T>::operator=(const Object& o)
qi::Object<T>::Object(const qi::Future<MaybeAnyObject>& fobj)
qi::Object<T>::Object(const qi::FutureSync<MaybeAnyObject>& fobj)
template<typename U>
qi::Object<T>::Object(GenericObject* go, boost::shared_ptr<U> other)

Shares ref counter with other, which must handle the destruction of go.

template<typename U>
qi::Object<T>::Object(boost::shared_ptr<U> other)
bool qi::Object<T>::operator<(const Object& b) const
template<typename U>
bool qi::Object<T>::operator!=(const Object<U>& b) const
template<typename U>
bool qi::Object<T>::operator==(const Object<U>& b) const
qi::Object<T>::operator bool() const
qi::Object<T>::operator Object<Empty>() const
boost::shared_ptr<T> qi::Object<T>::asSharedPtr()
T& qi::Object<T>::asT()
const T& qi::Object<T>::asT() const
T* qi::Object<T>::operator->()
const T* qi::Object<T>::operator->() const
T& qi::Object<T>::operator*()
const T& qi::Object<T>::operator*() const
bool qi::Object<T>::unique() const
GenericObject* qi::Object<T>::asGenericObject() const
void qi::Object<T>::reset()
unsigned int qi::Object<T>::use_count() const
ObjectTypeInterface* qi::Object<T>::interface()
void qi::Object<T>::checkT()

Check tha value actually has the T interface.

detail::ManagedObjectPtr qi::Object<T>::managedObjectPtr()

const MetaObject& qi::detail::GenericObjectBounce<O>::metaObject() const
qi::Future<AnyReference> qi::detail::GenericObjectBounce<O>::metaCall(unsigned int method, const GenericFunctionParameters& params, MetaCallType callType = MetaCallType_Auto, Signature returnSignature = Signature()
int qi::detail::GenericObjectBounce<O>::findMethod(const std::string& name, const GenericFunctionParameters& parameters) const
qi::Future<AnyReference> qi::detail::GenericObjectBounce<O>::metaCall(const std::string& nameWithOptionalSignature, const GenericFunctionParameters& params, MetaCallType callType = MetaCallType_Auto, Signature returnSignature = Signature()
void qi::detail::GenericObjectBounce<O>::metaPost(unsigned int event, const GenericFunctionParameters& params) const
void qi::detail::GenericObjectBounce<O>::metaPost(const std::string& nameWithOptionalSignature, const GenericFunctionParameters& in) const
void qi::detail::GenericObjectBounce<O>::post(const std::string& eventName, qi::AutoAnyReference p1 = qi::AutoAnyReference()
template<typename FUNCTOR_TYPE>
qi::FutureSync<SignalLink> qi::detail::GenericObjectBounce<O>::connect(const std::string& eventName, FUNCTOR_TYPE callback, MetaCallType threadingModel = MetaCallType_Auto) const
qi::FutureSync<SignalLink> qi::detail::GenericObjectBounce<O>::connect(const std::string& name, const SignalSubscriber& functor) const
qi::FutureSync<SignalLink> qi::detail::GenericObjectBounce<O>::connect(unsigned int signal, const SignalSubscriber& subscriber) const
qi::FutureSync<SignalLink> qi::detail::GenericObjectBounce<O>::connect(unsigned int signal, AnyObject target, unsigned int slot) const
qi::FutureSync<void> qi::detail::GenericObjectBounce<O>::disconnect(SignalLink linkId) const
template<typename T>
qi::FutureSync<T> qi::detail::GenericObjectBounce<O>::property(const std::string& name) const
template<typename T>
qi::FutureSync<void> qi::detail::GenericObjectBounce<O>::setProperty(const std::string& name, const T& val) const
qi::FutureSync<AnyValue> qi::detail::GenericObjectBounce<O>::property(unsigned int id) const
qi::FutureSync<void> qi::detail::GenericObjectBounce<O>::setProperty(unsigned int id, const AnyValue& val) const
ExecutionContext* qi::detail::GenericObjectBounce<O>::executionContext() const
bool qi::detail::GenericObjectBounce<O>::isStatsEnabled() const
void qi::detail::GenericObjectBounce<O>::enableStats(bool enable) const
ObjectStatistics qi::detail::GenericObjectBounce<O>::stats() const
void qi::detail::GenericObjectBounce<O>::clearStats() const
bool qi::detail::GenericObjectBounce<O>::isTraceEnabled() const
void qi::detail::GenericObjectBounce<O>::enableTrace(bool enable)
void qi::detail::GenericObjectBounce<O>::forceExecutionContext(boost::shared_ptr<qi::ExecutionContext> ec)