interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/apps/tutorials/vostut8creature.cc

Go to the documentation of this file.
00001 /* Eighth VOS tutorial.  Creating your own MetaObjects II
00002 
00003     See vostut8creature.hh for the declaration of this class and the beginning
00004     of this tutorial.
00005 
00006     All of these methods will simply set or get property values, more or
00007     less.  They use a convenient static member of Property. The convention
00008     for child object contextual names is similar to type names: a
00009     type name or type's "group" name prefix followed by the name of
00010     the property.
00011 
00012 */
00013 
00014 #include <string>
00015 #include <vos/vos/vos.hh>
00016 #include <vos/metaobjects/misc/hypercard.hh>
00017 
00018 #include "vostut8creature.hh"
00019 
00020 using namespace std;
00021 using namespace VUtil;
00022 using namespace VOS;
00023 
00024 
00025                     /* Creature */
00026 
00027 Creature::Creature(VobjectBase* superobject)
00028     : MetaObject(superobject) {
00029 }
00030 
00031 
00032 // Return the VOS type string for the metaobject
00033 const std::string Creature::getVOSType() {
00034     return "tutorial:creature";
00035 }
00036 
00037 MetaObject* Creature::new_Creature(VobjectBase* superobject, const std::string& type) {
00038     return new Creature(superobject);
00039 }
00040 
00041 
00042 /* Set genus */
00043 void Creature::setGenus(const std::string& value) {
00044     Property::setProperty(*this, "tutorial:genus", value, "string");
00045 }
00046 
00047 /* Get genus */
00048 void Creature::getGenus(std::string& value) {
00049     string t;
00050     Property::getProperty(*this, "tutorial:genus", value, t);
00051 }
00052 
00053 /* Set species */
00054 void Creature::setSpecies(const std::string& value) {
00055     Property::setProperty(*this, "tutorial:species", value, "string");
00056 }
00057 
00058 /* Get species */
00059 void Creature::getSpecies(std::string& value) {
00060     string t;
00061     Property::getProperty(*this, "tutorial:species", value, t);
00062 }
00063 
00064 /* Set common name */
00065 void Creature::setCommonName(const std::string& value) {
00066     Property::setProperty(*this, "tutorial:common-name", value, "string");
00067 }
00068 
00069 /* Get common name */
00070 void Creature::getCommonName(std::string& value) {
00071     string t;
00072     Property::getProperty(*this, "tutorial:common-name", value, t);
00073 }
00074 
00075 /* Set description */
00076 void Creature::setDescription(const std::string& value, const std::string& datatype) {
00077     Property::setProperty(*this, "tutorial:description", value, datatype);
00078 }
00079 
00080 /* Get description */
00081 void Creature::getDescription(std::string& value, std::string& datatype) {
00082     Property::getProperty(*this, "tutorial:description", value, datatype);
00083 }
00084 
00085 /* Set image data */
00086 void Creature::setImage(const std::string& data, const std::string& datatype) {
00087     Property::setProperty(*this, "tutorial:image", data, datatype);
00088 }
00089 
00090 /* Get image data */
00091 void Creature::getImage(std::string& value, std::string& datatype) {
00092     Property::getProperty(*this, "tutorial:image", value, datatype);
00093 }
00094 
00095 /* Add a pedator (something that eats this creature) */
00096 void Creature::addPredator(Creature* p) {
00097     /* We will use a hypercard to make an indirect link to the predator.  */
00098     vRef<Hypercard> hc = getSite()->createVobject<Hypercard>("predator", getDefaultPolicy());
00099     hc->setLink(p);
00100     insertChild(-1, "tutorial:predator", hc);
00101 }
00102 
00103 void Creature::setLength(double r){
00104     // The are several variants of setProperty for different types
00105     // They will set the property's datatype appropriately.
00106     Property::setProperty(*this, "tutorial:length", r);
00107 }
00108 
00109 double Creature::getLength() {
00110     double l;
00111     Property::getProperty(*this  , "tutorial:length", l);
00112     return l;
00113 }
00114 
00115 
00116 
00117 
00118                 /* Animal */
00119 
00120 Animal::Animal(VobjectBase* superobject)
00121     : Creature(superobject) {
00122 }
00123 
00124 // The convention for types that extend other types is to form a new type
00125 // name by appending a tag to the old type with a ".".  VOS will also use
00126 // this convention to "fall back" on the extended type if it sees a derived
00127 // type string that it doesn't recognize.
00128 const std::string Animal::getVOSType() {
00129     return "tutorial:creature.animal";
00130 }
00131 
00132 
00133 MetaObject* Animal::new_Animal(VobjectBase* superobject, const std::string& type) {
00134     return new Animal(superobject);
00135 }
00136 
00137 
00138 
00139 void Animal::setTailLength(double l) {
00140     Property::setProperty(*this, "tutorial:tail-length", l);
00141 }
00142 
00143 double Animal::getTailLength() {
00144     // If this property is missing, we'll return a default value.
00145     try {
00146         double l;
00147         Property::getProperty(*this, "tutorial:tail-length", l);
00148         return l;
00149     } catch(NoSuchObjectError&) {
00150         return 0.0;
00151     }
00152 }
00153 
00154 void Animal::addPrey(Creature* p) {
00155     /* We will use a hypercard to make an indirect link to the prey. */
00156     vRef<Hypercard> hc = getSite()->createVobject<Hypercard>("prey", getDefaultPolicy());
00157     hc->setLink(p);
00158     insertChild(-1, "tutorial:prey", hc);
00159 }
00160 
00161 
00162 
00163 
00164                     /* Factory registration */
00165 /* The following code between BEGIN_ and END_ will be executed
00166    _before_ the program main().  We want to register our MetaObject extension
00167    with Site, so we add our new_LocalCreature and new_RemoteCreature factory
00168    methods to a global table of MetaObject types and their factories.
00169 */
00170 BEGIN_REGISTER_METAOBJECT_FACTORIES(CreaturesEtc)
00171 
00172 /* Registering an extension consists of supplying a type string
00173    and a function to call to construct a new metaobject for that
00174    type.  Here we associate the new_LocalCreature function for the
00175    abstract class, the local class, and the VOS type name, meaning
00176    the user can call Site::createVobject() with any of
00177    these three strings and get an object with the LocalCreature extension.
00178 */
00179 Site::addLocalMetaObjectFactory(typeid(Creature).name(), &Creature::new_Creature);
00180 Site::addLocalMetaObjectFactory("tutorial:creature", &Creature::new_Creature);
00181 Site::addRemoteMetaObjectFactory(typeid(Creature).name(), "tutorial:creature", &Creature::new_Creature);
00182 Site::addRemoteMetaObjectFactory("tutorial:creature", "tutorial:creature", &Creature::new_Creature);
00183 
00184 Site::addLocalMetaObjectFactory(typeid(Animal).name(), &Animal::new_Animal);
00185 Site::addLocalMetaObjectFactory("tutorial:creature.animal", &Animal::new_Animal);
00186 Site::addRemoteMetaObjectFactory(typeid(Animal).name(), "tutorial:creature.animal", &Animal::new_Animal);
00187 Site::addRemoteMetaObjectFactory("tutorial:creature.animal", "tutorial:creature.animal", &Animal::new_Animal);
00188 
00189 END_REGISTER_METAOBJECT_FACTORIES(CreaturesEtc)
00190