Blackboard.h
class BBItem
{
private:
friend class Blackboard;
cPolymorphic *_item;
std::string _label;
SubscriberVector subscribers;
public:
/** Return the label of this data item*/
const char *label() {return _label.c_str();}
/** Return the data item*/
cPolymorphic *data() {return _item;}
};
typedef std::vector<BlackboardAccess *> SubscriberVector;
typedef std::map<std::string, BBItem*> ContentsMap;
BBItemRef Blackboard::publish(const char *label, cPolymorphic *item)
{
Enter_Method("publish(/"%s/", %s *ptr)", label, item->className());
// check uniqueness of label
ContentsMap::iterator k = contents.find(std::string(label));
if (k!=contents.end())
error("publish(): blackboard already contains an item with label `%s'", label);
// add to BB contents
BBItem *bbitem = new BBItem();
bbitem->_item = item;
bbitem->_label = label;
contents[bbitem->_label] = bbitem;
coreEV <<"pulish "<<label<<" on bb/n";
coreEV <<"bbItem->label: "<<bbitem->label()<<endl;
// notify
SubscriberVector& vec = registeredClients;
for (SubscriberVector::iterator i=vec.begin(); i!=vec.end(); ++i){
(*i)->blackboardItemPublished(bbitem);
}
return bbitem;
}
class BlackboardAccess
{
protected:
Blackboard *bb;
public:
BlackboardAccess() {bb=NULL;}
/** Returns a pointer to the Blackboard*/
Blackboard *blackboard();
/** @name Callbacks invoked by the blackboard */
//@{
/** Called whenever an already published item changes*/
virtual bool blackboardItemChanged(BBItemRef item) = 0;
/** Called whenever a new item is published on the Blackboard*/
virtual bool blackboardItemPublished(BBItemRef item) = 0;
/** Called whenever an item is removed from the Blackboard*/
virtual bool blackboardItemWithdrawn(BBItemRef item) = 0;
//@}
};
class BlackboardAccess
{
protected:
Blackboard *bb;
public:
BlackboardAccess() {bb=NULL;}
/** Returns a pointer to the Blackboard*/
Blackboard *blackboard();
/** @name Callbacks invoked by the blackboard */
//@{
/** Called whenever an already published item changes*/
virtual bool blackboardItemChanged(BBItemRef item) = 0;
/** Called whenever a new item is published on the Blackboard*/
virtual bool blackboardItemPublished(BBItemRef item) = 0;
/** Called whenever an item is removed from the Blackboard*/
virtual bool blackboardItemWithdrawn(BBItemRef item) = 0;
//@}
};
BlackModule
是从
BlackboardAccess
类继承而来的,它提供一个函数
blackboard(),
返回一个
Blackboard
指针,通过它来调用函数,用来
subscribe to and read information published on the Blackboard.
因为
BasicModule
是所有模块的基类,所以所有的模块也能够使用
Blackboard.