Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

apps/tutorials/vostut6.cc

Go to the documentation of this file.
00001 /* Sixth VOS tutorial.  Child and Property Listeners
00002 
00003    This tutorial covers:
00004    - Subclassing ChildChangeListener and PropertyListener
00005    - Handling notify callbacks
00006    - The VobjectEvent and PropertyEvent classes
00007    - Adding listeners to Vobjects
00008 
00009    This file (vostut6.cc) is released into the public domain.  No
00010    restrictions are placed on its use, distribution or inclusion into
00011    other works.
00012 */
00013 
00014 #include <vos/corelibs/vos/vos.hh>
00015 #include <vos/metaobjects/property/property.hh>
00016 
00017 /* A very useful feature of VOS is the ability to get a notification
00018    callback when something interesting changes, such as the child list
00019    of a Vobject or the value of a property.  To handle these
00020    callbacks, you must subclass the appropriate "Listener" class.  The
00021    Listener defines certain methods which will be called when various
00022    events happen.  You must override and implement these methods in
00023    your own class.
00024  */
00025 
00026 
00027 // This should be fairly self-explanatory.  The events signaled by
00028 // notifyInserted, notifyReplaced and notifyRemoved correspond to the
00029 // insertChild, setChild and removeChild methods in Vobject.  The
00030 // VobjectEvent class provides specific information about what has happened.
00031 
00032 class TutorialChildListener : public ChildChangeListener
00033 {
00034 public:
00035     virtual void notifyChildInserted(VobjectEvent& e);
00036     virtual void notifyChildReplaced(VobjectEvent& e);
00037     virtual void notifyChildRemoved(VobjectEvent& e);
00038 };
00039 
00040 void TutorialChildListener::notifyChildInserted(VobjectEvent& e)
00041 {
00042     // Important note: unlike almost every other method in VOS, the
00043     // getChild, getParent() etc methods of VobjectEvent do NOT
00044     // increase the reference count of the Vobject before returning
00045     // it.  Be careful.
00046 
00047     cout << "Parent " << e.getParent()->getURL().getString()
00048          << " has added child " << e.getChild()->getURL().getString()
00049          << "\n at position " << e.getPosition()
00050          << " with contextual name \"" << e.getContextualName() << "\"\n";
00051 }
00052 
00053 void TutorialChildListener::notifyChildReplaced(VobjectEvent& e)
00054 {
00055     cout << "Parent " << e.getParent()->getURL().getString()
00056          << " has replaced child " << e.getOldChild()->getURL().getString()
00057          << "\n with " << e.getNewChild()->getURL().getString()
00058          << "\n at position " << e.getPosition()
00059          << " with contextual name \"" << e.getContextualName() << "\"\n";
00060 }
00061 
00062 void TutorialChildListener::notifyChildRemoved(VobjectEvent& e)
00063 {
00064     cout << "Parent " << e.getParent()->getURL().getString()
00065          << " has removed child " << e.getChild()->getURL().getString()
00066          << "\n from position " << e.getPosition()
00067          << " (old contextual name \"" << e.getContextualName() << "\")\n";
00068 }
00069 
00070 
00071 class TutorialPropertyListener : public PropertyListener
00072 {
00073 public:
00074     virtual void notifyPropertyChange(const PropertyEvent& e);
00075 };
00076 
00077 
00078 void TutorialPropertyListener::notifyPropertyChange(const PropertyEvent& e)
00079 {
00080     // Important note: unlike almost every other method in VOS, the
00081     // getProperty() method of PropertyEvent does NOT increase the
00082     // reference count of the Vobject before returning it.  Be
00083     // careful.
00084 
00085     cout << "Property " << e.getProperty()->getURL().getString()
00086          << " has changed value from \"" << e.getOldValue()
00087          << "\" to \"" << e.getNewValue() << "\"\n";
00088 
00089 }
00090 
00091 int main(int argc, char** argv)
00092 {
00093     cout << "VOS Tutorial 6\n\n";
00094 
00095     LocalSocketSite localsite(&NoAccessControl::static_);
00096     localsite.setTimeoutOnSelect(-1);
00097 
00098     Property::registerExtenders();
00099 
00100     vRef<Vobject> sphere = localsite.createMetaObject("sphere", 0);
00101     vRef<Vobject> tertius = localsite.createMetaObject("tertius", 0);
00102     vRef<Vobject> quartus = localsite.createMetaObject("quartus", typeid(Property).name(), 0);
00103     vRef<Vobject> quintus = localsite.createMetaObject("quintus", 0);
00104     vRef<Vobject> sextus = localsite.createMetaObject("sextus", 0);
00105     vRef<Vobject> septimus = localsite.createMetaObject("septimus", 0);
00106 
00107     sphere->insertChild(-1, "position", &tertius);
00108     sphere->insertChild(-1, "orientation", &quartus);
00109 
00110 
00111     /* This adds the listener.  Whenever the child list for "sphere"
00112        changes, the TutorialChildListener class will be called.  Also
00113        note that when you add a listener, it will immediately be
00114        called with notifyChildInserted() for each child the Vobject
00115        currently has.
00116      */
00117     sphere->addChildListener(new TutorialChildListener());
00118 
00119 
00120     // Mess around with sphere's child list.  This should look
00121     // familiar :-)
00122 
00123     sphere->insertChild(-1, "radius", &quintus);
00124     sphere->insertChild(-1, "position", &quartus);
00125     sphere->insertChild(2, "color", &sextus);
00126     sphere->setChild(4, "mass", &septimus);
00127     sphere->removeChild(1);
00128     sphere->removeChild(-1);
00129 
00130     /* You may use listeners on both local and remote Vobjects. When
00131        you are set up as a listener for the children of a remote
00132        vobject, that vobject's child list is cached and considered
00133        "hot" because any changes to the child list are automatically
00134        pushed to listeners.  This means that calls to methods like
00135        Vobject::getChildren() will use the cached copy and not have to
00136        access the network.  This can be a significant performance win
00137        for certain applications. */
00138 
00139 
00140     // Property listeners work similarly to child listeners.  The
00141     // listener will be called immediately when first added with the
00142     // initial value of the property, and the called as further
00143     // updates happen (from a call to write() or replace()).
00144 
00145     Property* positionproperty = MetaObject::meta_cast<Property*>(&quartus);
00146 
00147     positionproperty->addPropertyListener(new TutorialPropertyListener());
00148 
00149     positionproperty->replace("1 2 3", "text/x-vector-float");
00150 
00151     positionproperty->write(2, "4");
00152 
00153 }

Generated on Tue Aug 12 03:55:36 2003 for Interreality Project - VOS by doxygen 1.3.2